史上最简单的Spring Security教程(三十三):用户名密码登录新增验证码和RememberMe(记住我)选项

前面我们讲了如何基于 Spring Security 框架默认的用户名密码登录方式,为其添加 RememberMe(记住我)选项。另外,也讲过如何基于 Spring Security 框架默认的用户名密码登录方式,添加 验证码 选项。本文即同样基于Spring Security 框架默认的用户名密码登录方式,同时添加 RememberMe(记住我)、验证码 选项。

首先,改造登录页,新增 记住我、验证码 选项。

<form th:action="@{/login}" method="post" th:method="post" class="mt-1">
  <div class="form-group">
    <input type="text" class="form-control" name="username" placeholder="用户名">
  </div>
  <div class="form-group">
    <input type="password" class="form-control" name="password" placeholder="密码">
  </div>
  <div class="form-row">
    <div class="form-group col-md-10">
      <input type="text" class="form-control" name="captcha" placeholder="验证码">
    </div>
    <div class="form-group col-md-2">
      <a href="####" id="captcha_link">
        <img id="captcha_img" src="../static/img/captcha.jpg" th:src="@{/captcha/generate}" /></a>
    </div>
  </div>
  <div class="checkbox">
    <label><input type="checkbox" name="remember-me"> 记住我</label>
  </div>
  <button type="submit" class="btn btn-primary btn-block mb-1 mt-1">登录</button>
  <p class="text-muted text-center"> <a href="login.html#">
    <small>忘记密码了?</small></a> | <a href="#">注册一个新账号</a>
  </p>
</form>

然后,修改 Spring Security 配置,添加自定义的 UsernamePasswordAuthenticationFilter 及 RememberMe 相关配置。

RememberMe 相关配置如下。

protected void configure(HttpSecurity http) throws Exception {
    http
        ......
        .rememberMe()
        .userDetailsService(userDetailsService())
        .tokenValiditySeconds(14 * 24 * 60 * 60)
        ......
​
}

token默认有效期为两周,即14天,与 Spring Security 默认配置相同。

添加自定义的 UsernamePasswordAuthenticationFilter 的相关配置如下。

protected void configure(HttpSecurity http) throws Exception {
    ......
​
        http.addFilterAt(usernamePasswordAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class);
​
}
​
private UsernamePasswordCaptchaAuthenticationFilter usernamePasswordAuthenticationFilter() throws Exception {
    UsernamePasswordCaptchaAuthenticationFilter authenticationFilter = new UsernamePasswordCaptchaAuthenticationFilter();
    authenticationFilter.setRememberMeServices(tokenBasedRememberMeServices());
    authenticationFilter.setAuthenticationSuccessHandler(authenticationSuccessHandler());
    authenticationFilter.setAuthenticationFailureHandler(authenticationFailureHandler());
    authenticationFilter.setAuthenticationManager(authenticationManager());
    return authenticationFilter;
}
  
......

好了,准备工作基本就绪,启动系统,访问首页,跳转到登录页。

 

输入用户名、密码、验证码,勾选 记住我 选项,就可以登录了。身份认证成功后,便会跳转到首页。

注意, 一定要记得勾选 记住我 选项哦!

关闭浏览器,直接访问系统首页,此时,仍然可以直接访问,并没有重定向到登录页,无需输入用户名、密码、验证码。相同的,重启应用之后,同样可以直接访问系统首页,也无需再次登录。

用户名密码登录新增验证码和RememberMe(记住我)选项改造完成。

其它详细源码,请参考文末源码链接,可自行下载后阅读。

我是银河架构师,十年饮冰,难凉热血,愿历尽千帆,归来仍是少年! 

如果文章对您有帮助,请举起您的小手,轻轻【三连】,这将是笔者持续创作的动力源泉。当然,如果文章有错误,或者您有任何的意见或建议,请留言。感谢您的阅读!

源码

github

https://github.com/liuminglei/SpringSecurityLearning/tree/master/33

gitee

https://gitee.com/xbd521/SpringSecurityLearning/tree/master/33

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值