千家信息网

Spring Security 解析之短信登录开发的示例分析

发表于:2026-11-03 作者:千家信息网编辑
千家信息网最后更新 2026年11月03日,今天就跟大家聊聊有关Spring Security 解析之短信登录开发的示例分析,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。Spring S
千家信息网最后更新 2026年11月03日Spring Security 解析之短信登录开发的示例分析

今天就跟大家聊聊有关Spring Security 解析之短信登录开发的示例分析,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

Spring Security 解析 -- 短信登录开发

>   在学习Spring Cloud 时,遇到了授权服务oauth 相关内容时,总是一知半解,因此决定先把Spring Security 、Spring Security Oauth3 等权限、认证相关的内容、原理及设计学习并整理一遍。本系列文章就是在学习的过程中加强印象和理解所撰写的,如有侵权请告知。

> 项目环境: > - JDK1.8 > - Spring boot 2.x > - Spring Security 5.x

一、如何在Security的基础上实现短信登录功能?

  回顾下Security实现表单登录的过程:

  从流程中我们发现其在登录过程中存在特殊处理或者说拥有其他姊妹实现子类的 : > - AuthenticationFilter:用于拦截登录请求; > - 未认证的Authentication 对象,作为认证方法的入参; > - AuthenticationProvider 进行认证处理。

  因此我们可以完全通过自定义 一个 SmsAuthenticationFilter 进行拦截 ,一个 SmsAuthenticationToken 来进行传输认证数据,一个 SmsAuthenticationProvider 进行认证业务处理。由于我们知道 UsernamePasswordAuthenticationFilter 的 doFilter 是通过 AbstractAuthenticationProcessingFilter 来实现的,而 UsernamePasswordAuthenticationFilter 本身只实现了attemptAuthentication() 方法。按照这样的设计,我们的 SmsAuthenticationFilter 也 只实现 attemptAuthentication() 方法,那么如何进行验证码的验证呢?这时我们需要在 SmsAuthenticationFilter 前 调用 一个 实现验证码的验证过滤 filter :ValidateCodeFilter。整理实现过后的流程如下图:

二、短信登录认证开发

(一) SmsAuthenticationFilter 实现

  模拟UsernamePasswordAuthenticationFilter实现SmsAuthenticationFilter后其代码如下:

