SpringSecurity(十四)—RememberMe功能实现

SpringSecurity(十四)—RememberMe功能实现

一.使用理由

Spring Security 中Remember Me为“记住我”功能,用户只需要在登录时添加remember-me复选框,取值为true。Spring Security会自动把用户信息存储到数据源中,以后就可以不登录进行访问。

二.实现步骤

1.添加依赖(jdbc和mysql)

Spring Security实现Remember Me 功能时底层实现依赖Spring-JDBC,所以需要添加jdbc的依赖,如果使用的是mybatis框架,只要导入mybatis的启动器即可,同时还需添加mysql的驱动

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.1</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>

2.配置数据源

在application.properties或application.yml中配置数据源。请确保数据库中已经存在test数据库

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/test?useSSL=false
    username: root
    password: admin

3.编写配置

新建com.yrp.config.RememberMeConfig类,并创建Bean对象

@Configuration
public class RememberMeConfig {
   
    @Autowired
    private DataSource dataSource;
    @Bean
    public PersistentTokenRepository getPersistentTokenRepository(){
   
        JdbcTokenRepositoryImpl jdbcTokenRepository=new JdbcTokenRepositoryImpl();
        jdbcTokenRepository.setDataSource(dataSource);
        //自动建表,第一次启动时需要,第二次启动时注释掉
        //jdbcTokenRepository.setCreateTableOnStartup(true);
        return jdbcTokenRepository;

    }
}

4.修改SecurityConfig

在SecurityConfig中添加PersistentTokenRepository和UserDetailsService实现类对象,并自动注入。
在configure中添加下面配置内容。

  @Autowired
  private MyAuthImpl myAuth;
  @Autowired
  private PersistentTokenRepository getPersistentTokenRepository;
     //加入rememberMe的功能
        http.rememberMe()
                .userDetailsService(myAuth)
                .tokenRepository(getPersistentTokenRepository)
        

5.在客户端页面中添加复选框

<form action = "/login" method="post">
    用户名:<input type="text" name="username"/><br/>
    密码:<input type="text" name="password"/><br/>
 记住我:   <input type="checkbox" name="remember-me" value="true"/> <br/>
    <input type="submit" value="登录"/>
</form>

6.有效时间

默认2周时间。但是可以通过设置状态有效时间,即使项目重新启动下次也可以正常登录。

//remember Me
http.rememberMe()
        .tokenValiditySeconds(120)//单位:秒
        .tokenRepository(repository)
        .userDetailsService(userDetailsServiceImpl);
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/2350.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Odin Inspector 系列教程 — Indent Attribute「建议收藏」

    Odin Inspector 系列教程 — Indent Attribute「建议收藏」IndentAttribute特性:用于缩进可用于任何属性,并将属性的标签向右移动。使用它可以清楚地组织检查器中的属性。usingSirenix.OdinInspector;usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;publicclass…

    2022年7月21日
    20
  • idea2021激活码【最新永久激活】

    (idea2021激活码)好多小伙伴总是说激活码老是失效,太麻烦,关注/收藏全栈君太难教程,2021永久激活的方法等着你。IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.html1HV55HYOZP-eyJsaWNlbnNlSW…

    2022年3月28日
    1.2K
  • java web项目中hibernate导入问题解决, AbstractInterceptor

    java web项目中hibernate导入问题解决, AbstractInterceptor解决步骤如下:先说一句:修改后先保存,然后看看错误消失没?只需要导入正确的包properties-&gt;javabuildpath-&gt;Libraries-&gt;AddLibaray-&gt;Myeclipselibrary-&gt;你需要导入的包-&gt;applyimportcom.opensymphony.xwork2.Action;importcom.opensym…

    2022年5月14日
    40
  • SQL 游标的写法[通俗易懂]

    SQL 游标的写法[通俗易懂]DECLARE@Avarchar(200),@Bvarchar(200),@Cdatetime                  —-定义变量DECLAREcursorCURSORFOR                             –定义游标SELECTA,B,CFROMTableWHERE…

    2022年7月14日
    16
  • 其实就是为了能有字幕特效,用MeGUI + AVS压制PSP MP4AVC视频01 – 基础知识篇

    其实就是为了能有字幕特效,用MeGUI + AVS压制PSP MP4AVC视频01 – 基础知识篇A、需要准备的软件: .netframework2.0运行库(下面两个软件都是.net写的)MeGUI(AVS等软件的集合GUI)LiteMP4Tool(批处理AVS转换的GUI)通用解码器包,能够播放各类视频文件即可,包括K-LiteCodec、完美解码甚至暴风影音(这个不推荐)之类。avisynth2.58,这个我自己用的完美解码附…

    2022年10月16日
    2
  • 关于Pycharm Interpreter报错 Invalid Python SDK/Permission Denied问题解决方案「建议收藏」

    关于Pycharm Interpreter报错 Invalid Python SDK/Permission Denied问题解决方案「建议收藏」1.问题背景之前用了Pycharm好好的,结果运行个简单的test.py文件,里面的内容是:print(“helloworld”)居然报错,程序运行居然不是exitcode0报错结果如下:程序返回了一个异常参数,这个参数实际上是说:“你的python解释器未安装成功”。Processfinishedwithexitcode-1073741515(0xC0000135)网上百度了这个参数报错的意思,我就去找了我的解释器:【File】–>【Setting】,找到解释

    2022年8月26日
    16

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号