在我的vaadin Web应用程序中,管理员用户应该能够强制注销当前登录的用户。当用户被强制注销时,他应该立即被重定向到登录页面,并且应该向用户显示他已被强制注销的错误消息。
到目前为止,我编写了以下代码,成功将用户注销到登录页面。
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();
后,我不知道如何访问新会话。
有人能指出我如何实现这个目标吗?
答案 0 :(得分:0)
可能不太理想,但以下是我最终完成这项工作的方法。
在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
发生更改,基础会话也不会更改。我不知道这是否可靠,但这就是我所能做的。