Session attribute not persisting which is setting at HttpSessionListener using Spring Session Data Redis
Session attributes are missing/not able to fetch which are setting at HttpSessionListener
I want to set some attributes to session when session is created using HttpSessionListener, and I want to validate that attribute in the next level.
To Reproduce After installing redis just install on localhost and default port, build and run the project shared on github.
Expected behavior What ever attributes I am setting on session creation HttpSessionListener
public void sessionCreated(HttpSessionEvent httpSessionEvent){
HttpSession httpSession = httpSessionEvent.getSession();
System.out.println("inside listner class ====");
httpSession.setAttribute("testKey","testValue");
System.out.println("test value inside listner===>"+httpSession.getAttribute("testKey"));
}
here I am able to fetch the session atrribute testKey, but after this when flow goes to servlet, I am not getting the attribute Sample
I have attached the code in this link
I have the same issue. Are there any updates?
No. But I have updated some work around in stackoverflow, please check and let me know.
On Thu, 21 Mar 2024 at 1:00 PM, Christoph Empl @.***> wrote:
I have the same issue. Are there any updates?
— Reply to this email directly, view it on GitHub https://github.com/spring-projects/spring-session/issues/1788#issuecomment-2011376964, or unsubscribe https://github.com/notifications/unsubscribe-auth/AH6HQC6TA7JFTDXVJYLJAVDYZKEDBAVCNFSM4XCECZMKU5DIOJSWCZC7NNSXTN2JONZXKZKDN5WW2ZLOOQ5TEMBRGEZTONRZGY2A . You are receiving this because you authored the thread.Message ID: @.***>
Do you have any link?
Hi everyone, what are you trying to achieve with this? Maybe if I can understand the use case better I can provide some insights.
As far as I can remember, the HttpSessionListener receives events outside of the filter chain, having no Spring Session filter to save the session after you performed the changes.
Put some initial attributes in every created session. I'm trying to migrate an application to Spring Session and in this application, every session is initialized with an attribute in a HttpSessionListener.
At first, I see two alternatives to achieve such result:
- Use the
SessionRepositorybean in yourHttpSessionListenerimplementation:
public class SessionEventsListener implements HttpSessionListener {
private SessionRepository<? extends Session> sessionRepository;
public void sessionCreated(HttpSessionEvent se) {
String sessionId = se.getSession().getId();
Session session = this.sessionRepository.findById(sessionId);
session.setAttribute("myAttribute", "myValue");
this.sessionRepository.save(session);
}
}
- Create your own implementation of
SessionRepositorythat insert the attributes after creating the session:
public class MySessionRepository implements SessionRepository {
private RedisSessionRepository delegate;
@Override
public Session createSession() {
Session session = this.delegate.createSession();
session.setAttribute("myAttribute", "myValue");
}
// ...
}
Note that I didn't test it myself, some compilation errors and parts might be missing but I believe it won't be hard to figure it out. Can you verify if those alternatives satisfies your needs?
https://stackoverflow.com/questions/66026796/session-atributes-missing-from-spring-session-when-setting-inside-httpsessionlis/66253899#66253899
Please check this link
On Thu, 21 Mar 2024 at 4:12 PM, Christoph Empl @.***> wrote:
Do you have any link?
— Reply to this email directly, view it on GitHub https://github.com/spring-projects/spring-session/issues/1788#issuecomment-2011899224, or unsubscribe https://github.com/notifications/unsubscribe-auth/AH6HQC42ZR56CG5HLTHOTL3YZK2RRAVCNFSM4XCECZMKU5DIOJSWCZC7NNSXTN2JONZXKZKDN5WW2ZLOOQ5TEMBRGE4DSOJSGI2A . You are receiving this because you authored the thread.Message ID: @.***>