Spring Security(八) Remember Me 功能实现

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

1 添加依赖

Spring Security 实 现 Remember Me 功 能 时 底 层 实 现 依 赖Spring-JDBC,所以需要导入 Spring-JDBC。以后多使用 MyBatis 框架而很少直接导入 spring-jdbc,所以此处导入 mybatis 启动器同时还需要添加 MySQL 驱动

<dependency>
	<groupId>org.mybatis.spring.boot</ groupId>
	<artifactId>mybatis-spring-boot-starter</ artifactId>
	<version>2.1.0</ version>
</dependency>
<dependency>
	<groupId>mysql</ groupId>
	<artifactId>mysql-connector-java</ artifact Id>
	<version>5.1.47</ version>
</dependency>

2 配置数据源

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

spring.datasource.driver-class-name= com.mysql.jdbc.Driver
spring.datasource.url= jdbc:mysql://127.0.0.1:3306/security
spring.datasource.username= root
spring.datasource.password= root

3 编写配置

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

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

4 修改 SecurityConfig

在SecurityConfig中添加RememberMeConfig和UserDetailsService实现类对象,并自动注入。

在 configure 中添加下面配置内容。

http.rememberMe()
.userDetailsService( userDetailsService) //登录逻辑交给哪个对象
.tokenRepository( repository); //持久层对象

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

在客户端登录页面中添加 remember-me 的复选框,只要用户勾选了复选框下次就不需要进行登录了。

<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 有效时间

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

//remember Me
http.rememberMe()
.tokenValiditySeconds(120) // 单位:秒
.tokenRepository( repository)
.userDetailsService( userDetailsServiceImpl);

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

plenilune-望月

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值