forked from yudaocode/SpringBoot-Labs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
YunaiV
committed
Nov 22, 2019
1 parent
cb5c3f2
commit d4bc1c5
Showing
18 changed files
with
356 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
...main/java/cn/iocoder/springboot/lab26/distributedsession/config/SessionConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package cn.iocoder.springboot.lab26.distributedsession.config; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.data.redis.serializer.RedisSerializer; | ||
import org.springframework.session.data.redis.RedisOperationsSessionRepository; | ||
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession; | ||
import org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration; | ||
|
||
@Configuration | ||
@EnableRedisHttpSession // 自动化配置 Spring Session 使用 Redis 作为数据源 | ||
public class SessionConfiguration { | ||
|
||
/** | ||
* 创建 {@link RedisOperationsSessionRepository} 使用的 RedisSerializer Bean 。 | ||
* | ||
* 具体可以看看 {@link RedisHttpSessionConfiguration#setDefaultRedisSerializer(RedisSerializer)} 方法, | ||
* 它会引入名字为 "springSessionDefaultRedisSerializer" 的 Bean 。 | ||
* | ||
* @return RedisSerializer Bean | ||
*/ | ||
@Bean(name = "springSessionDefaultRedisSerializer") | ||
public RedisSerializer springSessionDefaultRedisSerializer() { | ||
return RedisSerializer.json(); | ||
} | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
lab-26/lab-26-distributed-session-01/src/main/resources/application.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
spring: | ||
# 对应 RedisProperties 类 | ||
redis: | ||
host: 127.0.0.1 | ||
port: 6379 | ||
password: # Redis 服务器密码,默认为空。生产中,一定要设置 Redis 密码! | ||
database: 0 # Redis 数据库号,默认为 0 。 | ||
timeout: 0 # Redis 连接超时时间,单位:毫秒。 | ||
# 对应 RedisProperties.Jedis 内部类 | ||
jedis: | ||
pool: | ||
max-active: 8 # 连接池最大连接数,默认为 8 。使用负数表示没有限制。 | ||
max-idle: 8 # 默认连接数最大空闲的连接数,默认为 8 。使用负数表示没有限制。 | ||
min-idle: 0 # 默认连接池最小空闲的连接数,默认为 0 。允许设置 0 和 正数。 | ||
max-wait: -1 # 连接池最大阻塞等待时间,单位:毫秒。默认为 -1 ,表示不限制。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>2.1.10.RELEASE</version> | ||
<relativePath/> <!-- lookup parent from repository --> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>lab-26-distributed-session-02</artifactId> | ||
|
||
<dependencies> | ||
<!-- 实现对 Spring MVC 的自动化配置 --> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
|
||
<!-- 实现对 Spring Session 使用 MongoDB 作为数据源的自动化配置 --> | ||
<dependency> | ||
<groupId>org.springframework.session</groupId> | ||
<artifactId>spring-session-data-mongodb</artifactId> | ||
</dependency> | ||
|
||
<!-- 自动化配置 Spring Data Mongodb --> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-data-mongodb</artifactId> | ||
</dependency> | ||
|
||
</dependencies> | ||
|
||
</project> |
13 changes: 13 additions & 0 deletions
13
...-session-02/src/main/java/cn/iocoder/springboot/lab26/distributedsession/Application.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package cn.iocoder.springboot.lab26.distributedsession; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
public class Application { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(Application.class, args); | ||
} | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
...main/java/cn/iocoder/springboot/lab26/distributedsession/config/SessionConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package cn.iocoder.springboot.lab26.distributedsession.config; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.session.data.mongo.AbstractMongoSessionConverter; | ||
import org.springframework.session.data.mongo.JacksonMongoSessionConverter; | ||
import org.springframework.session.data.mongo.config.annotation.web.http.EnableMongoHttpSession; | ||
|
||
@Configuration | ||
@EnableMongoHttpSession // 自动化配置 Spring Session 使用 MongoDB 作为数据源 | ||
public class SessionConfiguration { | ||
|
||
@Bean | ||
public AbstractMongoSessionConverter mongoSessionConverter() { | ||
return new JacksonMongoSessionConverter(); | ||
} | ||
|
||
} |
38 changes: 38 additions & 0 deletions
38
...ain/java/cn/iocoder/springboot/lab26/distributedsession/controller/SessionController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package cn.iocoder.springboot.lab26.distributedsession.controller; | ||
|
||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import javax.servlet.http.HttpSession; | ||
import java.util.Enumeration; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
@RestController | ||
@RequestMapping("/session") | ||
public class SessionController { | ||
|
||
@GetMapping("/set") // 其实 PostMapping 更合适,单纯为了方便 | ||
public void set(HttpSession session, | ||
@RequestParam("key") String key, | ||
@RequestParam("value") String value) { | ||
session.setAttribute(key, value); | ||
} | ||
|
||
@GetMapping("/get_all") | ||
public Map<String, Object> getAll(HttpSession session) { | ||
Map<String, Object> result = new HashMap<>(); | ||
// 遍历 | ||
for (Enumeration<String> enumeration = session.getAttributeNames(); | ||
enumeration.hasMoreElements();) { | ||
String key = enumeration.nextElement(); | ||
Object value = session.getAttribute(key); | ||
result.put(key, value); | ||
} | ||
// 返回 | ||
return result; | ||
} | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
lab-26/lab-26-distributed-session-02/src/main/resources/application.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
spring: | ||
data: | ||
# MongoDB 配置项,对应 MongoProperties 类 | ||
mongodb: | ||
host: 127.0.0.1 | ||
port: 27017 | ||
database: yourdatabase | ||
username: test01 | ||
password: password01 | ||
# 上述属性,也可以只配置 uri | ||
|
||
logging: | ||
level: | ||
org: | ||
springframework: | ||
data: | ||
mongodb: | ||
core: DEBUG # 打印 mongodb 操作的具体语句。生产环境下,不建议开启。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>2.1.10.RELEASE</version> | ||
<relativePath/> <!-- lookup parent from repository --> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>lab-26-distributed-session-springsecurity</artifactId> | ||
|
||
<dependencies> | ||
<!-- 实现对 Spring MVC 的自动化配置 --> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
|
||
<!-- 实现对 Spring Session 使用 Redis 作为数据源的自动化配置 --> | ||
<dependency> | ||
<groupId>org.springframework.session</groupId> | ||
<artifactId>spring-session-data-redis</artifactId> | ||
</dependency> | ||
|
||
<!-- 实现对 Spring Data Redis 的自动化配置 --> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-data-redis</artifactId> | ||
<exclusions> | ||
<!-- 去掉对 Lettuce 的依赖,因为 Spring Boot 优先使用 Lettuce 作为 Redis 客户端 --> | ||
<exclusion> | ||
<groupId>io.lettuce</groupId> | ||
<artifactId>lettuce-core</artifactId> | ||
</exclusion> | ||
</exclusions> | ||
</dependency> | ||
<!-- 引入 Jedis 的依赖,这样 Spring Boot 实现对 Jedis 的自动化配置 --> | ||
<dependency> | ||
<groupId>redis.clients</groupId> | ||
<artifactId>jedis</artifactId> | ||
</dependency> | ||
|
||
<!-- 实现对 Spring Security 的自动化配置 --> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-security</artifactId> | ||
</dependency> | ||
|
||
</dependencies> | ||
|
||
</project> |
13 changes: 13 additions & 0 deletions
13
...ingsecurity/src/main/java/cn/iocoder/springboot/lab26/distributedsession/Application.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package cn.iocoder.springboot.lab26.distributedsession; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
public class Application { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(Application.class, args); | ||
} | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
...main/java/cn/iocoder/springboot/lab26/distributedsession/config/SessionConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package cn.iocoder.springboot.lab26.distributedsession.config; | ||
|
||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession; | ||
|
||
@Configuration | ||
@EnableRedisHttpSession // 自动化配置 Spring Session 使用 Redis 作为数据源 | ||
public class SessionConfiguration { | ||
|
||
} |
46 changes: 46 additions & 0 deletions
46
...ain/java/cn/iocoder/springboot/lab26/distributedsession/controller/SessionController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package cn.iocoder.springboot.lab26.distributedsession.controller; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.session.FindByIndexNameSessionRepository; | ||
import org.springframework.session.Session; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.util.Map; | ||
|
||
@RestController | ||
@RequestMapping("/session") | ||
public class SessionController { | ||
|
||
@Autowired | ||
private FindByIndexNameSessionRepository sessionRepository; | ||
|
||
@GetMapping("/list") | ||
public Map<String, ? extends Session> list(@RequestParam("username") String username) { | ||
return sessionRepository.findByPrincipalName(username); | ||
} | ||
|
||
// @GetMapping("/set") // 其实 PostMapping 更合适,单纯为了方便 | ||
// public void set(HttpSession session, | ||
// @RequestParam("key") String key, | ||
// @RequestParam("value") String value) { | ||
// session.setAttribute(key, value); | ||
// } | ||
// | ||
// @GetMapping("/get_all") | ||
// public Map<String, Object> getAll(HttpSession session) { | ||
// Map<String, Object> result = new HashMap<>(); | ||
// // 遍历 | ||
// for (Enumeration<String> enumeration = session.getAttributeNames(); | ||
// enumeration.hasMoreElements();) { | ||
// String key = enumeration.nextElement(); | ||
// Object value = session.getAttribute(key); | ||
// result.put(key, value); | ||
// } | ||
// // 返回 | ||
// return result; | ||
// } | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
lab-26/lab-26-distributed-session-springsecurity/src/main/resources/application.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
spring: | ||
# 对应 RedisProperties 类 | ||
redis: | ||
host: 127.0.0.1 | ||
port: 6379 | ||
password: # Redis 服务器密码,默认为空。生产中,一定要设置 Redis 密码! | ||
database: 0 # Redis 数据库号,默认为 0 。 | ||
timeout: 0 # Redis 连接超时时间,单位:毫秒。 | ||
# 对应 RedisProperties.Jedis 内部类 | ||
jedis: | ||
pool: | ||
max-active: 8 # 连接池最大连接数,默认为 8 。使用负数表示没有限制。 | ||
max-idle: 8 # 默认连接数最大空闲的连接数,默认为 8 。使用负数表示没有限制。 | ||
min-idle: 0 # 默认连接池最小空闲的连接数,默认为 0 。允许设置 0 和 正数。 | ||
max-wait: -1 # 连接池最大阻塞等待时间,单位:毫秒。默认为 -1 ,表示不限制。 | ||
# 对应 SecurityProperties 类 | ||
security: | ||
user: # 配置内存中,可登陆的用户名和密码 | ||
name: yudaoyuanma | ||
password: nicai |
Binary file added
BIN
+16 Bytes
...ession-springsecurity/target/classes/META-INF/lab-26-distributed-session-01.kotlin_module
Binary file not shown.
20 changes: 20 additions & 0 deletions
20
lab-26/lab-26-distributed-session-springsecurity/target/classes/application.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
spring: | ||
# 对应 RedisProperties 类 | ||
redis: | ||
host: 127.0.0.1 | ||
port: 6379 | ||
password: # Redis 服务器密码,默认为空。生产中,一定要设置 Redis 密码! | ||
database: 0 # Redis 数据库号,默认为 0 。 | ||
timeout: 0 # Redis 连接超时时间,单位:毫秒。 | ||
# 对应 RedisProperties.Jedis 内部类 | ||
jedis: | ||
pool: | ||
max-active: 8 # 连接池最大连接数,默认为 8 。使用负数表示没有限制。 | ||
max-idle: 8 # 默认连接数最大空闲的连接数,默认为 8 。使用负数表示没有限制。 | ||
min-idle: 0 # 默认连接池最小空闲的连接数,默认为 0 。允许设置 0 和 正数。 | ||
max-wait: -1 # 连接池最大阻塞等待时间,单位:毫秒。默认为 -1 ,表示不限制。 | ||
# 对应 SecurityProperties 类 | ||
security: | ||
user: # 配置内存中,可登陆的用户名和密码 | ||
name: yudaoyuanma | ||
password: nicai |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters