如何解决 Spring Security Oauth2中refresh token 获取到的新的token 无法进行刷新Token

今天爱分享给大家带来如何解决 Spring Security Oauth2中refresh_token刷新access_token异常,希望能够帮助到各位。

Spring Security Oauth2中,当access_token即将过期时,需要调用/oauth/token,使用refresh_token刷新access_token,此时会存在一个坑:

即如果使用Spring Security框架默认生成的AuthenticationManager时,接口调用异常。

具体报错信息为如下:

No AuthenticationProvider found for org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken

分析
为了解决此问题,翻阅了一些源码及资料,发现端倪在于tokenServices。

使用默认tokenServices
首先是AuthorizationServerEndpointsConfigurer中框架创建tokenServices的逻辑。

如果没有指定tokenServices,则创建默认的tokenServices,即DefaultTokenServices:

具体创建默认的tokenServices逻辑:


注意,默认创建的tokenServices并没有初始化authenticationManager,这里是重点,下面会用到。

其次,DefaultTokenServices中刷新access_token的逻辑。

关于user再次认证的部分逻辑如下:

如上所述,默认的tokenServices因为没有设置authenticationManager,所以不会走此逻辑,也就不会再次对用户进行认证。

tokenServices自定义
自定义tokenServices逻辑如下:

@Bean
public DefaultTokenServices defaultTokenServices() throws Exception {
    DefaultTokenServices defaultTokenServices = new DefaultTokenServices();
    defaultTokenServices.setAuthenticationManager(this.authenticationManager);
    defaultTokenServices.setTokenStore(jdbcTokenStore());
    defaultTokenServices.setClientDetailsService(jdbcClientDetailsServiceBuilder());
    
    // access token有效期2个小时
    defaultTokenServices.setAccessTokenValiditySeconds(60 * 60 * 2);
    // refresh token有效期30天
    defaultTokenServices.setRefreshTokenValiditySeconds(60 * 60 * 24 * 30);
    // 支持使用refresh token刷新access token
    defaultTokenServices.setSupportRefreshToken(true);
    // 允许重复使用refresh token
    defaultTokenServices.setReuseRefreshToken(true);
    return defaultTokenServices;
}

由于设置了自定义的authenticationManager,且此时身份认证模式为password,所以满足刷新access_token令牌时,针对用户进行的二次认证条件(文章开始处已说明),则会触发二次用户认证。

此时,若采用Spring Security框架默认的authenticationManager,则必然会爆出文章开头处描述的问题。因为框架默认的authenticationManager只包含一个DaoAuthenticationProvider。

Spring Security Oauth2框架中,存在一个开发者实现好的能认证PreAuthenticatedAuthenticationToken的Provider:PreAuthenticatedAuthenticationProvider。

但是,Spring Security框架并没有在默认authenticationManager中初始化,所以,DefaultTokenServices在执行刷新方法时,在二次认证的地方便会报错。

知道了问题的根本,就容易解决,添加PreAuthenticatedAuthenticationProvider到authenticationManager中应该就可以完美解决了。

关于如何authenticationManager配置,方法多种多样,如覆盖掉Spring Security框架默认逻辑,或者完全自定义,都可以。本文不再赘述,后续也会出文章,详细阐述Spring Security如何配置,结合Spring Security Oauth2如何配置等等。

下面附上PreAuthenticatedAuthenticationProvider定义逻辑:

 
private PreAuthenticatedAuthenticationProvider preAuthenticatedAuthenticationProvider() {
    PreAuthenticatedAuthenticationProvider preAuthenticatedAuthenticationProvider = new PreAuthenticatedAuthenticationProvider();
    preAuthenticatedAuthenticationProvider.setPreAuthenticatedUserDetailsService(new UserDetailsByNameServiceWrapper(userDetailsService));
    return preAuthenticatedAuthenticationProvider;
}

不得不说,Spring Security框架确实非常复杂,设计真是巧夺天工,不得不佩服设计者和开发者,由衷的赞叹!

人已赞赏
Java

Java Eclipse 开发工具配置详细步骤【图文并茂】

2020-10-16 17:35:02

Java

java.lang.IllegalStateException: Not on FX application thread解决办法

2020-10-16 18:45:00

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
'); })();