springSecurity+oauth2:使用refresh_token获取AccessToken

刚开始研究的时候我在好奇,为什么不直接/oauth/token来获取token,而要使用refresh_token来获取呢?

因为/oauth/token需要code参数(授权码模式),但是code是一次性的参数,无法获取,所以就需要refresh_token来获取AccessToken了。

使用refresh_token需要配置userDetailsService

/**自定义UserDetailsService **/

@Component
public class AuthUserDetailsService implements UserDetailsService {

    @Autowired
    private UserInfoMapper userInfoMapper;
    
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        UserInfo userInfo = userInfoMapper.findByUserName(username);
        List<GrantedAuthority> grantedAuths = new ArrayList<>();
        grantedAuths.add(new SimpleGrantedAuthority("ROLE_USER"));
        return new User(username, userInfo.getUserPwd(), grantedAuths);
    }

}

/**

 * 认证服务器配置
 */
@Configuration
@EnableAuthorizationServer
public class AuthServerConfig extends AuthorizationServerConfigurerAdapter{
    @Autowired
    private AuthenticationManager authenticationManager;
    @Autowired
    private UserDetailsService userDetailsS​​ervice;
    ……

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        endpoints.authenticationManager(authenticationManager).userDetailsService(userDetailsS​​ervice);
    }
    ……
}

另外,再包装一下token失效后的返回错误信息

@Configuration

@EnableResourceServer
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
    ……
    @Override
    public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
        super.configure(resources);
        resources.authenticationEntryPoint(new AuthEntryPoint());
    }
}

/**重写这个commence方法**/
public class AuthEntryPoint extends OAuth2AuthenticationEntryPoint {
    
    private static final Logger LOG = LoggerFactory.getLogger(AuthEntryPoint.class);
    
    private WebResponseExceptionTranslator exceptionTranslator = new DefaultWebResponseExceptionTranslator();
    
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        try {
            ResponseEntity<?> result = exceptionTranslator.translate(authException);
            if (result.getStatusCode() == HttpStatus.UNAUTHORIZED) {
                response.setCharacterEncoding("UTF-8");  
                response.setContentType("application/json; charset=utf-8");
                PrintWriter writer = response.getWriter();
                writer.append(HttpStatus.UNAUTHORIZED.toString());
            } else {
                super.commence(request,response,authException);
            }
        } catch (Exception e) {
            LOG.error("failed to commence: {}", e.getMessage(), e);
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值