<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
spring:
# 数据源配置
datasource:
username: root
password: root
url: jdbc:mysql://192.168.121.200:3306/study-security?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8
#mysql8版本以上驱动包指定新的驱动类
driver-class-name: com.mysql.cj.jdbc.Driver
// 记住我功能
@Autowired
DataSource dataSource;
@Bean
public JdbcTokenRepositoryImpl jdbcTokenRepository() {
JdbcTokenRepositoryImpl jdbcTokenRepository = new JdbcTokenRepositoryImpl();
jdbcTokenRepository.setDataSource(dataSource);
// 是否启动时自动创建表,第一次启动创建就行,后面启动把这个注释掉,不然报错已存在表
//jdbcTokenRepository.setCreateTableOnStartup(true);
return jdbcTokenRepository;
}
@Override
protected void configure(HttpSecurity http) throws Exception {
System.out.println("用户认证");
http.httpBasic() // 采用 httpBasic认证方式
http.addFilterBefore(imageCodeValidateFilter, UsernamePasswordAuthenticationFilter.class)
.formLogin() // 表单登录方式
.loginPage(authenticationProperties.getLoginPage()) //配置登录页面
.loginProcessingUrl(authenticationProperties.getLoginProcessingUrl()) //登录表单提交处理url,默认是/login
.usernameParameter(authenticationProperties.getUsernameParameter())
.passwordParameter(authenticationProperties.getPasswordParameter())
.successHandler(customAuthenticationSuccessHandler) // 认证成功处理器
.failureHandler(customAuthenticationFailureHandler) // 认证失败处理器
.and()
.authorizeRequests() // 认证请求
.antMatchers(authenticationProperties.getLoginPage(), "/code/image").permitAll()
.anyRequest().authenticated() //所有访问该应用的http请求都要通过身份认证才可以访问
.and()
.rememberMe() //记住我
.tokenRepository(jdbcTokenRepository()) //报错登录信息
.tokenValiditySeconds(60*60*24*7) //记住我有效时长(秒)
; // 注意不要少了分号
}
<input type="checkbox" name="remember-me" > 记住我
原文:https://www.cnblogs.com/xl4ng/p/13942810.html