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

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

How to get other claims from Keycloak AuthzClient

问题

我正在使用 AuthzClient 通过以下代码获取访问令牌:

    Map<String, Object> clientCredentials = new HashMap<>();
    clientCredentials.put("secret", keycloakClientSecret);

    Configuration configuration = new Configuration(
            keycloakUrl, keycloakRealmName, keycloakClientId, clientCredentials, null
    );

    AuthzClient authzClient = AuthzClient.create(configuration);
    AccessTokenResponse accessTokenResponse = authzClient.obtainAccessToken(
            loginRequest.getUsername(), loginRequest.getPassword()
    );

    System.out.println(accessTokenResponse.getOtherClaims());

我成功获取了访问令牌和刷新令牌,但无法获取其他声明(claims)。它是空的。

我已经配置了 Mapper 来包括我从门户获取的自定义属性。在这里,我做错了什么?
英文:

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

Map&lt;String,Object&gt; clientCredentials = new HashMap&lt;&gt;();
clientCredentials.put(&quot;secret&quot;, keycloakClientSecret);

Configuration configuration = new Configuration(
        keycloakUrl, keycloakRealmName, keycloakClientId, clientCredentials, null
);

AuthzClient authzClient = AuthzClient.create(configuration);
AccessTokenResponse accessTokenResponse = authzClient.obtainAccessToken(
        loginRequest.getUsername(), loginRequest.getPassword()
);

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类似:

public Claim getClaimByName(String key)
{
    try {
        DecodedJWT jwt = JWT.decode(this.tokenResponse.getAccessToken()); // just token as String
        return jwt.getClaim(key);
    } catch (JWTCreationException exception){
        return null;
    }
}
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:

public Claim getClaimByName(String key)
    {
        try {
            DecodedJWT jwt = JWT.decode(this.tokenResponse.getAccessToken()); // just token as String
            return jwt.getClaim(key);
        } catch (JWTCreationException exception){
         return null;
        }
    }
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:

确定