我的情况与您类似,我需要显示有关所有会话的多个信息。我所做的是创建了自己的 Servlet,它使用静态 ConcurrentHashmap 扩展 VaadinServlet 以保存我的会话信息,并使用 SessionDestroyListener 在注销时从地图中删除任何信息。最初我还有一个 SessionInitListener ,我在其中添加了 hashmap 中的信息,但我意识到我在身份验证后只有用户信息,所以我将这部分移到了处理登录的页面。
我想你可以做类似的事情,或者至少这应该让你开始:
public class SessionInfoServlet extends VaadinServlet {
private static final ConcurrentHashMap<User, VaadinSession> userSessionInfo = new ConcurrentHashMap<>();
// this could be called after login to save the session info
public static void saveUserSessionInfo(User user, VaadinSession session) {
VaadinSession oldSession = userSessionInfo.get(user);
if(oldSession != null){
// close the old session
oldSession.close();
}
userSessionInfo.put(user, session);
}
public static Map<User, VaadinSession> getUserSessionInfos() {
// access the cache if we need to, otherwise useless and removable
return userSessionInfo;
}
@Override
protected void servletInitialized() throws ServletException {
super.servletInitialized();
// register our session destroy listener
SessionLifecycleListener sessionLifecycleListener = new SessionLifecycleListener();
getService().addSessionDestroyListener(sessionLifecycleListener);
}
private class SessionLifecycleListener implements SessionDestroyListener {
@Override
public void sessionDestroy(SessionDestroyEvent event) {
// remove saved session from cache, for the user that was stored in it
userSessionInfo.remove(event.getSession().getAttribute("user"));
}
}
}