@EqualsAndHashCode(callSuper = true)@Datapublic class SmsCodeAuthenticationFilter extends AbstractAuthenticationProcessingFilter {    // 获取request中传递手机号的参数名    private String mobileParameter = SecurityConstants.DEFAULT_PARAMETER_NAME_MOBILE;    private boolean postOnly = true;    // 构造函数,主要配置其拦截器要拦截的请求地址url    public SmsCodeAuthenticationFilter() {        super(new AntPathRequestMatcher(SecurityConstants.DEFAULT_LOGIN_PROCESSING_URL_MOBILE, "POST"));    }    @Override    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException, IOException, ServletException {        // 判断请求是否为 POST 方式        if (postOnly && !request.getMethod().equals("POST")) {            throw new AuthenticationServiceException("Authentication method not supported: " + request.getMethod());        }        // 调用 obtainMobile 方法从request中获取手机号        String mobile = obtainMobile(request);        if (mobile == null) {            mobile = "";        }        mobile = mobile.trim();        // 创建 未认证的  SmsCodeAuthenticationToken  对象        SmsCodeAuthenticationToken authRequest = new SmsCodeAuthenticationToken(mobile);        setDetails(request, authRequest);                // 调用 认证方法        return this.getAuthenticationManager().authenticate(authRequest);    }    /**     * 获取手机号     */    protected String obtainMobile(HttpServletRequest request) {        return request.getParameter(mobileParameter);    }        /**     * 原封不动照搬UsernamePasswordAuthenticationFilter 的实现 (注意这里是 SmsCodeAuthenticationToken  )     */    protected void setDetails(HttpServletRequest request, SmsCodeAuthenticationToken authRequest) {        authRequest.setDetails(authenticationDetailsSource.buildDetails(request));    }    /**     * 开放设置 RemmemberMeServices 的set方法     */    @Override    public void setRememberMeServices(RememberMeServices rememberMeServices) {        super.setRememberMeServices(rememberMeServices);    }}

其内部实现主要有几个注意点: > - 设置传输手机号的参数属性 > - 构造方法调用父类的有参构造方法,主要用于设置其要拦截的url > - 照搬UsernamePasswordAuthenticationFilter 的 attemptAuthentication() 的实现 ,其内部需要改造有2点:1、 obtainMobile 获取 手机号信息 2、创建 SmsCodeAuthenticationToken 对象 > - 为了实现短信登录也拥有记住我的功能,这里开放 setRememberMeServices() 方法用于设置 rememberMeServices 。

(二) SmsAuthenticationToken 实现

  一样的我们模拟UsernamePasswordAuthenticationToken实现SmsAuthenticationToken:

public class SmsCodeAuthenticationToken extends AbstractAuthenticationToken {    private static final long serialVersionUID = SpringSecurityCoreVersion.SERIAL_VERSION_UID;    private final Object principal;    /**     * 未认证时,内容为手机号     * @param mobile     */    public SmsCodeAuthenticationToken(String mobile) {        super(null);        this.principal = mobile;        setAuthenticated(false);    }    /**     *     * 认证成功后,其中为用户信息     *     * @param principal     * @param authorities     */    public SmsCodeAuthenticationToken(Object principal,                                      Collection authorities) {        super(authorities);        this.principal = principal;        super.setAuthenticated(true);    }    @Override    public Object getCredentials() {        return null;    }    @Override    public Object getPrincipal() {        return this.principal;    }    @Override    public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException {        if (isAuthenticated) {            throw new IllegalArgumentException(                    "Cannot set this token to trusted - use constructor which takes a GrantedAuthority list instead");        }        super.setAuthenticated(false);    }    @Override    public void eraseCredentials() {        super.eraseCredentials();    }}

  对比UsernamePasswordAuthenticationToken,我们减少了 credentials(可以理解为密码),其他的基本上是原封不动。

(三) SmsAuthenticationProvider 实现

  由于SmsCodeAuthenticationProvider 是一个全新的不同的认证委托实现,因此这个我们按照自己的设想写,不必参照 DaoAuthenticationProvider。看下我们自己实现的代码:

@Datapublic class SmsCodeAuthenticationProvider implements AuthenticationProvider {    private UserDetailsService userDetailsService;    @Override    public Authentication authenticate(Authentication authentication) throws AuthenticationException {        SmsCodeAuthenticationToken authenticationToken = (SmsCodeAuthenticationToken) authentication;        UserDetails user = userDetailsService.loadUserByUsername((String) authenticationToken.getPrincipal());        if (user == null) {            throw new InternalAuthenticationServiceException("无法获取用户信息");        }        SmsCodeAuthenticationToken authenticationResult = new SmsCodeAuthenticationToken(user, user.getAuthorities());        authenticationResult.setDetails(authenticationToken.getDetails());        return authenticationResult;    }    @Override    public boolean supports(Class authentication) {        return SmsCodeAuthenticationToken.class.isAssignableFrom(authentication);    }}

  通过直接继承 AuthenticationProvider实现其接口方法 authenticate() 和 supports() 。 supports() 我们直接参照其他Provider写的,这个主要是判断当前处理的Authentication是否为SmsCodeAuthenticationToken或其子类。 authenticate() 我们就直接调用 userDetailsService的loadUserByUsername()方法简单实现,因为验证码已经在 ValidateCodeFilter 验证通过了,所以这里我们只要能通过手机号查询到用户信息那就直接判顶当前用户认证成功,并且生成 已认证 的 SmsCodeAuthenticationToken返回。

(四) ValidateCodeFilter 实现

   正如我们之前描述的一样ValidateCodeFilter只做验证码的验证,这里我们设置通过redis获取生成验证码来对比用户输入的验证码:

@Componentpublic class ValidateCodeFilter extends OncePerRequestFilter implements InitializingBean {    /**     * 验证码校验失败处理器     */    @Autowired    private AuthenticationFailureHandler authenticationFailureHandler;    /**     * 系统配置信息     */    @Autowired    private SecurityProperties securityProperties;    @Resource    private StringRedisTemplate stringRedisTemplate;    /**     * 存放所有需要校验验证码的url     */    private Map urlMap = new HashMap<>();    /**     * 验证请求url与配置的url是否匹配的工具类     */    private AntPathMatcher pathMatcher = new AntPathMatcher();    /**     * 初始化要拦截的url配置信息     */    @Override    public void afterPropertiesSet() throws ServletException {        super.afterPropertiesSet();        urlMap.put(SecurityConstants.DEFAULT_LOGIN_PROCESSING_URL_MOBILE, SecurityConstants.DEFAULT_PARAMETER_NAME_CODE_SMS);        addUrlToMap(securityProperties.getSms().getSendSmsUrl(), SecurityConstants.DEFAULT_PARAMETER_NAME_CODE_SMS);    }    /**     * 讲系统中配置的需要校验验证码的URL根据校验的类型放入map     *     * @param urlString     * @param smsParam     */    protected void addUrlToMap(String urlString, String smsParam) {        if (StringUtils.isNotBlank(urlString)) {            String[] urls = StringUtils.splitByWholeSeparatorPreserveAllTokens(urlString, ",");            for (String url : urls) {                urlMap.put(url, smsParam);            }        }    }    @Override    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)            throws ServletException, IOException {        String code = request.getParameter(getValidateCode(request));        if (code != null) {            try {                String oldCode = stringRedisTemplate.opsForValue().get(request.getParameter(SecurityConstants.DEFAULT_PARAMETER_NAME_MOBILE));                if (StringUtils.equalsIgnoreCase(oldCode,code)) {                    logger.info("验证码校验通过");                } else {                    throw new ValidateCodeException("验证码失效或错误!");                }            } catch (AuthenticationException e) {                authenticationFailureHandler.onAuthenticationFailure(request, response, e);                return;            }        }        chain.doFilter(request, response);    }    /**     * 获取校验码     *     * @param request     * @return     */    private String getValidateCode(HttpServletRequest request) {        String result = null;        if (!StringUtils.equalsIgnoreCase(request.getMethod(), "get")) {            Set urls = urlMap.keySet();            for (String url : urls) {                if (pathMatcher.match(url, request.getRequestURI())) {                    result = urlMap.get(url);                }            }        }        return result;    }}

