Vaadin 8设置会话超时

Sti*_*Cat 6 java session vaadin vaadin8

如何在Vaadin 8中设置会话超时?

我没有使用web.xml,它已经成为在框架的早期版本中设置它的地方.

Bas*_*que 3

太长了;博士

\n\n

int从包装中提取后,您可以将标准 Servlet session\xe2\x80\x99s 超时设置为整秒数VaadinSession

\n\n
VaadinSession.getCurrent().getSession().setMaxInactiveInterval ( ( int ) TimeUnit.MINUTES.toSeconds( 30 ) ) ;\n
Run Code Online (Sandbox Code Playgroud)\n\n

以编程方式设置会话超时

\n\n

设置会话超时是Web 容器、Servlet 引擎(例如 Tomcat、Jetty 等)中的一项功能。Servlet规范为 Java 应用程序定义了此行为,作为其会话处理的一部分。

\n\n

Vaadin 将 Servlet 会话包装在VaadinSession. 因此,从 Vaadin 中提取常规 Servlet 会话作为WrappedSession,然后调用该setMaxInactiveInterval方法来设置过期时间。

\n\n

将时间限制指定为整秒数。枚举TimeUnit可以方便地计算秒数,而无需借助\xe2\x80\x9cmagic\xe2\x80\x9d 数字

\n\n
VaadinSession               // Wraps a standard Servlet session.\n.getCurrent()               // Access the current user\xe2\x80\x99s session.\n.getSession()               // Access the wrapped standard Servlet session.\n.setMaxInactiveInterval(    // Set the timeout.\n    ( int )                 // Cast a `long` to an `int`.\n    TimeUnit                // The `TimeUnit` enum is more self-documenting than using a literal integer number.\n    .MINUTES                // Here we set a half hour, 30 minutes.\n    .toSeconds( 30 )        // Set a number of whole seconds.      \n)\n;\n
Run Code Online (Sandbox Code Playgroud)\n\n

以下是从 Maven 原型创建的完整示例 Vaadin 8.5 应用程序vaadin-archetype-application。我们在方法的开头添加了一行init

\n\n
package com.basilbourque.example;\n\nimport javax.servlet.annotation.WebServlet;\n\nimport com.vaadin.annotations.Theme;\nimport com.vaadin.annotations.VaadinServletConfiguration;\nimport com.vaadin.server.VaadinRequest;\nimport com.vaadin.server.VaadinServlet;\nimport com.vaadin.server.VaadinSession;\nimport com.vaadin.ui.Button;\nimport com.vaadin.ui.Label;\nimport com.vaadin.ui.TextField;\nimport com.vaadin.ui.UI;\nimport com.vaadin.ui.VerticalLayout;\n\nimport java.util.concurrent.TimeUnit;\n\n/**\n * This UI is the application entry point. A UI may either represent a browser window\n * (or tab) or some part of an HTML page where a Vaadin application is embedded.\n * <p>\n * The UI is initialized using {@link #init(VaadinRequest)}. This method is intended to be\n * overridden to add component to the user interface and initialize non-component functionality.\n */\n@Theme ( "mytheme" )\npublic class MyUI extends UI {\n\n    @Override\n    protected void init ( VaadinRequest vaadinRequest ) {\n        // Set Session timeout programmatically. Overrides the default timeout configured for Servlet.\n        VaadinSession.getCurrent().getSession().setMaxInactiveInterval( ( int ) TimeUnit.MINUTES.toSeconds( 45 ) );  // Setting timeout of 45 minutes = ( 45 * 60 ) seconds.\n\n        final VerticalLayout layout = new VerticalLayout();\n\n        final TextField name = new TextField();\n        name.setCaption( "Type your name here:" );\n\n        Button button = new Button( "Click Me" );\n        button.addClickListener( e -> {\n            layout.addComponent( new Label( "Thanks " + name.getValue()\n                                                + ", it works!" ) );\n        } );\n\n        layout.addComponents( name , button );\n\n        setContent( layout );\n    }\n\n    @WebServlet ( urlPatterns = "/*",\n        name = "MyUIServlet",\n        asyncSupported = true )\n    @VaadinServletConfiguration ( ui = MyUI.class,\n        productionMode = false )\n    public static class MyUIServlet extends VaadinServlet {\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

Servlet,而不是 Vaadin

\n\n
\n

我没有使用 web.xml,它是在框架的早期版本中设置它的地方。

\n
\n\n

实际上,会话超时是 Servlet 的事情,而不是 Vaadin 特定的事情。这web.xml是 Servlet 的东西,而不是 Vaadin 特定的东西。

\n\n

看:

\n\n\n\n

进一步讨论如何在 Java Web 应用程序中动态设置会话超时?

\n