Skip to content

Commit d4bc1c5

Browse files
author
YunaiV
committed
spring session 示例
1 parent cb5c3f2 commit d4bc1c5

File tree

18 files changed

+356
-0
lines changed

18 files changed

+356
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# Web 开发
44

55
* [《芋道 Spring Boot SpringMVC 入门》](http://www.iocoder.cn/Spring-Boot/SpringMVC/?github) 对应 [lab-23](https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-23)
6+
* [《芋道 Spring Boot 分布式 Session 入门》](http://www.iocoder.cn/Spring-Boot/Distributed-Session/?github) 对应 [lab-26](https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-26)
67
* [《芋道 Spring Boot API 接口文档 Swagger 入门》](http://www.iocoder.cn/Spring-Boot/Swagger/?github) 对应 [lab-24](https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-24)
78
* [《芋道 Spring Boot 参数校验 Validation 入门》](http://www.iocoder.cn/Spring-Boot/Validation/?github) 对应 [lab-22](https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-22)
89
* [《芋道 Spring Boot WebSocket 入门》](http://www.iocoder.cn/Spring-Boot/WebSocket/?github) 对应 [lab-25](https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-25)

lab-26/lab-26-distributed-session-01/pom.xml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,30 @@
1919
<artifactId>spring-boot-starter-web</artifactId>
2020
</dependency>
2121

22+
<!-- 实现对 Spring Session 使用 Redis 作为数据源的自动化配置 -->
23+
<dependency>
24+
<groupId>org.springframework.session</groupId>
25+
<artifactId>spring-session-data-redis</artifactId>
26+
</dependency>
27+
28+
<!-- 实现对 Spring Data Redis 的自动化配置 -->
29+
<dependency>
30+
<groupId>org.springframework.boot</groupId>
31+
<artifactId>spring-boot-starter-data-redis</artifactId>
32+
<exclusions>
33+
<!-- 去掉对 Lettuce 的依赖,因为 Spring Boot 优先使用 Lettuce 作为 Redis 客户端 -->
34+
<exclusion>
35+
<groupId>io.lettuce</groupId>
36+
<artifactId>lettuce-core</artifactId>
37+
</exclusion>
38+
</exclusions>
39+
</dependency>
40+
<!-- 引入 Jedis 的依赖,这样 Spring Boot 实现对 Jedis 的自动化配置 -->
41+
<dependency>
42+
<groupId>redis.clients</groupId>
43+
<artifactId>jedis</artifactId>
44+
</dependency>
45+
2246
</dependencies>
2347

2448
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package cn.iocoder.springboot.lab26.distributedsession.config;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.data.redis.serializer.RedisSerializer;
6+
import org.springframework.session.data.redis.RedisOperationsSessionRepository;
7+
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
8+
import org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration;
9+
10+
@Configuration
11+
@EnableRedisHttpSession // 自动化配置 Spring Session 使用 Redis 作为数据源
12+
public class SessionConfiguration {
13+
14+
/**
15+
* 创建 {@link RedisOperationsSessionRepository} 使用的 RedisSerializer Bean 。
16+
*
17+
* 具体可以看看 {@link RedisHttpSessionConfiguration#setDefaultRedisSerializer(RedisSerializer)} 方法,
18+
* 它会引入名字为 "springSessionDefaultRedisSerializer" 的 Bean 。
19+
*
20+
* @return RedisSerializer Bean
21+
*/
22+
@Bean(name = "springSessionDefaultRedisSerializer")
23+
public RedisSerializer springSessionDefaultRedisSerializer() {
24+
return RedisSerializer.json();
25+
}
26+
27+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
spring:
2+
# 对应 RedisProperties 类
3+
redis:
4+
host: 127.0.0.1
5+
port: 6379
6+
password: # Redis 服务器密码,默认为空。生产中,一定要设置 Redis 密码!
7+
database: 0 # Redis 数据库号,默认为 0 。
8+
timeout: 0 # Redis 连接超时时间,单位:毫秒。
9+
# 对应 RedisProperties.Jedis 内部类
10+
jedis:
11+
pool:
12+
max-active: 8 # 连接池最大连接数,默认为 8 。使用负数表示没有限制。
13+
max-idle: 8 # 默认连接数最大空闲的连接数,默认为 8 。使用负数表示没有限制。
14+
min-idle: 0 # 默认连接池最小空闲的连接数,默认为 0 。允许设置 0 和 正数。
15+
max-wait: -1 # 连接池最大阻塞等待时间,单位:毫秒。默认为 -1 ,表示不限制。
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>2.1.10.RELEASE</version>
9+
<relativePath/> <!-- lookup parent from repository -->
10+
</parent>
11+
<modelVersion>4.0.0</modelVersion>
12+
13+
<artifactId>lab-26-distributed-session-02</artifactId>
14+
15+
<dependencies>
16+
<!-- 实现对 Spring MVC 的自动化配置 -->
17+
<dependency>
18+
<groupId>org.springframework.boot</groupId>
19+
<artifactId>spring-boot-starter-web</artifactId>
20+
</dependency>
21+
22+
<!-- 实现对 Spring Session 使用 MongoDB 作为数据源的自动化配置 -->
23+
<dependency>
24+
<groupId>org.springframework.session</groupId>
25+
<artifactId>spring-session-data-mongodb</artifactId>
26+
</dependency>
27+
28+
<!-- 自动化配置 Spring Data Mongodb -->
29+
<dependency>
30+
<groupId>org.springframework.boot</groupId>
31+
<artifactId>spring-boot-starter-data-mongodb</artifactId>
32+
</dependency>
33+
34+
</dependencies>
35+
36+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package cn.iocoder.springboot.lab26.distributedsession;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class Application {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(Application.class, args);
11+
}
12+
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package cn.iocoder.springboot.lab26.distributedsession.config;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.session.data.mongo.AbstractMongoSessionConverter;
6+
import org.springframework.session.data.mongo.JacksonMongoSessionConverter;
7+
import org.springframework.session.data.mongo.config.annotation.web.http.EnableMongoHttpSession;
8+
9+
@Configuration
10+
@EnableMongoHttpSession // 自动化配置 Spring Session 使用 MongoDB 作为数据源
11+
public class SessionConfiguration {
12+
13+
@Bean
14+
public AbstractMongoSessionConverter mongoSessionConverter() {
15+
return new JacksonMongoSessionConverter();
16+
}
17+
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package cn.iocoder.springboot.lab26.distributedsession.controller;
2+
3+
import org.springframework.web.bind.annotation.GetMapping;
4+
import org.springframework.web.bind.annotation.RequestMapping;
5+
import org.springframework.web.bind.annotation.RequestParam;
6+
import org.springframework.web.bind.annotation.RestController;
7+
8+
import javax.servlet.http.HttpSession;
9+
import java.util.Enumeration;
10+
import java.util.HashMap;
11+
import java.util.Map;
12+
13+
@RestController
14+
@RequestMapping("/session")
15+
public class SessionController {
16+
17+
@GetMapping("/set") // 其实 PostMapping 更合适,单纯为了方便
18+
public void set(HttpSession session,
19+
@RequestParam("key") String key,
20+
@RequestParam("value") String value) {
21+
session.setAttribute(key, value);
22+
}
23+
24+
@GetMapping("/get_all")
25+
public Map<String, Object> getAll(HttpSession session) {
26+
Map<String, Object> result = new HashMap<>();
27+
// 遍历
28+
for (Enumeration<String> enumeration = session.getAttributeNames();
29+
enumeration.hasMoreElements();) {
30+
String key = enumeration.nextElement();
31+
Object value = session.getAttribute(key);
32+
result.put(key, value);
33+
}
34+
// 返回
35+
return result;
36+
}
37+
38+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
spring:
2+
data:
3+
# MongoDB 配置项,对应 MongoProperties 类
4+
mongodb:
5+
host: 127.0.0.1
6+
port: 27017
7+
database: yourdatabase
8+
username: test01
9+
password: password01
10+
# 上述属性,也可以只配置 uri
11+
12+
logging:
13+
level:
14+
org:
15+
springframework:
16+
data:
17+
mongodb:
18+
core: DEBUG # 打印 mongodb 操作的具体语句。生产环境下,不建议开启。
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>2.1.10.RELEASE</version>
9+
<relativePath/> <!-- lookup parent from repository -->
10+
</parent>
11+
<modelVersion>4.0.0</modelVersion>
12+
13+
<artifactId>lab-26-distributed-session-springsecurity</artifactId>
14+
15+
<dependencies>
16+
<!-- 实现对 Spring MVC 的自动化配置 -->
17+
<dependency>
18+
<groupId>org.springframework.boot</groupId>
19+
<artifactId>spring-boot-starter-web</artifactId>
20+
</dependency>
21+
22+
<!-- 实现对 Spring Session 使用 Redis 作为数据源的自动化配置 -->
23+
<dependency>
24+
<groupId>org.springframework.session</groupId>
25+
<artifactId>spring-session-data-redis</artifactId>
26+
</dependency>
27+
28+
<!-- 实现对 Spring Data Redis 的自动化配置 -->
29+
<dependency>
30+
<groupId>org.springframework.boot</groupId>
31+
<artifactId>spring-boot-starter-data-redis</artifactId>
32+
<exclusions>
33+
<!-- 去掉对 Lettuce 的依赖,因为 Spring Boot 优先使用 Lettuce 作为 Redis 客户端 -->
34+
<exclusion>
35+
<groupId>io.lettuce</groupId>
36+
<artifactId>lettuce-core</artifactId>
37+
</exclusion>
38+
</exclusions>
39+
</dependency>
40+
<!-- 引入 Jedis 的依赖,这样 Spring Boot 实现对 Jedis 的自动化配置 -->
41+
<dependency>
42+
<groupId>redis.clients</groupId>
43+
<artifactId>jedis</artifactId>
44+
</dependency>
45+
46+
<!-- 实现对 Spring Security 的自动化配置 -->
47+
<dependency>
48+
<groupId>org.springframework.boot</groupId>
49+
<artifactId>spring-boot-starter-security</artifactId>
50+
</dependency>
51+
52+
</dependencies>
53+
54+
</project>

0 commit comments

Comments
 (0)