这里主要看下 doFilterInternal 实现验证码验证逻辑即可。

三、如何将设置SMS的Filter加入到FilterChain生效呢?

这里我们需要引进新的配置类 SmsCodeAuthenticationSecurityConfig,其实现代码如下:

@Componentpublic class SmsCodeAuthenticationSecurityConfig extends SecurityConfigurerAdapter {    @Autowired    private AuthenticationSuccessHandler authenticationSuccessHandler ;    @Autowired    private AuthenticationFailureHandler authenticationFailureHandler;    @Resource    private UserDetailsService userDetailsService;    @Override    public void configure(HttpSecurity http) throws Exception {        SmsCodeAuthenticationFilter smsCodeAuthenticationFilter = new SmsCodeAuthenticationFilter();        // 设置 AuthenticationManager        smsCodeAuthenticationFilter.setAuthenticationManager(http.getSharedObject(AuthenticationManager.class));        // 分别设置成功和失败处理器        smsCodeAuthenticationFilter.setAuthenticationSuccessHandler(authenticationSuccessHandler);        smsCodeAuthenticationFilter.setAuthenticationFailureHandler(authenticationFailureHandler);        // 设置 RememberMeServices        smsCodeAuthenticationFilter.setRememberMeServices(http                .getSharedObject(RememberMeServices.class));        // 创建 SmsCodeAuthenticationProvider 并设置 userDetailsService        SmsCodeAuthenticationProvider smsCodeAuthenticationProvider = new SmsCodeAuthenticationProvider();        smsCodeAuthenticationProvider.setUserDetailsService(userDetailsService);        // 将Provider添加到其中        http.authenticationProvider(smsCodeAuthenticationProvider)                // 将过滤器添加到UsernamePasswordAuthenticationFilter后面                .addFilterAfter(smsCodeAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);    }

最后我们需要 在 SpringSecurityConfig 配置类中引用 SmsCodeAuthenticationSecurityConfig :

http.addFilterBefore(validateCodeFilter, AbstractPreAuthenticatedProcessingFilter.class)                .apply(smsCodeAuthenticationSecurityConfig)                . ...

四、新增发送验证码接口和验证码登录表单

