Vaadin 8如何设置会话超时时间

5

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

我没有使用web.xml,而这在该框架的先前版本中是设置会话超时时间的地方。

2个回答

3

简述

您可以通过从包装的VaadinSession中提取,将标准Servlet会话的超时时间设置为整个秒数的int数字。

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

以编程方式设置会话超时

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

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

将时间限制指定为整数秒数。使用TimeUnit枚举可以方便地计算秒数,而不必诉诸“魔法”数字

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.      
)
;

这是一个完整的Vaadin 8.5应用程序示例,由Maven原型 vaadin-archetype-application 创建。我们在 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

我没有使用web.xml,在之前的版本中它是设置它的地方。

实际上,会话超时是一个Servlet的事情,而不是Vaadin特有的事情。而且,web.xml是一个Servlet的东西,而不是Vaadin特有的东西。

参见:

更多讨论请参见 如何在Java Web应用程序中动态设置会话超时时间?


谢谢。我认为在你的例子中,30仍然是一个神奇的数字,最好使用一个常量SESSION_TIMEOUT_HALF_HOUR或类似的东西。但无论如何,还是谢谢! - Stimpson Cat

2

这个文件应该放在哪里?我正在使用Spring Boot。 - Stimpson Cat
@StimpsonCat 或者,您可以通过在您的 init 方法中调用 VaadinSession.getCurrent().getSession().setMaxInactiveInterval(numberOfSeconds); 来以编程方式设置它。 - Morfic
虽然这个链接可能回答了问题,但最好在此处包含答案的基本部分并提供参考链接。如果链接页面更改,仅链接的答案可能会失效。- [来自审查] (/ review / low-quality-posts / 18821037) - grizzthedj
@grizzthedj同意使用外部链接,但这些链接也是stackoverflow的帖子。如果用户提出了更清晰的问题,我本可以将其标记为spring boot相关问题的重复。 - André Schild
问题一直很清楚。但是评论没有帮助,因为它们不起作用。 - Stimpson Cat

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接