0

so I'm using the spring-session project and I want to know if it is possible to autowire the HttpSessionManager bean? I can see in the users example you are getting it from the request together with the SessionRepository:

    HttpSessionManager sessionManager =
            (HttpSessionManager) req.getAttribute(HttpSessionManager.class.getName());
    SessionRepository<Session> repo =
            (SessionRepository<Session>) req.getAttribute(SessionRepository.class.getName());

However, I want to access it from a service near the db layer and because I don't think it is a good design practice to pass the request down to the service I tried to autowire it but it doesn't find a bean of this type. The SessionRepository can be autowired fine because I have defined the bean in my configuration. I also tried to get it using the RequestContextHolder but then the getSessionIds methods always returns empty map, so I end up creating a new session all the time. Here's my whole method:

@Override
public Session getCurrentSession() {

    HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();

    HttpSessionManager sessionManager =
        (HttpSessionManager) request.getAttribute(HttpSessionManager.class.getName());

    final Map<String, String> sessionIds = sessionManager.getSessionIds(request);

    if (sessionIds != null) {

        for (Map.Entry<String, String> e : sessionIds.entrySet()) {
            final Session session = sessionRepository.getSession(e.getValue());
            if (session != null) {
                return session;
            }
        }
    }

    Session session = sessionRepository.createSession();

    sessionRepository.save(session);

    return session;
}
Petar Tahchiev
  • 4,336
  • 4
  • 35
  • 48

1 Answers1

0

My guess is that the RequestContextHolder is capturing the HttpServletRequest before the SessionRepositoryFilter is invoked. That means that the request will not yet be wrapped.

By default the EnableRedisHttpSession configuration does not expose CookieHttpSessionStrategy as a Bean. This is necessary in order to allow users to override the SessionStrategy and supporting older versions of Spring (newer versions of Spring support @Conditional). If you wish to expose CookieHttpSessionStrategy as a Bean, then you can add the following to your configuration:

@Bean
public CookieHttpSessionStrategy sessionStragegy() {
    return new CookieHttpSessionStrategy();
}

After thinking about it some I may be able to expose it in future versions. I have created gh-spring-session-75 to address it.

Rob Winch
  • 21,440
  • 2
  • 59
  • 76
  • Hi Rob, I tried adding the session strategy as a bean, and now I can autowire it, but when I call `sessionStrategy.getRequestedSessionId(request);` with the request from `RequestContextHolder` it always returns null. I'm not sure if `RequestContetHolder` passes the cookies.... – Petar Tahchiev Dec 08 '14 at 20:18
  • Hi Rob, the cookies are passed. The first request comes with some sessionID but because the server was restarted the repository has no session so I end up creating a new session using the repository (look at the last 3 lines of my method). However this new session is not saved on the cookie, so the next request the cookie session ID is the same and the repository again cannot find it and I create another session. Do you know how to set the newly created session on the `CookieHttpSessionStrategy`? – Petar Tahchiev Dec 09 '14 at 23:23