Vaadin 7 + Spring - session listener

Hello,

I have Vaadin and Spring app.

I need Spring @Service to contain information about sessions ids.

First solution:

@Service
public class MySessionsServiceImpl implements HttpSessionListener, MySessionsService {
    private static List<String> totalActiveSessions = new ArrayList<String>();

    public int getTotalActiveSession() {
        return totalActiveSessions.size();
    }
    
    public List<String> getActiveSessions() {
        return totalActiveSessions;
    }

    @Override
    public void sessionCreated(HttpSessionEvent arg0) {
        totalActiveSessions.add(arg0.getSession().getId());
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent arg0) {
        totalActiveSessions.remove(arg0.getSession().getId());
    }
    
    @Override
    public String getRequestSessionId() {
        return RequestContextHolder.currentRequestAttributes().getSessionId();
    }
}

Problem is that the same session has in

arg0.getSession().getId()

and

RequestContextHolder.currentRequestAttributes().getSessionId()

different id.

Second solution

@Service
public class MySessionsServiceImpl implements SessionInitListener, SessionDestroyListener, MySessionsService {
    private static List<String> totalActiveSessions = new ArrayList<String>();

    public int getTotalActiveSession() {
        return totalActiveSessions.size();
    }
    
    public List<String> getActiveSessions() {
        return totalActiveSessions;
    }

      @Override
    public void sessionDestroy(SessionDestroyEvent event) {
        event.getSession().getSession().getId();
        
    }

      @Override
    public void sessionInit(SessionInitEvent event) throws ServiceException {
        event.getSession().getSession().getId();
    }
    
    @Override
    public String getRequestSessionId() {
        return RequestContextHolder.currentRequestAttributes().getSessionId();
    }
}

Problem is that
sessionInit(…)
and
sessionDestroy(…)
are never called.

How to solve this problem?

I found it.

I use SpringSecurity.
That’s the reason why arg0.getSession().getId() and RequestContextHolder.currentRequestAttributes().getSessionId() return different ids. Spring Security invalidates the existing session when the user authenticates and creates a new one, transferring the session data to it.