繁体   English   中英

Vaadin 8设置会话超时

[英]Vaadin 8 set session timeout

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

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

会话超时在web.xml中设置。

如果你没有,那么你需要创建一个。

如何在web.xml中设置会话超时(以秒为单位)?

当你似乎使用弹簧靴时,这可能适用于你

Spring Boot Java Config设置会话超时

TL;博士

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

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

以编程方式设置会话超时

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

Vaadin将Servlet会话包装在VaadinSession 因此,从Vaadin中提取常规Servlet会话作为WrappedSession ,然后调用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.      
)
;

以下是从Maven原型vaadin-archetype-application创建的完整示例Vaadin 8.5应用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应用程序中动态设置会话超时?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM