【Spring Security OAuth2】- spring security - 短信登录开发

作者简介:大家好,我是smart哥,前中兴通讯、美团架构师,现某互联网公司CTO

联系qq:184480602,加我进群,大家一起学习,一起进步,一起对抗互联网寒冬

学习必须往深处挖,挖的越深,基础越扎实! 

短信登录开发

202306181902569641.png

上图,可见要自己扩展的明白原理流程;

短信登录和 用户名密码登录的逻辑不同(现在也不知道为什么不同,跟着走吧),不能和之前的写在一起

这里模仿它的原理进行另外一条线,加入短信登录的认证(注意不是验证);不是验证发送的短信验证码;

之前写的图形验证码是在 UsernamePasswordAuthenticationFilter前增加了我们自己的图形验证过滤器,
验证成功之后再交给用户名和密码进行认证,调用userDetailsService进行匹配验证;

最后通过的话,会进入Authentication已认证流程;

短信认证的思路和上面一样:

  1. SmsCodeAuthenticationFilter 短信登录请求

  2. SmsCodeAuthenticationProvider 提供短信登录处理的实现类

  3. SmsCodeAuthenticationToken 存放认证信息(包括未认证前的参数信息传递)

  4. 最后开发一个过滤器放在 短信登录请求之前,进行短信验证码的验证,

    因为这个过滤器只关心提交的验证码是否正常就行了。所以可以应用到任意业务中,对任意业务提交进行短信的验证

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值