使用Java Spring Boot Session Data Redis无法将会话存储在Redis中

7

我认为我在Redis中保存会话信息时遇到了问题。 我尝试按照有关spring-session-data-redis的说明进行操作,但是当我发起请求时,无法在redis中找到任何会话信息。 以下是我的代码和配置。

应用程序属性文件:

spring.session.store-type=redis
spring.session.redis.flush-mode= on-save
spring.session.redis.namespace= spring:session
spring.redis.host= 10.10.10.10
spring.redis.port=10000

pom.xml:

<dependency>
    <groupId>org.springframework.session</groupId>
    <artifactId>spring-session-data-redis</artifactId>
</dependency>
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.9.0</version>
    <type>jar</type>
</dependency>

应用程序配置类:

@Configuration
@EnableRedisHttpSession
@PropertySource("application.properties")
public class AppConfig {

    @Value("${spring.redis.host}")
    private String redisHostName;

    @Value("${spring.redis.port}")
    private int redisPort;

    @Bean
    JedisConnectionFactory jedisConnectionFactory() {
        RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration(
                redisHostName, redisPort);
        return new JedisConnectionFactory(redisStandaloneConfiguration);
    }
}

以下是我的示例 Get 请求控制器:

// test only
    @CrossOrigin
    @GetMapping(value = "/test/test")
    public String justTest(HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.setAttribute("sessionId", "ssssss");

        String value = (String) session.getAttribute("sessionId").toString();
        return value;
    }

我是不是漏掉了什么?spring-session-data-redis应该自动将会话存储到Redis中,但似乎并没有这样做。我需要spring-sessionspring-data-redis的依赖吗?Redis连接已经建立,我将其设置为监听所有接口。

看起来当我发起请求时可以检索到会话ID,但为什么会话没有插入到Redis中呢?


2
你解决了你的问题吗?我也遇到了同样的问题,正在浏览Spring Boot的示例项目以寻找解决方案。 - Petros Makris
1
嗨@billcyz,你的问题解决了吗?我遇到了同样的情况,正在寻找答案。https://dev59.com/GLzpa4cB1Zd3GeqPO6Q5 - Lokesh Pandey
1个回答

1

spring.session.redis.flush-mode=IMMEDIATE

/**
     * Flush mode for the Redis sessions. The default is {@code ON_SAVE} which only
     * updates the backing Redis when {@link SessionRepository#save(Session)} is invoked.
     * In a web environment this happens just before the HTTP response is committed.
     * <p>
     * Setting the value to {@code IMMEDIATE} will ensure that the any updates to the
     * Session are immediately written to the Redis instance.
     * @return the {@link RedisFlushMode} to use
     * @since 1.1

文档链接


你能解释一下你的答案吗? - Alex Yu
spring.session.redis.flush-mode=on-save,会在HTTP响应提交之前保存数据。 - user9482905

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接