[权限管理系统(四)]-spring boot +spring security短信认证+redis整合

本文详细介绍了如何在权限管理系统中结合Spring Boot、Spring Security实现短信认证登录,并利用Redis进行缓存。内容包括自定义Filter、Authentication、AuthenticationProvider、UserDetailsService以及SecurityConfig的配置,同时讲解了Redis的使用。通过这个教程,开发者可以快速理解和应用短信验证登录方式。
摘要由CSDN通过智能技术生成

[权限管理系统]spring boot +spring security短信认证+redis整合

现在主流的登录方式主要有 3 种:账号密码登录、短信验证码登录和第三方授权登录,前面一节Spring security(三)---认证过程已分析了spring security账号密码方式登陆,现在我们来分析一下spring security短信方式认证登陆。

Spring security 短信方式、IP验证等类似模式登录方式验证,可以根据账号密码方式登录步骤仿写出来,其主要以以下步骤进行展开:

  1. 自定义Filter:

  2. 自定义Authentication

  3. 自定义AuthenticationProvider

  4. 自定义UserDetailsService

  5. SecurityConfig配置

1. 自定义filter:

自定义filter可以根据UsernamePasswordAuthenticationFilter过滤器进行仿写,其实质即实现AbstractAuthenticationProcessingFilter抽象类,主要流程分为:

  1. 构建构造器,并在构造器中进行配置请求路径以及请求方式的过滤

  2. 自定义attemptAuthentication()认证步骤

  3. 在2步骤中认证过程中需要AuthenticationProvider进行最终的认证,在认证filter都需要将AuthenticationProvider设置进filter中,而管理AuthenticationProvider的是AuthenticationManager,因此我们创建过滤器filter的时候需要设置AuthenticationManager,这步具体详情在5.1 SecurityConfig配置步骤

在第2步中attemptAuthentication()认证方法主要进行以下步骤:

  1).post请求认证;

  2).request请求获取手机号码和验证码;

  3).用自定义的Authentication对象封装手机号码和验证码;

  4).使用AuthenticationManager.authenticate()方法进行验证。

自定义filter实现代码:

public class SmsAuthenticationfilter extends AbstractAuthenticationProcessingFilter {
    private boolean postOnly = true;
​
    public SmsAuthenticationfilter() {
      super(new AntPathRequestMatcher(SecurityConstants.APP_MOBILE_LOGIN_URL, "POST"));
   }
    @Override
    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException, IOException, ServletException {
        if (postOnly && !request.getMethod().equals("POST")) {
              throw new AuthenticationServiceException(
                   "Authentication method not supported: " + request.getMethod());
      }
        Assert.hasText(SecurityConstants.MOBILE_NUMBER_PARAMETER, "mobile parameter must not be empty or null");
     
         String mobile = request.getParameter(SecurityConstants.MOBILE_NUMBER_PARAMETER);
     
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值