簡體   English   中英

無法使用Java Spring Boot Session Data Redis在Redis中存儲會話

[英]Unable to store session in Redis using Java Spring Boot Session Data Redis

我認為我在Redis中保存會話信息時遇到問題。 我試圖按照spring-session-data-redis的說明進行操作,但是當我啟動請求時,我無法在redis中找到任何會話信息。 以下是我的代碼和配置。

application.properties文件:

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);
    }
}

我的示例獲取請求控制器:

// 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-session依賴和spring-data-redis依賴? redis連接正常,我將其設置為偵聽所有接口。

我似乎可以在啟動請求時檢索會話ID,但為什么會話沒有插入Redis?

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

鏈接到文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM