spring-session icon indicating copy to clipboard operation
spring-session copied to clipboard

Session attribute not persisting which is setting at HttpSessionListener using Spring Session Data Redis

Open surya0420 opened this issue 4 years ago • 7 comments

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

surya0420 avatar Feb 04 '21 04:02 surya0420

I have the same issue. Are there any updates?

chrempl avatar Mar 21 '24 07:03 chrempl

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: @.***>

surya0420 avatar Mar 21 '24 08:03 surya0420

Do you have any link?

chrempl avatar Mar 21 '24 10:03 chrempl

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.

marcusdacoregio avatar Mar 21 '24 12:03 marcusdacoregio

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.

chrempl avatar Mar 21 '24 14:03 chrempl

At first, I see two alternatives to achieve such result:

  1. Use the SessionRepository bean in your HttpSessionListener implementation:
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);
  }
}
  1. Create your own implementation of SessionRepository that 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?

marcusdacoregio avatar Mar 21 '24 14:03 marcusdacoregio

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: @.***>

surya0420 avatar Mar 21 '24 15:03 surya0420