【问题标题】:Vaadin 8 set session timeoutVaadin 8 设置会话超时
【发布时间】:2018-07-22 08:21:30
【问题描述】:

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

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

【问题讨论】:

    标签: java session vaadin vaadin8


    【解决方案1】:

    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.
     * <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 特定的东西。

    见:

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

    【讨论】:

    • 谢谢。我认为 30 在您的示例中仍然是一个神奇的数字,最好使用常量 SESSION_TIMEOUT_HALF_HOUR 或类似的东西。不过还是谢谢!
    【解决方案2】:

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

    如果您没有,则需要创建一个。

    How do i set session timeout in seconds in web.xml?

    由于您似乎使用弹簧靴,那么这可能适用于您

    Spring Boot Java Config Set Session Timeout

    【讨论】:

    • 这个文件应该放在哪里?我正在使用弹簧靴
    • @StimpsonCat 或者,您可以通过在init 方法中调用VaadinSession.getCurrent().getSession().setMaxInactiveInterval(numberOfSeconds); 以编程方式设置它。
    • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接答案可能会失效。 - From Review
    • @grizzthedj 同意外部链接,但这些也是 stackoverflow 帖子。如果用户有更明确的问题,我可以将其标记为 spring boot 内容的副本。
    • 问题一直很清楚。但是 cmets 没有帮助,因为它们不起作用
    猜你喜欢
    • 2013-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多