Spring Security Token持久化
在网站的登录页面中,记住我选项是一个很常见的功能,勾选记住我后在一段时间内,用户无需进行登录操作就可以访问系统资源。在Spring Security中添加记住我功能很简单,大致过程是:当用户勾选了记住我选项并登录成功后,Spring Security会生成一个token标识,然后将该token标识持久化到数据库,并且生成一个与该token相对应的cookie返回给浏览器。当用户过段时间再次访问系统时,如果该cookie没有过期,Spring Security便会根据cookie包含的信息从数据库中获取相应的token信息,然后帮用户自动完成登录操作。
在application.yml中添加数据源配置
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/security?useUnicode=yes&characterEncoding=UTF-8&useSSL=false
username: root
password: 123456
添加数据库依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
然后我们在BrowserSecurityConfig
中配置个token持久化对象
@Configuration
public class BrowserSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UserDetailService userDetailService;
@Autowired
private DataSource dataSource;
@Bean
public PersistentTokenRepository persistentTokenRepository() {
JdbcTokenRepositoryImpl jdbcTokenRepository = new JdbcTokenRepositoryImpl();
jdbcTokenRepository.setDataSource(dataSource);
jdbcTokenRepository.setCreateTableOnStartup(false);
return jdbcTokenRepository;
}
...
}
PersistentTokenRepository
为一个接口类,这里我们用的是数据库持久化,所以实例用的是PersistentTokenRepository
的实现类JdbcTokenRepositoryImpl
。
JdbcTokenRepositoryImpl
需要指定数据源,所以我们将配置好的数据源对象DataSource
注入进来并配置到JdbcTokenRepositoryImpl
的dataSource
属性中。createTableOnStartup
属性用于是否启动项目时创建保存token信息的数据表,这里设置为false,我们自己手动创建。
查看JdbcTokenRepositoryImpl
的源码,可以看到其包含了一个CREATE_TABLE_SQL
属性:
这个其实就是用于保存token对象数据表的SQL语句,我们复制出来手动执行创建表
CREATE TABLE persistent_logins (
username VARCHAR (64) NOT NULL,
series VARCHAR (64) PRIMARY KEY,
token VARCHAR (64) NOT NULL,
last_used TIMESTAMP NOT NULL
)
最后我们需要在Spring Security的认证流程中启用记住我的功能,在BrowserSecurityConfig
的configure
方法中开启记住我功能
@Override
protected void configure(HttpSecurity http) throws Exception {
http.addFilterBefore(validateCodeFilter, UsernamePasswordAuthenticationFilter.class) // 添加验证码校验过滤器
.formLogin() // 表单登录
// http.httpBasic() // HTTP Basic
.loginPage("/authentication/require") // 登录跳转 URL
.loginProcessingUrl("/login") // 处理表单登录 URL
.successHandler(authenticationSucessHandler) // 处理登录成功
.failureHandler(authenticationFailureHandler) // 处理登录失败
.and()
.rememberMe()
.tokenRepository(persistentTokenRepository()) // 配置 token 持久化仓库
.tokenValiditySeconds(3600) // remember 过期时间,单为秒
.userDetailsService(userDetailService) // 处理自动登录逻辑
.and()
.authorizeRequests() // 授权配置
.antMatchers("/authentication/require",
"/login.html",
"/image/code").permitAll() // 无需认证的请求路径
.anyRequest() // 所有请求
.authenticated() // 都需要认证
.and()
.csrf().disable();
}
|
rememberMe()
用于开启记住我功能;tokenRepository(persistentTokenRepository())
用于指定token持久化方法;tokenValiditySeconds
配置了token的有效时长,单为为秒;userDetailsService(userDetailService)
用于处理通过token对象自动登录