gpt4 book ai didi

spring-boot - Java客户端刷新keycloak token

转载 作者:行者123 更新时间:2023-12-04 03:25:24 26 4
gpt4 key购买 nike

想象,

以下是 keyclock 中的 2 个客户端(2 个微服务)。

  • rest-service-1
  • rest-service-2

  • 以下是rest-service-2中的角色
  • service-2-user

  • 做service to service call,即:rest-service-1调用rest-service-2

    'rest-service-1' 在 Keycloak 中配置了以下值
    Access Type: confidential
    Service Account Enabled: Yes

    此外,在 rest-service-1 的“服务帐户角色”下,添加/映射了以下角色
    Role for client rest-service-2: service-2-user

    在keyclock中设置2个客户端和服务帐户以调用客户端后。我创建了一个 Spring boot 2.0.3 项目并使用以下代码获取 token 。
    @Bean
    public AuthzClient authzClient(KeycloakSpringBootProperties kcProperties) {
    //org.keycloak.authorization.client.Configuration
    Configuration configuration =
    new Configuration(kcProperties.getAuthServerUrl(),
    kcProperties.getRealm(),
    kcProperties.getResource(),
    kcProperties.getCredentials(), null);

    return AuthzClient.create(configuration);
    }

    这是我获取访问 token 的方法
    @Autowired
    private AuthzClient authzClient;

    public AccessTokenResponse token() {
    return authzClient.obtainAccessToken();
    }

    以下是收到的 token :
    {
    "access_token": ${access-token},
    "expires_in": 300,
    "refresh_expires_in": 1800,
    "refresh_token": ${refresh-token},
    "token_type": "bearer",
    "id_token": null,
    "not-before-policy": 0,
    "session_state": "6f284b2f-5bb6-4018-8acd-b83923ebb7d7",
    "scope": "profile email"
    }

    注意:我替换了 token 以使其简短/简短。

    问题:

    How do use the refresh token stated above and get a new Access Token. Does AuthzClient support that? If so, how do I do that?

    Do I need to create a new Instance of TokenCallable and get the token? If so how to instanciate TokenCallable?

    Is TokenCallable thread safe?

    最佳答案

    您不能通过 AuthzClient 明确地做到这一点。类(class)。但是,您可以使用来自 org.keycloak.authorization.client.util 的一些低级 API。包,即 Http类(class)。例如:

    public AccessTokenResponse refreshToken(String refreshToken) {
    String url = kcProperties.getAuthServerUrl() + "/realms/" + kcProperties.getRealm() + "/protocol/openid-connect/token";
    String clientId = kcProperties.getResource();
    String secret = (String) kcProperties.getCredentials().get("secret");
    Http http = new Http(kcConfig, (params, headers) -> {});

    return http.<AccessTokenResponse>post(url)
    .authentication()
    .client()
    .form()
    .param("grant_type", "refresh_token")
    .param("refresh_token", refreshToken)
    .param("client_id", clientId)
    .param("client_secret", secret)
    .response()
    .json(AccessTokenResponse.class)
    .execute();
    }

    @Bean
    public org.keycloak.authorization.client.Configuration kcConfig() {
    return new org.keycloak.authorization.client.Configuration(
    kcProperties.getAuthServerUrl(),
    kcProperties.getRealm(),
    kcProperties.getResource(),
    kcProperties.getCredentials(),
    null
    );
    }

    此解决方案是完全线程安全的(有关详细信息,请参阅 CloseableHttpClient)。

    关于spring-boot - Java客户端刷新keycloak token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51091376/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com