java - Vaadin 8 设置 session 超时

标签 java session vaadin vaadin8

如何在 Vaadin 8 中设置 session 超时?

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

最佳答案

tl;dr

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

VaadinSession.getCurrent().getSession().setMaxInactiveInterval ( ( int ) TimeUnit.MINUTES.toSeconds( 30 ) ) ;

以编程方式设置 session 超时

设置session超时是您的 web container 中的一项功能,你的Servlet引擎,比如Tomcat,Jetty等。Servlet规范将 Java 应用程序的这种行为定义为其 session 处理的一部分。

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

将时间限制指定为整数秒数。 TimeUnit 枚举可以方便地计算秒数,而无需求助于 “magic” numbers。 .

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

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

package com.basilbourque.example;

import javax.servlet.annotation.WebServlet;

import com.vaadin.annotations.Theme;
import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServlet;
import com.vaadin.server.VaadinSession;
import com.vaadin.ui.Button;
import com.vaadin.ui.Label;
import com.vaadin.ui.TextField;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;

import java.util.concurrent.TimeUnit;

/**
 * This UI is the application entry point. A UI may either represent a browser window
 * (or tab) or some part of an HTML page where a Vaadin application is embedded.
 * <p>
 * The UI is initialized using {@link #init(VaadinRequest)}. This method is intended to be
 * overridden to add component to the user interface and initialize non-component functionality.
 */
@Theme ( "mytheme" )
public class MyUI extends UI {

    @Override
    protected void init ( VaadinRequest vaadinRequest ) {
        // Set Session timeout programmatically. Overrides the default timeout configured for Servlet.
        VaadinSession.getCurrent().getSession().setMaxInactiveInterval( ( int ) TimeUnit.MINUTES.toSeconds( 45 ) );  // Setting timeout of 45 minutes = ( 45 * 60 ) seconds.

        final VerticalLayout layout = new VerticalLayout();

        final TextField name = new TextField();
        name.setCaption( "Type your name here:" );

        Button button = new Button( "Click Me" );
        button.addClickListener( e -> {
            layout.addComponent( new Label( "Thanks " + name.getValue()
                                                + ", it works!" ) );
        } );

        layout.addComponents( name , button );

        setContent( layout );
    }

    @WebServlet ( urlPatterns = "/*",
        name = "MyUIServlet",
        asyncSupported = true )
    @VaadinServletConfiguration ( ui = MyUI.class,
        productionMode = false )
    public static class MyUIServlet extends VaadinServlet {
    }
}

Servlet,不是 Vaadin

I am not using web.xml, which has been the place to set it in prior versions of the framework.

其实 session 超时是Servlet的东西,不是Vaadin特有的东西。而 web.xml 是 Servlet 的东西,而不是特定于 Vaadin 的东西。

参见:

How to set session timeout dynamically in Java web applications? 中进一步讨论.

关于java - Vaadin 8 设置 session 超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48741820/

相关文章:

java - 如何创建泛型类型 T 的实例

java - 以编程方式检索 Bean

java - 为什么 char 值在输出过程中会发生变化?

php - Codeigniter 3.x session 是安全的,是否使用已弃用的购物车类?

java - 当前没有 session 绑定(bind)到执行上下文

seo - Vaadin 14 Flow 和 SEO、PWA

java - Android:如何访问 Slave Sim

java - Vaadin 检查哪个组件具有焦点

grails - 为什么 Vaadin 而不是 Groovy 和 Grails?

c# - session 超时与定时器