8.6. 권한 부여 클라이언트 Java API


리소스 서버는 요구 사항에 따라 리소스를 원격으로 관리하거나 프로그래밍 방식으로 권한을 확인할 수 있어야 합니다. Java를 사용하는 경우 Authorization Client API를 사용하여 Red Hat Single Sign-On 인증 서비스에 액세스할 수 있습니다.

토큰 엔드 포인트, 리소스, 권한 관리 엔드포인트와 같이 서버에서 제공하는 다양한 엔드포인트에 액세스하려는 리소스 서버를 대상으로 합니다.

8.6.1. Maven 종속성

Copy to Clipboard Toggle word wrap
<dependencies>
    <dependency>
        <groupId>org.keycloak</groupId>
        <artifactId>keycloak-authz-client</artifactId>
        <version>${KEYCLOAK_VERSION}</version>
    </dependency>
</dependencies>

8.6.2. 설정

클라이언트 구성은 다음과 같이 keycloak.json 파일에 정의됩니다.

Copy to Clipboard Toggle word wrap
{
  "realm": "hello-world-authz",
  "auth-server-url" : "http://localhost:8080/auth",
  "resource" : "hello-world-authz-service",
  "credentials": {
    "secret": "secret"
  }
}
  • 영역 (필수)

    영역의 이름입니다.

  • auth-server-url (필수)

    Red Hat Single Sign-On 서버의 기본 URL입니다. 다른 모든 Red Hat Single Sign-On 페이지 및 REST 서비스 엔드포인트는 여기에서 파생됩니다. 일반적으로 https://host:port/auth 형식으로 되어 있습니다.

  • 리소스 (필수)

    애플리케이션의 클라이언트 ID입니다. 각 애플리케이션에는 애플리케이션을 식별하는 데 사용되는 클라이언트 ID가 있습니다.

  • 인증 정보 (필수)

    애플리케이션의 자격 증명을 지정합니다. 키가 인증 정보 유형이고 값은 인증 정보 유형의 값인 오브젝트 표기법입니다.

구성 파일은 일반적으로 클라이언트가 keycloak.json 파일을 찾으려고 하는 기본 위치인 애플리케이션의 classpath에 있습니다.

8.6.3. 권한 부여 클라이언트 생성

classpath에 keycloak.json 파일이 있으면 다음과 같이 새 AuthzClient 인스턴스를 만들 수 있습니다.

Copy to Clipboard Toggle word wrap
    // create a new instance based on the configuration defined in a keycloak.json located in your classpath
    AuthzClient authzClient = AuthzClient.create();

8.6.4. 사용자 인타이틀먼트 가져오기

다음은 사용자 인타이틀먼트를 얻는 방법을 보여주는 예입니다.

Copy to Clipboard Toggle word wrap
// create a new instance based on the configuration defined in keycloak.json
AuthzClient authzClient = AuthzClient.create();

// create an authorization request
AuthorizationRequest request = new AuthorizationRequest();

// send the entitlement request to the server in order to
// obtain an RPT with all permissions granted to the user
AuthorizationResponse response = authzClient.authorization("alice", "alice").authorize(request);
String rpt = response.getToken();

System.out.println("You got an RPT: " + rpt);

// now you can use the RPT to access protected resources on the resource server

다음은 하나 이상의 리소스 집합에 대한 사용자 자격을 얻는 방법을 보여주는 예입니다.

Copy to Clipboard Toggle word wrap
// create a new instance based on the configuration defined in keycloak.json
AuthzClient authzClient = AuthzClient.create();

// create an authorization request
AuthorizationRequest request = new AuthorizationRequest();

// add permissions to the request based on the resources and scopes you want to check access
request.addPermission("Default Resource");

// send the entitlement request to the server in order to
// obtain an RPT with permissions for a single resource
AuthorizationResponse response = authzClient.authorization("alice", "alice").authorize(request);
String rpt = response.getToken();

System.out.println("You got an RPT: " + rpt);

// now you can use the RPT to access protected resources on the resource server

8.6.5. 보호 API를 사용하여 리소스 생성

Copy to Clipboard Toggle word wrap
// create a new instance based on the configuration defined in keycloak.json
AuthzClient authzClient = AuthzClient.create();

// create a new resource representation with the information we want
ResourceRepresentation newResource = new ResourceRepresentation();

newResource.setName("New Resource");
newResource.setType("urn:hello-world-authz:resources:example");

newResource.addScope(new ScopeRepresentation("urn:hello-world-authz:scopes:view"));

ProtectedResource resourceClient = authzClient.protection().resource();
ResourceRepresentation existingResource = resourceClient.findByName(newResource.getName());

if (existingResource != null) {
    resourceClient.delete(existingResource.getId());
}

// create the resource on the server
ResourceRepresentation response = resourceClient.create(newResource);
String resourceId = response.getId();

// query the resource using its newly generated id
ResourceRepresentation resource = resourceClient.findById(resourceId);

System.out.println(resource);

8.6.6. RPT 검사

Copy to Clipboard Toggle word wrap
// create a new instance based on the configuration defined in keycloak.json
AuthzClient authzClient = AuthzClient.create();

// send the authorization request to the server in order to
// obtain an RPT with all permissions granted to the user
AuthorizationResponse response = authzClient.authorization("alice", "alice").authorize();
String rpt = response.getToken();

// introspect the token
TokenIntrospectionResponse requestingPartyToken = authzClient.protection().introspectRequestingPartyToken(rpt);

System.out.println("Token status is: " + requestingPartyToken.getActive());
System.out.println("Permissions granted by the server: ");

for (Permission granted : requestingPartyToken.getPermissions()) {
    System.out.println(granted);
}
맨 위로 이동
Red Hat logoGithubredditYoutubeTwitter

자세한 정보

평가판, 구매 및 판매

커뮤니티

Red Hat 문서 정보

Red Hat을 사용하는 고객은 신뢰할 수 있는 콘텐츠가 포함된 제품과 서비스를 통해 혁신하고 목표를 달성할 수 있습니다. 최신 업데이트를 확인하세요.

보다 포괄적 수용을 위한 오픈 소스 용어 교체

Red Hat은 코드, 문서, 웹 속성에서 문제가 있는 언어를 교체하기 위해 최선을 다하고 있습니다. 자세한 내용은 다음을 참조하세요.Red Hat 블로그.

Red Hat 소개

Red Hat은 기업이 핵심 데이터 센터에서 네트워크 에지에 이르기까지 플랫폼과 환경 전반에서 더 쉽게 작업할 수 있도록 강화된 솔루션을 제공합니다.

Theme

© 2025 Red Hat, Inc.