8.6. 授权客户端 java API
根据您的要求,资源服务器应能够远程管理资源,甚至以编程方式检查权限。如果使用 Java,您可以使用 Authorization Client API 访问红帽构建的 Keycloak 授权服务。
对于需要访问服务器提供的不同端点的资源服务器的目标,如 Token Endpoint、Resource 和 Permission 管理端点。
8.6.1. Maven 依赖项 复制链接链接已复制到粘贴板!
<dependencies>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-authz-client</artifactId>
<version>${KEYCLOAK_VERSION}</version>
</dependency>
</dependencies>
8.6.2. Configuration 复制链接链接已复制到粘贴板!
客户端配置在 keycloak.json 文件中定义,如下所示:
{
"realm": "hello-world-authz",
"auth-server-url" : "http://localhost:8080",
"resource" : "hello-world-authz-service",
"credentials": {
"secret": "secret"
}
}
realm (必需)
域的名称。
auth-server-url (必需)
红帽构建的 Keycloak 服务器的基本 URL。所有其他红帽构建的 Keycloak 页面和 REST 服务端点都源自此内容。它通常采用 https://host:port 格式。
resource (必需)
应用程序的客户端 ID。每个应用都有一个客户端 ID,用于识别应用。
credentials (必需)
指定应用程序的凭证。这是一个对象表示法,其中键是凭证类型,值是凭证类型的值。
配置文件通常位于应用程序的 classpath 中,来自客户端要尝试查找 文件的默认位置。
keycloak.json
8.6.3. 创建授权客户端 复制链接链接已复制到粘贴板!
考虑在 classpath 中有一个 文件,您可以创建一个新的 keycloak.json 实例,如下所示:
AuthzClient
// create a new instance based on the configuration defined in a keycloak.json located in your classpath
AuthzClient authzClient = AuthzClient.create();
8.6.4. 获取用户权利 复制链接链接已复制到粘贴板!
以下是如何获取用户权利的示例:
// 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
以下是如何为一个或多个资源获取用户权利的示例:
// 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 创建资源 复制链接链接已复制到粘贴板!
// 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 复制链接链接已复制到粘贴板!
// 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);
}