SpringBoot整合SpringSecurity实现手机号码登录
SpringBoot整合SpringSecurity实现⼿机号码登录
⼀. 概述
整合SpringSecurity实现⼿机号码登录, ⾸先要了解SpringSecurity默认的账号密码登录流程
1.1 SpringSecurity默认账号密码认证流程
SpringSecurity默认的账号密码验证主要由UsernamePasswordAuthenticationFilter完成的,可以
从UsernamePasswordAuthenticationFilter⼊⼿
参考UsernamePasswordAuthenticationFilter详解
1. 拦截登录请求:默认/login,可以通过配置HttpSecurity.loginProcessingUrl("/login")设定
2. 尝试认证:调⽤⽅法UsernamePasswordAuthenticationFilter.attemptAuthentication()
3. 查具体Provider认证处理:调⽤AuthenticationManager.authenticate(Authentication authentication)⽅法认证返回认证信息. AuthenticationManager管
理着Provider,会根据传参的Token类型到对应的Provider处理,UsernamePasswordAuthenticationToken对应
的Provider为DaoAuthenticationProvider
4. Session 策略处理: 认证没有错误就进⾏Session 策略处理,Authentication(authResult, request, response);
5. 认证成功:Context().setAuthentication(authResult)把认证结果放到当前线程,这⼀步就表⽰认证成功
6. 记住我策略处理:rememberMeServices.loginSuccess(request, response, authResult);
7. 认证失败处理: 调⽤SecurityContextHolder.clearContext();清除认证结果,调⽤rememberMeServices.loginFail(request, response);记住我逻辑处理,调
⽤AuthenticationFailure(request, response, failed);失败处理
1.2 实现⼿机号码登录思路
清楚账号密码认证流程后,实现⼿机号码登录思路就清晰了, 模仿⼀下账号密码认证流程就⾏
1. 编写短信验证码校验过滤器SmsCodeFilter
2. 模仿UsernamePasswordAuthenticationFilter重写⼀个⼿机号码认证过滤PhoneNumAuthenticationFilter手机密码
3. 模仿UsernamePasswordAuthenticationToken重写⼀个⼿机号码TOKEN PhoneNumAuthenticationToken
4. 模仿DaoAuthenticationProvider重写⼀个⼿机号码登录认证处理DaoPhoneNumAuthenticationProvider
⼆. SpringBootDemo

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。