在SpringBoot 使用Redis存储Session

 引入依赖

1.5.6.RELEASE
2.9.0

关于第一个依赖是一定要引入的但是第二个和第三个依赖就要看你是否会使用Redis数据库,但是尽量加入

   org.springframework.session
   spring-session-data-redis

这个依赖一般在主pom和从pom中都要有

    org.springframework.boot
    spring-boot-starter-data-redis
    ${spring-boot-starter.version}

这个依赖主要是在主pom中

    redis.clients
    jedis
    ${jedis.version}

在配置文件中高配置内容:

2、Session配置:

@Configuration
@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 86400*30)
public class SessionConfig {
}

maxInactiveIntervalInSeconds: 设置Session失效时间,使用Redis Session之后,原Boot的server.session.timeout属性不再生效

好了,这样就配置好了,

注意: 如果我们不需要设置过期时间的话,可以不用配置

maxInactiveIntervalInSeconds这个属性也是可以的。

3.在配置文件中的操作

# SPRING SESSION (SessionProperties)
#会话存储类型。
spring.session.store-type=redis

#session更新策略,有ON_SAVE、IMMEDIATE,前者是在调用#SessionRepository#save(org.springframework.session.Session)时,在response commit前刷新缓存,#后者是只要有任何更新就会刷新缓存
spring.session.redis.flush-mode = on-save 
#用于存储会话的密钥的命名空间。
spring.session.redis.namespace=SESSIONS
  • properties配置
# Session store type
spring.session.store-type=redis
# Session timeout. If a duration suffix is not specified, seconds will be used. session过期时间
server.servlet.session.timeout=3600
# Sessions flush mode.
spring.session.redis.flush-mode=on-save
# Namespace for keys used to store sessions.
spring.session.redis.namespace=spring:session

 

你可能感兴趣的:(spring)