Lokesh Pandey
Lokesh Pandey

Reputation: 1789

Getting session uuid set in redis

I am setting a session attribute whenever a user is logged in like this request.getSession().setAttribute(sessionUuid, user);. sessionUuid is uuid which I am setting. But seems like this sessionUuid is not the session id, redis generates a session id by itself. Which looks like something like this

1) "spring:session:expirations:1597563240000"
2) "spring:session:sessions:expires:9dfef52d-d94a-4bcf-a08e-4a362b36f332"
3) "spring:session:sessions:9dfef52d-d94a-4bcf-a08e-4a362b36f332"

From my understanding the session uuid is 9dfef52d-d94a-4bcf-a08e-4a362b36f332, for the attribute I have set.

What I am trying to understand is since redis generates a session id by itslef, then how am I supposed to get the redis session id, so that next time whenever user makes a request I can validate if the session is active or not ?

if(request.getSession().getAttribute("9dfef52d-d94a-4bcf-a08e-4a362b36f332") != null) {
    return (UserSessionComponent) request.getSession().getAttribute(session);
}

Or does the redis search all the session for that particular attribute ?

Upvotes: 0

Views: 2156

Answers (1)

Lokesh Pandey
Lokesh Pandey

Reputation: 1789

Ok, I think I got it by experimenting. So for the SO readers we create a new session using this HttpSession newSession = request.getSession(true);, this will return a session or else it will create a new session if it doesn't exist.

And the HttpServletRequest will have the session whenever a user is making request or it may not have the session for the first time. This request.getRequestedSessionId() will give the same session uuid which is set on redis server, in this case it is 9dfef52d-d94a-4bcf-a08e-4a362b36f332 or else it will be null if it's first time.

Then using request.getSession().getAttribute(session); if it return null that means the session has been expired or else this will return the value.

By the way this is a good read on spring session

Upvotes: 1

Related Questions