如何使用 Spring 引导和 OAuth2.0 配置新访问令牌的实验和生成

How to configure experation and generation of new access token for login with Spring boot and OAuth2.0

我已经实施 Spring 使用 OAuth 2.0 授权启动。

当我调用 /oauth/token 端点时,我收到下一个输出:

 `{
    "access_token": "03497838-790f-495f-91f4-029628ad084b",
    "token_type": "bearer",
    "refresh_token": "386ce1bd-5f76-4ea5-9091-5b46cec5429d",
    "scope": "tester"
}`

问题是我想至少每小时接收一次新的访问令牌。我看到可以配置“expire_in”,但我应该怎么做?

我尝试添加令牌有效性,但没有帮助

    @Bean
public AuthorizationServerTokenServices tokenServices() {
            DefaultTokenServices tokenServices = new DefaultTokenServices();
            tokenServices.setTokenStore(tokenStore());
            tokenServices.setSupportRefreshToken(true);
            tokenServices.setClientDetailsService(clientDetailsService());
            //tokenServices.setTokenEnhancer(endpoints.getTokenEnhancer());
            tokenServices.setAccessTokenValiditySeconds(10);
    return tokenServices;
}

@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) {

    endpoints
        .approvalStoreDisabled()
        //.approvalStore(approvalStore())
        .authorizationCodeServices(authorizationCodeServices())
        .tokenStore(tokenStore())
        .tokenServices(tokenServices())
        .authenticationManager(authenticationManager)
        .userDetailsService(restUserDetailsService);
}

我运行不同的配置,有时它会禁用令牌,但较新的会创建新的。

我发现默认的过期时间是 12 小时,这对我来说太长了。

有什么方法可以指定何时 Spring 认为访问令牌无效并创建一个新的?

我发现如果你想让你的令牌过期,那么你需要设置验证令牌期限 在 db table 中,在列 access_token_validity table oauth_client_details 中。然后收到的令牌将包含“expires_in”部分,过期后您将获得新令牌。