Skip to content

Commit 340fc3e

Browse files
committed
持久化令牌RememberMe访问
1 parent 9146908 commit 340fc3e

File tree

5 files changed

+158
-0
lines changed

5 files changed

+158
-0
lines changed

remember-me-persistent/pom.xml

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>2.5.6</version>
9+
<relativePath/> <!-- lookup parent from repository -->
10+
</parent>
11+
<groupId>com.sspath</groupId>
12+
<artifactId>remember-me-persistent</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<name>remember-me-persistent</name>
15+
<description>remember-me-persistent</description>
16+
<properties>
17+
<java.version>1.8</java.version>
18+
</properties>
19+
<dependencies>
20+
<dependency>
21+
<groupId>org.springframework.boot</groupId>
22+
<artifactId>spring-boot-starter-jdbc</artifactId>
23+
</dependency>
24+
<dependency>
25+
<groupId>org.springframework.boot</groupId>
26+
<artifactId>spring-boot-starter-security</artifactId>
27+
</dependency>
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-web</artifactId>
31+
</dependency>
32+
33+
<dependency>
34+
<groupId>mysql</groupId>
35+
<artifactId>mysql-connector-java</artifactId>
36+
<scope>runtime</scope>
37+
</dependency>
38+
<dependency>
39+
<groupId>org.springframework.boot</groupId>
40+
<artifactId>spring-boot-starter-test</artifactId>
41+
<scope>test</scope>
42+
</dependency>
43+
<dependency>
44+
<groupId>org.springframework.security</groupId>
45+
<artifactId>spring-security-test</artifactId>
46+
<scope>test</scope>
47+
</dependency>
48+
</dependencies>
49+
50+
<build>
51+
<plugins>
52+
<plugin>
53+
<groupId>org.springframework.boot</groupId>
54+
<artifactId>spring-boot-maven-plugin</artifactId>
55+
</plugin>
56+
</plugins>
57+
</build>
58+
59+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.sspath.remembermepersistent;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class RememberMePersistentApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(RememberMePersistentApplication.class, args);
11+
}
12+
13+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.sspath.remembermepersistent.config;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
7+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
8+
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
9+
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
10+
import org.springframework.security.crypto.password.PasswordEncoder;
11+
import org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl;
12+
13+
import javax.sql.DataSource;
14+
15+
/**
16+
* @FileName: SecurityConfig.java
17+
* @Description: DataSource和JdbcTokenRepository需要配置
18+
* @Author: ABCpril
19+
* @Date: 2021/11/18
20+
*/
21+
22+
@Configuration
23+
public class SecurityConfig extends WebSecurityConfigurerAdapter {
24+
@Autowired
25+
DataSource dataSource;
26+
27+
@Bean
28+
JdbcTokenRepositoryImpl jdbcTokenRepository() {
29+
JdbcTokenRepositoryImpl jdbcTokenRepository = new JdbcTokenRepositoryImpl();
30+
jdbcTokenRepository.setDataSource(dataSource);
31+
return jdbcTokenRepository;
32+
}
33+
34+
@Bean
35+
PasswordEncoder passwordEncoder() {
36+
return NoOpPasswordEncoder.getInstance();
37+
}
38+
39+
@Override
40+
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
41+
auth.inMemoryAuthentication()
42+
.withUser("jack")
43+
.password("123")
44+
.roles("admin");
45+
}
46+
47+
@Override
48+
protected void configure(HttpSecurity http) throws Exception {
49+
http.authorizeRequests()
50+
.anyRequest().authenticated()
51+
.and()
52+
.formLogin()
53+
.and()
54+
.rememberMe()
55+
// 持久化RememberMe不需要配置key
56+
// 配置了tokenRepository就会获取到PersistentTokenBasedRememberMeServices的RememberMeServices实例
57+
// .key("remember")
58+
.tokenRepository(jdbcTokenRepository())
59+
.and()
60+
.csrf().disable();
61+
}
62+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.sspath.remembermepersistent.controller;
2+
3+
import org.springframework.stereotype.Controller;
4+
import org.springframework.web.bind.annotation.GetMapping;
5+
import org.springframework.web.bind.annotation.RestController;
6+
7+
/**
8+
* @FileName: HelloController.java
9+
* @Description: 重启浏览器或者服务端后访问接口,都会建立新会话,token也随之更新
10+
* @Author: ABCpril
11+
* @Date: 2021/11/18
12+
*/
13+
14+
@RestController
15+
public class HelloController {
16+
@GetMapping("/hello")
17+
public String hello() {
18+
return "hello";
19+
}
20+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
spring.datasource.username=root
2+
spring.datasource.password=123
3+
spring.datasource.url=jdbc:mysql://localhost:3306/security06?useSSL=false&useUnicode=true&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true&characterEncoding=utf-8
4+

0 commit comments

Comments
 (0)