文章目录
概要
参考资料
整体架构流程
SmsAuthenticationProvider
ProviderManager中配置两个provider
小结
概要
Spring Security 是一个很常用的安全框架,当然老外写的框架很多时候还是不会适应咱们的国情,比如现在的登录,手机号加验证码才是主流,毕竟密码太多,谁都会忘。而其默认的认证方式还是username 加 password的方式:UsernamePasswordAuthenticationToken。本文讲述了如何使用解决手机号和验证码的方式完成认证。
参考资料
这里我参考几篇文章,不过大部分的文章还在用WebSecurityConfigurerAdapter ,这个已经不适用于最新的SpringSecurity了,还有就是一般会让你写token,provider,和filter三个文件。 SpringSecurity自定义实现手机短信登录 Spring Security学习(六)——配置多个Provider(存在两种认证规则)
整体架构流程
本文会相对来说比较简单,同时本文建立在第四节 springsecurity结合jwt实现前后端分离开发的基础上进行开发的。
创建一个SmsAuthenticationProvider
`ProviderManager``中配置两个provider
SmsAuthenticationProvider
首先我们参考DaoAuthenticationProvider写一个针对手机号验证码的认证提供者。我们需要继承AbstractUserDetailsAuthenticationProvider ,然后实现retrieveUser和additionalAuthenticationChecks,第一个方法其实基本不用动,核心就是在第二个方法里面对验证码进行判断
package com.sbvadmin.config;
import com.jayway.jsonpath.Criteria;
import com.sbvadmin.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.InternalAuthenticationServiceException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UserDetails;