如何从Keycloak的AuthzClient获取其他声明

huangapple go评论105阅读模式
英文:

How to get other claims from Keycloak AuthzClient

问题

  1. 我正在使用 AuthzClient 通过以下代码获取访问令牌:
  2. Map<String, Object> clientCredentials = new HashMap<>();
  3. clientCredentials.put("secret", keycloakClientSecret);
  4. Configuration configuration = new Configuration(
  5. keycloakUrl, keycloakRealmName, keycloakClientId, clientCredentials, null
  6. );
  7. AuthzClient authzClient = AuthzClient.create(configuration);
  8. AccessTokenResponse accessTokenResponse = authzClient.obtainAccessToken(
  9. loginRequest.getUsername(), loginRequest.getPassword()
  10. );
  11. System.out.println(accessTokenResponse.getOtherClaims());
  12. 我成功获取了访问令牌和刷新令牌,但无法获取其他声明(claims)。它是空的。
  13. 我已经配置了 Mapper 来包括我从门户获取的自定义属性。在这里,我做错了什么?
英文:

I'm using AuthzClient to obtain a access token using the following code:

  1. Map&lt;String,Object&gt; clientCredentials = new HashMap&lt;&gt;();
  2. clientCredentials.put(&quot;secret&quot;, keycloakClientSecret);
  3. Configuration configuration = new Configuration(
  4. keycloakUrl, keycloakRealmName, keycloakClientId, clientCredentials, null
  5. );
  6. AuthzClient authzClient = AuthzClient.create(configuration);
  7. AccessTokenResponse accessTokenResponse = authzClient.obtainAccessToken(
  8. loginRequest.getUsername(), loginRequest.getPassword()
  9. );
  10. System.out.println(accessTokenResponse.getOtherClaims());

I'm getting the access token and refresh token successfully but I can't get the other claims. It's empty.

I've configured Mapper to include my custom attribute from portal. What I'm doing wrong here?

答案1

得分: 1

我在关于Keycloak的authzclient方面没有找到任何解决方案。但我正在使用JWT解码的解决方案,网址为https://github.com/auth0/java-jwt。

我的示例与jwt-decoder类似:

  1. public Claim getClaimByName(String key)
  2. {
  3. try {
  4. DecodedJWT jwt = JWT.decode(this.tokenResponse.getAccessToken()); // just token as String
  5. return jwt.getClaim(key);
  6. } catch (JWTCreationException exception){
  7. return null;
  8. }
  9. }
  10. getClaimByName("site").asString()

请注意,这是你提供的内容的中文翻译部分。

英文:

I didnt find any solution about keycloak authzclient. But i am using jwt decode solution as https://github.com/auth0/java-jwt

my example like this with jwt-decoder:

  1. public Claim getClaimByName(String key)
  2. {
  3. try {
  4. DecodedJWT jwt = JWT.decode(this.tokenResponse.getAccessToken()); // just token as String
  5. return jwt.getClaim(key);
  6. } catch (JWTCreationException exception){
  7. return null;
  8. }
  9. }
  10. getClaimByName(&quot;site&quot;).asString()

huangapple
  • 本文由 发表于 2020年4月9日 20:20:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/61121041.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定