(九)、SpringBoot + Security RememberMe(记住我)功能
2018-03-19 10:39
621 查看
可以前往第一篇博客查看目录结构 --> 这里
一、修改application.properties文件,添加数据库配置spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/zeke-demo
spring.datasource.username=root
spring.datasource.password=
二、修改zeke-login.html,添加记住我勾选框 <tr>
<td colspan="2"><input type="checkbox" name="remember-me" value="true">记住我</td>
</tr>
三、在BrowserProperties类中添加 rememberMeSeconds属性和对应的getter、setter方法 private int rememberMeSeconds = 3600;
四、修改BrowserSecurityConfig,注入一个Bean @Autowired
private DataSource dataSource;
/**
* 记住我功能
* @return
*/
@Bean
public PersistentTokenRepository persistentTokenRepository(){
JdbcTokenRepositoryImpl jdbcTokenRepository = new JdbcTokenRepositoryImpl();
jdbcTokenRepository.setDataSource(dataSource);
//自动创建数据库表,使用一次后注释掉,不然会报错
// jdbcTokenRepository.setCreateTableOnStartup(true);
return jdbcTokenRepository;
}
五、修改configure(HttpSecurity http)方法 @Override
protected void configure(HttpSecurity http) throws Exception {
ValidateCodeFilter validateCodeFilter = new ValidateCodeFilter();
validateCodeFilter.setAuthenticationFailureHandler(zekeAuthenticationFailureHandler);
validateCodeFilter.setSecurityProperties(securityProperties);
validateCodeFilter.afterPropertiesSet();
http.csrf().disable();
http.addFilterBefore(validateCodeFilter, UsernamePasswordAuthenticationFilter.class);
http.formLogin() //表单登录
.loginPage("/authentication/require") //用户未登录时的处理地址
.loginProcessingUrl("/authentication/form") //用户登录
.successHandler(zekeAuthenticationSuccessHandler) //登录成功处理
.failureHandler(zekeAuthenticationFailureHandler) //登录失败处理
.and()
.rememberMe()
.tokenRepository(persistentTokenRepository())
.tokenValiditySeconds(securityProperties.getBrowser().getRememberMeSeconds())
.userDetailsService(userDetailsService)
.and()
.authorizeRequests()
.antMatchers("/authentication/require",
securityProperties.getBrowser().getLoginPage(),
"/code/image") //不拦截的URL
.permitAll()
.anyRequest()
.authenticated();
}
六、启动服务,访问localhost/zeke-login.html,勾选记住我测试
一、修改application.properties文件,添加数据库配置spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/zeke-demo
spring.datasource.username=root
spring.datasource.password=
二、修改zeke-login.html,添加记住我勾选框 <tr>
<td colspan="2"><input type="checkbox" name="remember-me" value="true">记住我</td>
</tr>
三、在BrowserProperties类中添加 rememberMeSeconds属性和对应的getter、setter方法 private int rememberMeSeconds = 3600;
四、修改BrowserSecurityConfig,注入一个Bean @Autowired
private DataSource dataSource;
/**
* 记住我功能
* @return
*/
@Bean
public PersistentTokenRepository persistentTokenRepository(){
JdbcTokenRepositoryImpl jdbcTokenRepository = new JdbcTokenRepositoryImpl();
jdbcTokenRepository.setDataSource(dataSource);
//自动创建数据库表,使用一次后注释掉,不然会报错
// jdbcTokenRepository.setCreateTableOnStartup(true);
return jdbcTokenRepository;
}
五、修改configure(HttpSecurity http)方法 @Override
protected void configure(HttpSecurity http) throws Exception {
ValidateCodeFilter validateCodeFilter = new ValidateCodeFilter();
validateCodeFilter.setAuthenticationFailureHandler(zekeAuthenticationFailureHandler);
validateCodeFilter.setSecurityProperties(securityProperties);
validateCodeFilter.afterPropertiesSet();
http.csrf().disable();
http.addFilterBefore(validateCodeFilter, UsernamePasswordAuthenticationFilter.class);
http.formLogin() //表单登录
.loginPage("/authentication/require") //用户未登录时的处理地址
.loginProcessingUrl("/authentication/form") //用户登录
.successHandler(zekeAuthenticationSuccessHandler) //登录成功处理
.failureHandler(zekeAuthenticationFailureHandler) //登录失败处理
.and()
.rememberMe()
.tokenRepository(persistentTokenRepository())
.tokenValiditySeconds(securityProperties.getBrowser().getRememberMeSeconds())
.userDetailsService(userDetailsService)
.and()
.authorizeRequests()
.antMatchers("/authentication/require",
securityProperties.getBrowser().getLoginPage(),
"/code/image") //不拦截的URL
.permitAll()
.anyRequest()
.authenticated();
}
六、启动服务,访问localhost/zeke-login.html,勾选记住我测试
相关文章推荐
- SpringBoot+Shiro学习之“记住我”和“GIF验证码”功能的实现
- spring security remember-me
- SpringBoot学习:整合shiro(rememberMe记住我功能)
- 记住我remember-me功能的几种实现方式
- SpringBoot + SpringSecurity 短信验证码登录功能实现
- SpringBoot+Shiro学习之“记住我”和“GIF验证码”功能的实现
- Spring Security Remember Me Example
- 通过Eureka、DiscoveryClient实现Spring Boot Admin管理功能
- [Spring Boot 系列] 集成maven和Spring boot的profile功能
- springboot security
- 记一次spring boot 功能模块化 freemarker只能识别一个resources目录下前端展示模板问题
- SpringBoot + Spring Security 基本使用及个性化登录配置详解
- SpringBoot+Security 发送短信验证码的实现
- Spring-Security+Freemarker 开启跨域请求伪造防护功能
- springboot整合tomcat自带的websocket实现在线聊天及象棋网页对战功能
- springboot+security 的BCryptPasswordEncoder 使用
- Spring Boot Shiro 权限信息缓存处理,记住我,thymleaf使用shiro标签
- Spring Boot + Spring Security 防止用户在多处同时登录(一个用户同时只能登录一次)及源码分析
- spring boot security默认配置启动导致的权限错误
- spring boot 加入 spring-security配置 角色前缀 静态资源访问