微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java – Vaadin 8设置会话超时

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

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

最佳答案
TL;博士

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

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

以编程方式设置会话超时

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

Vaadin将Servlet会话包装在VaadinSession内.因此,将Vaadin中的常规Servlet会话作为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原型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.
 * figured 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.

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

看到:

> javax.servlet.http.HttpSession::setMaxInactiveInterval(int interval)方法
> Servlet 3.1 specification(PDF文件)
> Servlet 4 specification

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

原文地址:https://www.jb51.cc/java/437475.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