SpringCloud/Boot + Oauth2 集成短信登陆方案
登录框架有很多,Oauth2算是属于比较常用的一个框架了,诸如腾讯,阿里,字节跳动等产品登录都是使用Oauth2的。那么Oauth2怎么集成短信登陆和第三方登录呢?
最终我参考了这个作者的文章,把短信登录集成做好了,这个方案是属于非侵入式的解决方案,实现起来也相对来说简单。
1.实现思路:

首先需要做一个Filter,用来拦截/oauth/token的请求,并增加auth_type用来区分登录方式。
public class IntegrationAuthenticationFilter extends GenericFilterBean implements ApplicationContextAware {
private static final String AUTH_TYPE_PARM_NAME = "auth_type";
private static final String OAUTH_TOKEN_URL = "/oauth/token";
private Collection<IntegrationAuthenticator> authenticators;
private ApplicationContext applicationContext;
private RequestMatcher requestMatcher;
public IntegrationAuthenticationFilter() {
this.requestMatcher = new OrRequestMatcher(
new AntPathRequestMatcher(OAUTH_TOKEN_URL, "GET"),
new AntPathRequestMatcher(OAUTH_TOKEN_URL, "POST")
);
}
@Bean
public FilterRegistrationBean registrationBean(IntegrationAuthenticationFilter integrationAuthenticationFilter){
FilterRegistrationBean registrationBean = new FilterRegistrationBean(integrationAuthenticationFilter);
registrationBean.setEnabled(false);
return registrationBean;
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
if (requestMatcher.matches(request)) {
//设置集成登录信息
IntegrationAuthentication integrationAuthenticat

本文介绍了如何将短信登录集成到基于SpringCloudBoot和Oauth2的系统中,提供了一种非侵入式的解决方案。主要步骤包括实现拦截器、自定义SmsAuthenticator以及修改AuthorizationServerConfiguration。测试表明,通过增加auth_type参数,可以成功实现短信验证登录,并解决不加auth_type时的安全问题。
最低0.47元/天 解锁文章
2256

被折叠的 条评论
为什么被折叠?



