Spring Security实现记住我功能
Posted P_Shelter
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Security实现记住我功能相关的知识,希望对你有一定的参考价值。
POM:
1 <dependency> 2 <groupId>org.springframework.security</groupId> 3 <artifactId>spring-security-web</artifactId> 4 <version>5.0.4.RELEASE</version> 5 </dependency> 6 <dependency> 7 <groupId>org.springframework.security</groupId> 8 <artifactId>spring-security-config</artifactId> 9 <version>5.0.3.RELEASE</version> 10 </dependency> 11 <dependency> 12 <groupId>com.alibaba</groupId> 13 <artifactId>druid</artifactId> 14 <version>1.1.11</version> 15 </dependency>
html:
1 <input type="checkbox" name="remember-me" checked value="true">
配置类:
1 @Autowired 2 private DataSource myDataSource; 3 4 @Autowired 5 private UserDetailService userDetailsService; 6 7 @Override 8 protected void configure(HttpSecurity http) throws Exception { 9 http.rememberMe() 10 .tokenRepository(persistentTokenRepository()) 11 .userDetailsService(userDetailsService) 12 //token "记住我"功能的token过期时间(秒) 13 .tokenValiditySeconds(3600); 14 } 15 @Bean 16 public PersistentTokenRepository persistentTokenRepository(){ 17 //remember-me token 18 JdbcTokenRepositoryImpl tokenRepository=new JdbcTokenRepositoryImpl(); 19 tokenRepository.setDataSource(myDataSource); 20 //第一次启动时自动建token-登陆表 21 // tokenRepository.setCreateTableOnStartup(true); 22 return tokenRepository; 23 }
dataSource:
1 @Configuration 2 public class DataSourceConfig { 3 @Bean(name = "myDataSource") 4 @Qualifier("myDataSource") 5 @ConfigurationProperties(prefix="spring.datasource") 6 public DataSource dataSource(){ 7 DruidDataSource druidDataSource = new DruidDataSource(); 8 return druidDataSource; 9 // return DataSourceBuilder.create().build(); 10 } 11 }
yml:
1 spring: 2 datasource: 3 type: com.alibaba.druid.pool.DruidDataSource 4 driver-class-name: com.mysql.jdbc.Driver 5 url: jdbc:mysql://127.0.0.1:3306/security?characterEncoding=utf8 6 username: root 7 password: 123456 8 #最大活跃数 9 maxActive: 20 10 #初始化数量 11 initialSize: 1 12 #最大连接等待超时时间 13 maxWait: 60000 14 #打开PSCache,并且指定每个连接PSCache的大小 15 poolPreparedStatements: true 16 maxPoolPreparedStatementPerConnectionSize: 20 17 #通过connectionProperties属性来打开mergeSql功能;慢SQL记录 18 #connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000 19 minIdle: 1 20 timeBetweenEvictionRunsMillis: 60000 21 minEvictableIdleTimeMillis: 300000 22 validationQuery: select 1 from dual 23 testWhileIdle: true 24 testOnBorrow: false 25 testOnReturn: false 26 jpa: 27 properties: 28 hibernate: 29 show_sql: true 30 format_sql: true
Druid配置类:
1 @Configuration 2 public class DruidConfiguration { 3 4 @Bean 5 public ServletRegistrationBean statViewServlet(){ 6 //创建servlet注册实体 7 ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(),"/druid/*"); 8 //设置ip白名单 9 servletRegistrationBean.addInitParameter("allow","127.0.0.1"); 10 //设置ip黑名单,如果allow与deny共同存在时,deny优先于allow 11 servletRegistrationBean.addInitParameter("deny","192.168.0.19"); 12 //设置控制台管理用户 13 servletRegistrationBean.addInitParameter("loginUsername","druid"); 14 servletRegistrationBean.addInitParameter("loginPassword","123456"); 15 //是否可以重置数据 16 servletRegistrationBean.addInitParameter("resetEnable","false"); 17 return servletRegistrationBean; 18 } 19 20 @Bean 21 public FilterRegistrationBean statFilter(){ 22 //创建过滤器 23 FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter()); 24 //设置过滤器过滤路径 25 filterRegistrationBean.addUrlPatterns("/*"); 26 //忽略过滤的形式 27 filterRegistrationBean.addInitParameter("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"); 28 return filterRegistrationBean; 29 } 30 }
以上是关于Spring Security实现记住我功能的主要内容,如果未能解决你的问题,请参考以下文章
Spring WebFlux + Security - 我们有“记住我”功能吗?