springboot将session存储在redis中,可以实现session共享,亲测

springboot将session存储在redis中,可以实现session共享

添加依赖


        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-data-redisartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframework.sessiongroupId>
            <artifactId>spring-session-data-redisartifactId>
        dependency>

写一个配置类


package com.jf.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.session.data.redis.config.ConfigureRedisAction;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;

@Configuration
@EnableRedisHttpSession(maxInactiveIntervalInSeconds=60*30)//设置session过期时间,默认也是1800秒
public class HttpSessionConfig {

    //enableRedisKeyspaceNotificationsInitializer
    @Bean
    public static ConfigureRedisAction configureRedisAction() {
        return ConfigureRedisAction.NO_OP;
    }
}

session存取,和以前一样不用变化,还是在httpSession中操作。
springboot将session存储在redis中,可以实现session共享,亲测_第1张图片
redis中存的值
springboot将session存储在redis中,可以实现session共享,亲测_第2张图片

你可能感兴趣的:(springboot)