이상 access_token은 refresh_token도 새로 고침에 봄 보안 OAuth2를 : 구덩이 가이드 (A)를 피

문제

access_token은이 만료 될 때 봄 보안으로 OAuth2는, 당신이 호출 할 필요가 / OAuth를 / 토큰 사용 refresh_token도 새로 고침 access_token은,이 시간이 구덩이가있을 것입니다 :

즉, 봄 보안 프레임 워크의 기본의 AuthenticationManager를 사용하는 경우 생성 된 인터페이스에 이상이 호출이다.

다음과 같이 구체적인 정보가 제공됩니다 :

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

분석

이 문제를 해결하려면, 소스 코드와 데이터 tokenServices이 발견 단서의 일부를 읽어 보시기 바랍니다.

기본 tokenServices를 사용하여

첫 번째는 논리 tokenServices을 만드는 프레임 워크 AuthorizationServerEndpointsConfigurer이다.

tokenServices를 지정하지 않을 경우, 기본 tokenServices, 즉 DefaultTokenServices을 만듭니다

특정는 기본 tokenServices 논리를 만들 :

기본 생성 tokenServices, 여기의 AuthenticationManager를 초기화하지 않았 음을 참고 열쇠입니다, 다음이 사용됩니다.

둘째, DefaultTokenServices 새로 고침 논리를 access_token은.

다음과 같이 논리의 일부가 다시 사용자 인증은 다음과 같습니다

디폴트 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에 사용자 정의의 제공,이 시간 인증 모드는 암호입니다 그래서 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框架确实非常复杂,设计真是巧夺天工,不得不佩服设计者和开发者,由衷的赞叹!

 

正文完!

 

本文系【银河架构师】原创,如需转载请在文章明显处注明作者及出处。

微信搜索【银河架构师】,发现更多精彩内容。

 

发布了29 篇原创文章 · 获赞 1 · 访问量 2217

추천

출처blog.csdn.net/liuminglei1987/article/details/103763106