小编Pat*_*rix的帖子

在客户端 Spring Boot 应用程序上配置自定义 OAuth2AccessToken

授权服务器通常为您提供的标准 JSON 格式有一个名为“expires_in”的属性,但现在我正在使用一个自动化服务器,它为我提供了一个名为“access_token_expires_in”的属性。因此,即使在 access_token 过期时,我的 OAuth2AccessToken 也总是将 isExpired 返回为 false,这是有道理的,因为它试图读取不存在的“expires_in”属性。OAuth2AccessToken 的 getAdditionalInformation 返回我的“access_token_expires_in”属性值,值为 18000。

我想知道我是否可以告诉 spring 使用“access_token_expires_in”属性作为我的 access_token 的过期值?

我的代码:

@Configuration
class OAuth2RestConfiguration {
    @Bean
    protected OAuth2ProtectedResourceDetails resource() {

        final ClientCredentialsResourceDetails resourceDetails = new ClientCredentialsResourceDetails();
        resourceDetails.setAccessTokenUri("<tokenUri>");
        resourceDetails.setClientId("<clientId>");
        resourceDetails.setClientSecret("<clientSecret>");

        return resourceDetails;
    }

    @Bean
    public OAuth2RestTemplate restTemplate() throws Exception {
        final AccessTokenRequest atr = new DefaultAccessTokenRequest();
        final OAuth2RestTemplate oAuth2RestTemplate = new OAuth2RestTemplate(resource(),
                new DefaultOAuth2ClientContext(atr));

        return oAuth2RestTemplate;
    }
}
Run Code Online (Sandbox Code Playgroud)

授权服务器响应示例:

{
    "refresh_token_expires_in": 0,
    "access_token": "<access_token>",
    "access_token_expires_in": 18000,
    "token_type": "bearer"
}
Run Code Online (Sandbox Code Playgroud)

编辑 1: 作为一种解决方法,我扩展了 …

java spring-boot spring-security-oauth2

4
推荐指数
1
解决办法
5024
查看次数

标签 统计

java ×1

spring-boot ×1

spring-security-oauth2 ×1