强制注销vaadin中的用户。如何显示消息以强制注销用户
问题描述:
在我的vaadin web应用程序中,管理员用户应该能够强制注销当前登录的用户。当用户强制注销时,应立即将其重定向到登录页面,并向用户显示错误消息,表明他已被强制注销。强制注销vaadin中的用户。如何显示消息以强制注销用户
到目前为止,我已经编写了以下代码,它们已成功将用户注销到登录页面。
try {
vaadinSession.lock(); //The session to be forcefully logged out
try {
vaadinSession.getUIs().forEach(ui -> {
if (ui.getPage() != null) {
ui.getPage().setLocation("");
ui.push();
Notification notification = new Notification("You have been forcefully logged out", Notification.Type.WARNING_MESSAGE);
notification.setDelayMsec(-1);
notification.show(ui.getPage());
ui.push();
}
});
} catch (Exception e) {
logger.error("Exception triggered when redirecting pages on forceDisconnect " + e.getLocalizedMessage(), e);
}
vaadinSession.close();
} finally {
vaadinSession.unlock();
}
但是,代码中显示的通知并未实际显示给用户。我认为这是因为当调用vaadinSession.close();
时,会创建一个新的Vaadin会话。如果我在新的vaadin会话中显示通知,我认为它会成功显示。
但是,我不知道如何在拨打vaadinSession.close();
后访问新会话。
有人可以指点我怎么做到这一点?
答
可能不理想,但以下是我如何最终完成这项工作。
在forceDisconnect()
方法中,在VaadinSession
vaadinSession.getSession().setAttribute("PrevSessionError", "You have been forcefully logged out");
的底层会话设置消息作为会话变量在登录视图的attach()
,示出了消息给用户,如果预先设定的变量被发现。
@Override
public void attach() {
super.attach();
Object previousSessionError = getSession().getSession().getAttribute("PrevSessionError");
if (previousSessionError != null) {
Notification notification = new Notification(previousSessionError.toString(), Notification.Type.ERROR_MESSAGE);
notification.setDelayMsec(-1);
notification.show(getUI().getPage());
getSession().getSession().setAttribute("PrevSessionError", null);
}
}
这样做的原因是即使VaadinSession
被更改时,底层会话也不会更改。我不知道这是否可靠,但这是我所能做到的。