作者简介:大家好,我是smart哥,前中兴通讯、美团架构师,现某互联网公司CTO
联系qq:184480602,加我进群,大家一起学习,一起进步,一起对抗互联网寒冬
学习必须往深处挖,挖的越深,基础越扎实!
短信登录开发

上图,可见要自己扩展的明白原理流程;
短信登录和 用户名密码登录的逻辑不同(现在也不知道为什么不同,跟着走吧),不能和之前的写在一起
这里模仿它的原理进行另外一条线,加入短信登录的认证(注意不是验证);不是验证发送的短信验证码;
之前写的图形验证码是在 UsernamePasswordAuthenticationFilter前增加了我们自己的图形验证过滤器,
验证成功之后再交给用户名和密码进行认证,调用userDetailsService进行匹配验证;
最后通过的话,会进入Authentication已认证流程;
短信认证的思路和上面一样:
-
SmsCodeAuthenticationFilter 短信登录请求
-
SmsCodeAuthenticationProvider 提供短信登录处理的实现类
-
SmsCodeAuthenticationToken 存放认证信息(包括未认证前的参数信息传递)
-
最后开发一个过滤器放在 短信登录请求之前,进行短信验证码的验证,
因为这个过滤器只关心提交的验证码是否正常就行了。所以可以应用到任意业务中,对任意业务提交进行短信的验证
SmsCodeAuthenticationToken
package cn.mrcode.imooc.springsecurity.securitycore.authentication.mobile;
import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
import java.util.Collection;
/**
* 编写思路:直接复制参考 UsernamePasswordAuthenticationToken 的写法
* 分析哪些需要哪些是不需要的。包括功能
* @author zhuqiang
* @version 1.0.1 2023/8/4 17:54
* @date 2023/8/4 17:54
*/
public class SmsCodeAuthenticationToken extends AbstractAuthenticationToken {
// ~ Instance fields
// ================================================================================================
/** 存放用户名 : credentials 字段去掉,因为短信认证在授权认证前已经过滤了 */
private final Object principal;
// ~ Constructors
// ===================================================================================================
/**
* This constructor can be safely used by any code that wishes to create a
* <code>UsernamePasswordAuthenticationToken</code>, as the {@link #isAuthenticated()}
* will return <code>false</code>.
*/
public SmsCodeAuthenticationToken(String mobile) {
super(null);
this.principal = mobile;
setAuthenticated(false);
}
/**
* This constructor should only be used by <code>AuthenticationManager</code> or
* <code>AuthenticationProvider</code> implementations that are satisfied with
* producing a trusted (i.e. {@link #isAuthenticated()} = <code>true</code>)
* authentication token.
* @param principal
* @param authorities
*/
public SmsCodeAuthenticationToken(Object principal,
Collection<? extends GrantedAuthority> authorities) {
super(authorities);
this.principal = principal;
super.setAuthenticated(true); // must use super, as we override
}
// ~ Methods
// ========================================================================================================
@Override
public Object getPrincipal() {
return this.principal;
}
@Override
public Object getCredentials() {
return null;
}
@Override
public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException {
if (isAuthenticated) {
throw new IllegalArgume

最低0.47元/天 解锁文章
639

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