   新增发送验证码接口(主要设置成无权限访问):

    @GetMapping("/send/sms/{mobile}")    public void sendSms(@PathVariable String mobile) {        // 随机生成 6 位的数字串        String code = RandomStringUtils.randomNumeric(6);        // 通过 stringRedisTemplate 缓存到redis中         stringRedisTemplate.opsForValue().set(mobile, code, 60 * 5, TimeUnit.SECONDS);        // 模拟发送短信验证码        log.info("向手机: " + mobile + " 发送短信验证码是: " + code);    }

   新增验证码登录表单:

// 注意这里的请求接口要与 SmsAuthenticationFilter的构造函数 设置的一致
手机号:
短信验证码: 发送验证码
记住我

五、个人总结

  其实实现另一种登录方式,关键点就在与 filter 、 AuthenticationToken、AuthenticationProvider 这3个点上。整理出来就是: 通过自定义 一个 SmsAuthenticationFilter 进行拦截 ,一个 AuthenticationToken 来进行传输认证数据,一个 AuthenticationProvider 进行认证业务处理。由于我们知道 UsernamePasswordAuthenticationFilter 的 doFilter 是通过 AbstractAuthenticationProcessingFilter 来实现的,而 UsernamePasswordAuthenticationFilter 本身只实现了attemptAuthentication() 方法。按照这样的设计,我们的 AuthenticationFilter 也 只实现 attemptAuthentication() 方法,但同时需要在 AuthenticationFilter 前 调用 一个 实现验证过滤 filter :ValidatFilter。 正如下面的流程图一样,可以按照这种方式添加任意一种登录方式:

   本文介绍短信登录开发的代码可以访问代码仓库中的 security 模块 ,项目的github 地址 : http

看完上述内容,你们对Spring Security 解析之短信登录开发的示例分析有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注行业资讯频道,感谢大家的支持。

验证 认证 登录 方法 短信 手机 手机号 处理 配置 信息 内容 开发 代码 用户 接口 方式 成功 对象 流程 表单 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 互联网科技园 科技互联网企业 厦门中拓互联网络科技有限公司 金融科技还是互联网 河南真二互联网科技有限公司 青岛互联网科技公司 广西互联网科技公司 互联网科技公司有哪些 互联网前沿科技 镁科技互联网资讯平台 中国互联网有什么科技含量 承德科技产业互联网发展 顶级互联网科技成果有哪些 花神(南京)互联网科技有限公司 国大互联网教育科技有限公司工作 青岛互联网科技园 互联网大会出现的黑科技 互联网高科技绘画 恒生科技互联网指数十大成分股 山东亿川互联网科技有限责任公司 上海挚品互联网科技有限公司 疫情对互联网科技公司影响 新浪科技互联网 互联网科技视频素材 滨湖区互联网智能科技产品供应 中化能源互联网科技招聘 如何创新互联网科技 山东有哪些互联网科技公司 信承诺互联网科技有限公司 互联网科技创新创业项目
0