Spring Security Note-7
实现短信验证码登录
开发短信验证码接口
生成验证码接口
1 |
|
图片验证码 & 短信验证码实体改造
1 | public class ValidateCode { |
继承关系ImageCode
1 | public class ImageCode extends ValidateCode { |
验证码生成接口改造
1 | public interface ValidateCodeGenerator { |
Bean配置类装配
1 |
|
短信验证码模拟实现(默认)
1 | public class DefaultSmsCodeSender implements SmsCodeSender { |
短信验证码生成器
1 | "smsCodeGenerator") ( |
参数配置实体改造
1 | public class SmsCodeProperties { |
配置添加
1 | public class ValidateCodeProperties { |
改造重构
1 | public interface ValidateCodeProcessor { |
1 | public abstract class AbstractValidateCodeProcessor<C extends ValidateCode> implements ValidateCodeProcessor { |
1 |
|
校验短信验证码并登录
SmsAuthenticationFilter
接受请求生成SmsAuthenticationToken
;
然后交给系统的AuthenticationManager
进行管理,然后找到SmsAuthenticationProvider
;
然后再调用UserDetailsService
进行短信验证;
验证码的验证是在请求之前进行验证码过滤验证的;
SmsCodeAuthenticationToken 模仿 UsernamePasswordAuthenticationToken
1 | public class SmsCodeAuthenticationToken extends AbstractAuthenticationToken { |
SmsCodeAuthenticationFilter 模仿 UsernamePasswordAuthenticationFilter
1 | /** |
1 | public class SmsCodeAuthenticationProvider implements AuthenticationProvider { |
1 | public class SmsCodeFilter extends OncePerRequestFilter implements InitializingBean { |
配置
1 | public class SmsCodeAuthenticationSecurityConfig extends SecurityConfigurerAdapter<DefaultSecurityFilterChain,HttpSecurity>{ |
安全配置类
1 |
|
重构
重构代码过多,不予展示,详见GITHUB;