oAuth2.0中access_token默认有效时长为12个小时,refresh_token默认时长为30天。在实际运用中需要根据需求设置有效时长。
在AuthorizationServerConfigurerAdapter,重写一个TokenServices,
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
// 设置令牌
endpoints.tokenStore(tokenStore()).userDetailsService(userDetailsService1());
// 最后一个参数为替换之后授权页面的url
endpoints.pathMapping("/oauth/confirm_access","/custom/confirm_access");
//endpoints.tokenServices(defaultTokenServices());
DefaultTokenServices tokenServices = new DefaultTokenServices();
tokenServices.setTokenStore(endpoints.getTokenStore());
tokenServices.setSupportRefreshToken(true);
tokenServices.setClientDetailsService(endpoints.getClientDetailsService());
tokenServices.setTokenEnhancer(endpoints.getTokenEnhancer());
tokenServices.setAccessTokenValiditySeconds(60*60*2);//token有效期设置2个小时
tokenServices.setRefreshTokenValiditySeconds(60*60*12);//Refresh_token:12个小时
endpoints.tokenServices(tokenServices);
}
也可以参考如下博客配置:
Spring cloud oauth2.0 access_token 永不失效设置方法
一个朋友新做的公众号,帮忙宣传一下,会不定时推送一些开发中碰到的问题的解决方法,以及会分享一些开发视频。资料等。请大家关注一下谢谢: