記住我是登入常用功能 即登入一次過一段時間免登入
SpringSecurityConfig
dataSource,jdbcTokenRepository,configure後三行為記住我配置 同時提供了一個persistent_logins表作為記住我功能的記錄(會自動生成)
@EnableWebSecurity
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
DataSource dataSource;
/**
* 記住我功能
*
* @return
*/
@Bean
public JdbcTokenRepositoryImpl jdbcTokenRepository() {
JdbcTokenRepositoryImpl jdbcTokenRepository = new JdbcTokenRepositoryImpl();
jdbcTokenRepository.setDataSource(dataSource);
return jdbcTokenRepository;
}
@Override
protected void configure(HttpSecurity http) throws Exception {
// 驗證碼過濾器
http.addFilterBefore(imageCodeValidateFilter, UsernamePasswordAuthenticationFilter.class)
// 跳轉前台的位址
.formLogin().loginPage("/loginPage")
// 登入調用的接口位址
.loginProcessingUrl("/login").successHandler(customAuthenticationSuccessHandler)
// 無權限允許通路
.and().authorizeRequests()
.antMatchers("/login", "/loginPage", "/code/image", "/logout/expirePage", "/logout/page",
"/oauth/logoutSession", "/oauth/customLogout")
.permitAll().anyRequest().authenticated()
// 記住功能配置
.and().rememberMe()
.tokenRepository(jdbcTokenRepository()) // 儲存登入資訊
.tokenValiditySeconds(60 * 30 * 30); // 記住我有效時長
}
}