spring-session-data-redis
是spring session项目中的一个子模块,,他允许你使用Redis来存储http session,,从而支持多个应用实例之间共享session,,,即分布式session
原理: @EnableRedisHttpSession
annotation. This creates a Spring bean with the name of springSessionRepositoryFilter
that implements Filter. The filter is in charge of replacing the HttpSession
implementation to be backed by Spring Session.
@EnableRedisHttpSession 会创建一个 过滤器,,这个过滤器,去拦截请求,设置session,,,你用的还是request.getSession()
但是底层已经不是tomcat的Session,,,而是在redis中取session
文档:https://docs.spring.io/spring-session/reference/3.4-SNAPSHOT/configuration/redis.html
引入包:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- spring session + redis : 将session放到redis中存储-->
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
配置:
spring:
redis:
host: localhost
database: 0
port: 6379
timeout: 5000
session:
store-type: redis
# session 30天过期
timeout: 2592000
redis:
namespace: spring:session:waterkid
使用@EnableRedisHttpSession
开启这个session过滤器:
@Configuration
@EnableRedisHttpSession(redisNamespace = "spring:session:test")
public class RedisConfig {
@Bean
public RedisSerializer<Object> springSessionDefaultRedisSerializer() {
return new GenericJackson2JsonRedisSerializer();
}
}
这样就会自动将session存入到redis中
不知道为什么传入 ObjectMapper 会报错,,