Vaadin 14.9 – 会话销毁事件重定向

huangapple go评论59阅读模式
英文:

Vaadin 14.9 - Redirect on Session Destroy Event

问题

我在Vaadin上遇到会话超时的问题。具体来说,我试图在SessionDestroyEvent(由server.servlet.session.timeout触发)中将用户重定向到一个静态页面。这可能吗?我已经尝试在实现VaadinServiceInitListener的addSessionDestroyListener方法中使用VaadinServletResponse.sendRedirect,但似乎对用户端没有影响。

非常感谢您的帮助,
Giuseppe。

英文:

I'm struggling with session timeout on Vaadin. In particular I'm trying to redirect user to a static page on SessionDestroyEvent (triggered by server.servlet.session.timeout). Is that possible ? I've already tried with VaadinServletResponse.sendRedirect in addSessionDestroyListener method implementing VaadinServiceInitListener, but it seems there is no effect on the user side.

Many thanks for your help,
Giuseppe.

答案1

得分: 2

No, that won't work. Once the SessionDestroyEvent is fired, the session has already been destroyed. As you can see from the JavaDoc:

Event fired when a Vaadin service session is no longer in use.
``` What you can do instead is provide a session expired URL in customized system messages:
```java
public class CustomInitServiceListener implements VaadinServiceInitListener {
    @Override
    public void serviceInit(ServiceInitEvent event) {
        event.getSource().setSystemMessagesProvider(new SystemMessagesProvider() {
            @Override
            public SystemMessages getSystemMessages(SystemMessagesInfo systemMessagesInfo) {
                CustomizedSystemMessages messages = new CustomizedSystemMessages();
                messages.setSessionExpiredCaption("Session expired");
                messages.setSessionExpiredMessage("Take note of any unsaved data, and click here or press ESC key to continue.");
                messages.setSessionExpiredURL("session-expired.html");
                messages.setSessionExpiredNotificationEnabled(true);
                return messages;
            }
        });
    };
};

The browser will be redirected to the URL after the session expired notification is shown. If you want to redirect without showing the notification, just disable the notification. See more in the docs: https://vaadin.com/docs/latest/advanced/customize-system-messages

英文:

No, that won't work. Once the SessionDestroyEvent is fired, the session has already been destroyed. As you can see from the JavaDoc:

Event fired when a Vaadin service session is no longer in use.

What you can do instead is provide a session expired URL in customized system messages:

public class CustomInitServiceListener implements VaadinServiceInitListener {
    @Override
    public void serviceInit(ServiceInitEvent event) {
        event.getSource().setSystemMessagesProvider(new SystemMessagesProvider() {
            @Override
            public SystemMessages getSystemMessages(SystemMessagesInfo systemMessagesInfo) {
                CustomizedSystemMessages messages = new CustomizedSystemMessages();
                messages.setSessionExpiredCaption("Session expired");
                messages.setSessionExpiredMessage("Take note of any unsaved data, and click here or press ESC key to continue.");
                messages.setSessionExpiredURL("session-expired.html");
                messages.setSessionExpiredNotificationEnabled(true);
                return messages;
            }
        });
    };
};

The browser will be redirected to the URL after the session expired notification is shown. If you want to redirect without showing the notification, just disable the notification. See more in the docs: https://vaadin.com/docs/latest/advanced/customize-system-messages

huangapple
  • 本文由 发表于 2023年3月21日 00:12:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75792738.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定