Vaadin关闭另一个浏览器/标签/系统中同一用户的UI [英] Vaadin close UI of same user in another browser/tab/system
问题描述
我正在Vaadin 7中做一个项目.因此,我需要实现以下类似的登录名.
I'm doing a project in Vaadin 7. In that I need to implement something like below for the login.
用户'A'登录到系统'1'.然后他再次登录到另一个系统"2".现在,我想知道如何在系统"1"上关闭用户界面.
A user 'A' is logged in to a system '1'. And again he logs into another system '2'. Now I want to know how to close the UI on the system '1'.
我尝试了一些操作,并且可以关闭UI(如果是同一浏览器).但是,对于不同的系统/浏览器.我不知道.
I tried something and can able to close the UI, If it is the same browser. But, for different systems/browser. I don't have any idea.
我的代码:
private void closeUI(String attribute) {
for (UI ui : getSession().getUIs()) {
if(ui.getSession().getAttribute(attribute) != null)
if(ui.getSession().getAttribute(attribute).equals(attribute))
ui.close();
}
}
有人可以帮我吗?
推荐答案
我遇到的情况与您类似,需要显示有关所有会话的多个信息.我所做的是创建了自己的Servlet,使用静态ConcurrentHashmap扩展了VaadinServlet来保存我的会话信息,并使用SessionDestroyListener退出后从地图上删除了任何信息.最初,我还有一个SessionInitListener,我在哈希图中添加了信息,但是我意识到身份验证后只有用户信息,所以我将这一部分移到了处理登录的页面上.
I have a situation similar to your where I need to display several info regarding all sessions. What I did was I created my own Servlet extending the VaadinServlet with a static ConcurrentHashmap to save my sessions info, and a SessionDestroyListener to remove any info from the map upon logout. Initially I also had a SessionInitListener where I added the info in the hashmap but I realized I only had the user information after authentication so I moved this part to the page handling the login.
我想您可以做类似的事情,或者至少这应该使您入门:
I guess you could do something similar, or at least this should get you started:
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"));
}
}
}
这篇关于Vaadin关闭另一个浏览器/标签/系统中同一用户的UI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!