7

I think I have problems saving session information in Redis. I've tried to follow the instruction about spring-session-data-redis, but I can't find any session information within redis when I start a request. The following is my code and config.

application.properties file:

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>

Application Configuration class:

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

And my sample Get request controller:

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

Am I missing something? The spring-session-data-redis is supposed to store session to Redis automatically, but it seems not acting like that. Do I need spring-session dependency and spring-data-redis dependency? The redis connection is ok, I set it to listen to all interfaces.

It seems I can retrieve the session id when I start a request, but why the session is not inserted into Redis?

2
  • 2
    Did you solve your problem? I am facing the same issue and I browsing the sample projects from spring boot for a solution Commented Nov 28, 2018 at 14:06
  • 1
    Hi @billcyz, did your problem get's solved ? I ran into the same situation and looking for answers. stackoverflow.com/questions/63326384/… Commented Aug 15, 2020 at 6:59

1 Answer 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

Link to docs

Sign up to request clarification or add additional context in comments.

2 Comments

Can you explain your answer?
spring.session.redis.flush-mode= on-save, will save the data just before the HTTP response is committed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.