oauth2官方只有4种授权方式,不过springsecurity oauth2把refresh token也归为authorizedGrantTypes的一种,因此
springsecurity的oauth2实现有5中模式。
authorization_code | 权码模式(即先登录获取code,再获取token)
|
implicit | 简化模式(在redirect_uri 的Hash传递token; Auth客户端运行在浏览器中,如JS,Flash) |
password | 密码模式(将用户名,密码传过去,直接获取token)
|
client_credentials | 客户端模式(无用户,用户向客户端注册,然后客户端以自己的名义向'服务端'获取资源) |
refresh_token | 刷新token |
要使用refresh_token的话,需要在认证服务器中配置userDetailsService。
@Autowired
private CustomUserDetailsService customUserDetailsService;
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.tokenStore(tokenStore)
.authorizationCodeServices(authorizationCodeServices)
.userDetailsService(customUserDetailsService)
.userApprovalHandler(userApprovalHandler())
.authenticationManager(authenticationManager);
}
如果不配置,或者配置错误(比如在SecurityConfig中配置了一个userDetailsService,而在认证服务器中配置了另一个userDetailsService),会报userDetailsService缺失错误或空指针异常。
需要注意的另一个点:refresh_token必须在过期之前调用才能换新的token。
刷新token时,如果access_token,refresh_token均未过期,access_token会是一个新的token,而且过期时间expires延长,refresh_token根据设定的过期时间,没有失效则不发生变化。
刷新token时,如果access_token过期,refresh_token未过期,access_token会是一个新的token,而且过期时间expires延长,refresh_token根据设定的过期时间,没有失效则不发生变化。
刷新token时,如果refresh_token过期,会返回401状态码,{"error":"invalid_token","error_description":"Invalid refresh token (expired): 7f0cf53a-8b89-4a1f-b5ae-09b7e48dc888"}