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
Ok, So I got it on my own, I did some changes in session config class and initialize it by extending
AbstractHttpSessionApplicationInitializer
By extending
AbstractHttpSessionApplicationInitializer
, ensures that Spring Bean by the name ofspringSessionRepositoryFilter
is registered with our Servlet Container for every request. For more information refer thisAfter this when I looked in the redis server I can see something like this
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 beHGETALL <key>
You have to initialize the connection with redis server using its default constructor.
You can get values from properties file using
@value
or better method is marking the redis @configuration class with@ConfigurationProperties(prefix="spring.redis")
add fields String host, int port with respective getters and setters.