配置数据源com.alibaba druid1.2.8 mysql mysql-connector-java5.1.38 org.mybatis.spring.boot mybatis-spring-boot-starter2.2.0
spring.thymeleaf.cache=false spring.datasource.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/security?characterEncoding=UTF-8 spring.datasource.username=root spring.datasource.password=root mybatis.mapper-locations=classpath:mapper/*.xml mybatis.type-aliases-package=com.entity配置持久化令牌
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
//...
@Autowired
private DataSource dataSource;
@Bean
public PersistentTokenRepository persistentTokenRepository(){
JdbcTokenRepositoryImpl jdbcTokenRepository = new JdbcTokenRepositoryImpl();
jdbcTokenRepository.setCreateTableOnStartup(false);//只需要没有表时设置为 true
jdbcTokenRepository.setDataSource(dataSource);
return jdbcTokenRepository;
}
//..
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.mvcMatchers("/login.html").permitAll()
.anyRequest().authenticated()
//...
.logout()
.and()
.rememberMe() //开启记住我功能
.tokenRepository(persistentTokenRepository())
.and()
.csrf().disable();
}
}
启动项目并查看数据库
注意:启动项目会自动创建一个表,用来保存记住我的 token 信息



