英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论