使用springsecurity框架可以实现自动登录(或者说在一定的时间内免登陆): 步骤实现的流程
第二步代码
/**
* 配置springsecurity的web 自动登录
* @return
*/
@Bean
public PersistentTokenRepository persistentTokenRepository(){
JdbcTokenRepositoryImpl jdbcTokenRepository = new JdbcTokenRepositoryImpl();
jdbcTokenRepository.setDataSource(dataSource);
//自动在数据库创建表
//jdbcTokenRepository.setCreateTableOnStartup(true);
return jdbcTokenRepository;
}
第三部代码:
//自定义自己编写的登录页面
@Override
protected void configure(HttpSecurity http) throws Exception {
//配置没有权限访问跳转自定义页面
http.exceptionHandling().accessDeniedPage("/unauth.html");
http.logout().logoutUrl("/logout").logoutSuccessUrl("/test/hello").permitAll();
http.formLogin()
.loginPage("/login.html")//登录页面设置
.loginProcessingUrl("/user/login") //登录访问路径
.defaultSuccessUrl("/success.html").permitAll() //登录成功之后, 跳转的路径
.and().authorizeRequests()
.antMatchers("/","/test/hello","/user/login").permitAll() //设置哪些路径可以直接访问不需要认证
//当前登录用户,只有具有admin权限才可以访问这个路径
//.antMatchers("/test/index").hasAnyAuthority("admin")
.anyRequest().authenticated()
.and().rememberMe().tokenRepository(persistentTokenRepository())
.tokenValiditySeconds(60) //设置时间 ,设置自动登录有效时间为60秒
.userDetailsService(userDetailsService)
.and().csrf().disable(); //关闭csrf防护
}
第五步的代码 注意名字复选框的名字是固定的
<input type="checkbox"name="remember-me"title="记住密码"/><br/>