【问题标题】:Forcing logout of a user in vaadin. How to show a message to force logged out user在 vaadin 中强制用户注销。如何显示消息以强制注销用户
【发布时间】:2015-09-28 05:29:38
【问题描述】:

在我的 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(); 后如何访问新会话。

有人可以告诉我如何实现这一目标吗?

【问题讨论】:

    标签: java vaadin vaadin7


    【解决方案1】:

    可能并不理想,但以下是我最终完成的方法。

    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 发生更改,基础会话也不会更改。我不知道这是否可靠,但这是我能做的。

    【讨论】:

      猜你喜欢
      • 2013-12-04
      • 2014-11-12
      • 1970-01-01
      • 2019-04-30
      • 2012-05-11
      • 2017-11-18
      • 2018-11-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多