我正在尝试使用令牌交换功能模拟用户,但我总是收到不允许客户端交换的错误。
这是我使用 WebFlux WebClient 执行的请求:
MultiValueMap<String, String> formData = new LinkedMultiValueMap<>();
formData.add("grant_type", "urn:ietf:params:oauth:grant-type:token-exchange");
formData.add("requested_token_type", "urn:ietf:params:oauth:token-type:access_token");
formData.add("client_id", keycloakClientId);
formData.add("requested_subject", userId);
formData.add("subject_token", token);
return WebClient.create()
.post()
.uri(authServerUrl + "/realms/" + authServerRealm + "/protocol/openid-connect/token")
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.body(BodyInserters.fromFormData(formData))
.exchangeToMono(clientResponse -> clientResponse.bodyToMono(AccessTokenResponse.class));
我得到的回应是这样的:
{"error":"access_denied","error_description":"Client not allowed to exchange"}
如何获取上面要交换的令牌:
KeycloakBuilder.builder()
.serverUrl(authServerUrl)
.realm(authServerRealm)
.grantType(OAuth2Constants.PASSWORD)
.username(keycloakAdminUsername)
.password(keycloakAdminPassword)
.clientId(keycloakClientId)
.build();
我做错了什么?为什么需要允许客户端进行交换,因为此模拟而不是客户端令牌交换。客户是否需要保密才能完成此操作?
答案 0 :(得分:0)
您需要授予权限(在目标客户端上)以允许 keycloakClientId
为目标客户端创建令牌。
文档中的 7.1.1:
https://www.keycloak.org/docs/latest/securing_apps/#internal-token-to-internal-token-exchange
您还需要使用以下标志启动 keycloak,以便启用细粒度权限:
-Dkeycloak.profile.feature.admin_fine_grained_authz=enabled -Dkeycloak.profile.feature.token_exchange=enabled