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 ?