refresh_token 授予类型提供另一个刷新令牌?

refresh_token grant type supplies another refresh token?

在开发我的 Spring 休息 API 时,我注意到一些我不太明白的事情。我正在使用 refresh_token 授权类型。当我访问 /myapi/oauth/token?grant_type=refresh_token 时它工作正常,但我很困惑为什么它 returns 是一个闪亮的新刷新令牌以及一个新的访问令牌。

这不会使刷新令牌的到期时间变得无用吗?如果他们可以使用一个刷新令牌来获得另一个刷新令牌,他们不就拥有了无限的刷新令牌吗?

编辑:OAuth 配置的相关部分

@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {

@Autowired
private AuthenticationManager authenticationManager;

@Autowired
private UserDetailsService userDetailsService;

@Bean
public JwtAccessTokenConverter accessTokenConverter() {
    JwtAccessTokenConverter accessTokenConverter = new JwtAccessTokenConverter();
    accessTokenConverter.setSigningKey("abcd1234");
    return accessTokenConverter;
}

@Bean
public TokenStore tokenStore() {
    return new JwtTokenStore(accessTokenConverter());
}

@Bean
@Primary
public DefaultTokenServices tokenServices() {
    DefaultTokenServices defaultTokenServices = new DefaultTokenServices();
    defaultTokenServices.setTokenStore(tokenStore());
    defaultTokenServices.setSupportRefreshToken(true);
    return defaultTokenServices;
}

@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
    clients.inMemory()
            .withClient("myclient")
            .secret("mysecret")
            .authorizedGrantTypes("password", "refresh_token")
            .scopes("read")
            .autoApprove("read")
            .accessTokenValiditySeconds(300)
            .refreshTokenValiditySeconds(12000);
}

@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
    TokenEnhancerChain tokenEnhancerChain = new TokenEnhancerChain();
    tokenEnhancerChain.setTokenEnhancers(Arrays.accessTokenConverter()));
    endpoints
            .tokenStore(tokenStore())
            .tokenEnhancer(tokenEnhancerChain)
            .authenticationManager(authenticationManager)
            .userDetailsService(userDetailsService);
}

}

根据 OAuth2 spec(H 点),在刷新访问令牌时可选择 return 一个新的刷新令牌是可以的,因此该行为符合规范。

Spring Boot 提供了 reuseRefreshToken 标志来控制这种行为。它默认设置为 true,这意味着刷新令牌 应该 被重用,并且当您请求新的访问令牌时它应该 生成新的刷新令牌.

但是,在 JWT 令牌的情况下,有一个开放的 bug,因此,标志值似乎被忽略,并且每次在 JwtAccessTokenConverter#enhance() 方法中都会生成一个新的刷新令牌。

旁注:reuseRefreshToken 标志可以在 AuthorizationServerConfigurerAdapter 的实例上设置,或者通过公开类型 DefaultTokenServices 的 bean 并在其中设置标志。