skip to Main Content

I am writing my rest apis using spring boot. And I am trying to maintain user’s session on redis server. Redis is up and running on the default port 6379. I have used the lettuce jar to make connection to the redis server. But seems like my session is not being set on redis server. I try to get the session object set using uuid, and it return something like this

127.0.0.1:6379> get 02978830-2f35-47b7-a367-1f48e40d0ea0
(nil)

From redis cli, I am able to set and get the key values.

127.0.0.1:6379> set 123 123dummy
OK
127.0.0.1:6379> get 123
"123dummy"
127.0.0.1:6379> 

This is code snippet where I am trying to see if the user has been logged in depending on their active session, if the session is there then I am returning user. Else I am logging em and then setting session on redis server and then returning user.

UserAttributes findUserByEmailIdOrPhoneNumber(HttpServletRequest request,
      @RequestParam(value = "userLoginWay", required = false) String userLoginWay,
      @RequestParam(value = "userPassword", required = false) String userPassword,
      @RequestParam(value = "session", required = false) String session) {
    if(request.getSession().getAttribute(session) != null) {
        //we have session return user
        return user;
    } else {
        login(userLoginWay, userPassword)
        //set the session in redis here
        String sessionUuid = UUID.randomUUID().toString();
        request.getSession().setAttribute(sessionUuid, user);
        return user;
    }
}

This user is the object which I am trying to set as a session value and uuid as a key.
This is how I am trying to connect to the redis server

@Configuration
@EnableRedisHttpSession
public class SessionConfig extends AbstractHttpSessionApplicationInitializer {

    @Bean
    public LettuceConnectionFactory connectionFactory() {
        return new LettuceConnectionFactory();
    }

}

This is what I have in application.properties

#Configuring Redis server to manage sessions
spring.session.store-type=redis
spring.redis.host=localhost
spring.redis.port=6379

Any idea what’s wrong with this ?

2

Answers


  1. Chosen as BEST ANSWER

    Ok, So I got it on my own, I did some changes in session config class and initialize it by extending AbstractHttpSessionApplicationInitializer

    @EnableRedisHttpSession
    public class SessionConfig {
    
        @Bean
        public LettuceConnectionFactory connectionFactory() {
            return new LettuceConnectionFactory();
        }
    }
    

    By extending AbstractHttpSessionApplicationInitializer, ensures that Spring Bean by the name of springSessionRepositoryFilter is registered with our Servlet Container for every request. For more information refer this

    public class SessionConfigInitializer extends AbstractHttpSessionApplicationInitializer {
    
        public SessionConfigInitializer() {
            super(SessionConfig.class);
        }
    }
    

    After this when I looked in the redis server I can see something like this

    127.0.0.1:6379> keys '*'
    1) "spring:session:expirations:1597497540000"
    2) "spring:session:index:org.springframework.session.FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME:admin"
    3) "spring:session:sessions:expires:dc2172eb-2282-4600-9cd3-7f5d9bbb62b8"
    4) "spring:session:sessions:dc2172eb-2282-4600-9cd3-7f5d9bbb62b8"
    127.0.0.1:6379> 
    

    The other aspect is from the redis server, once you set your session, depending upon what the value you are setting you need to retrieve it. Redis support six types string, list, set, zset, hash and stream. In my case it was hash so to see the value the command will be HGETALL <key>


  2. You have to initialize the connection with redis server using its default constructor.

    return new LettuceConnectionFactory(new RedisStandaloneConfiguration(host, port));
    
    1. You can get values from properties file using @value or better method is marking the redis @configuration class with @ConfigurationProperties(prefix="spring.redis")

    2. add fields String host, int port with respective getters and setters.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search