简体   繁体   English

如何在Java Web应用程序中动态设置会话超时?

[英]How to set session timeout dynamically in Java web applications?

I need to give my user a web interface to change the session timeout interval. 我需要为我的用户提供一个Web界面来更改会话超时间隔。 So, different installations of the web application would be able to have different timeouts for their sessions, but their web.xml cannot be different. 因此,Web应用程序的不同安装可以为其会话设置不同的超时,但是它们的web.xml不能有所不同。

Is there a way to set the session timeout programatically, so that I could use, say, ServletContextListener.contextInitialized() to read the configured interval and set it upon application startup? 有没有办法以编程方式设置会话超时,以便我可以使用ServletContextListener.contextInitialized()来读取配置的间隔并在应用程序启动时设置它?

Thanks a lot. 非常感谢。

Instead of using a ServletContextListener, use a HttpSessionListener . 不使用ServletContextListener,而是使用HttpSessionListener

In the sessionCreated() method, you can set the session timeout programmatically: sessionCreated()方法中,您可以以编程方式设置会话超时

public class MyHttpSessionListener implements HttpSessionListener {

  public void sessionCreated(HttpSessionEvent event){
      event.getSession().setMaxInactiveInterval(15 * 60); // in seconds
  }

  public void sessionDestroyed(HttpSessionEvent event) {}

}

And don't forget to define the listener in the deployment descriptor : 并且不要忘记在部署描述符中定义侦听器

<webapp>
...      
  <listener>                                  
    <listener-class>com.example.MyHttpSessionListener</listener-class>
  </listener>
</webapp>

(or since Servlet version 3.0 you can use @WebListener annotation instead). (或者从Servlet 3.0版起,您可以使用@WebListener注释)。


Still, I would recommend creating different web.xml files for each application and defining the session timeout there: 不过,我建议为每个应用程序创建不同的web.xml文件,并在那里定义会话超时:

<webapp>
...
  <session-config>
    <session-timeout>15</session-timeout> <!-- in minutes -->
  </session-config>
</webapp>

Is there a way to set the session timeout programatically 有没有办法以编程方式设置会话超时

There are basically three ways to set the session timeout value: 基本上有三种方法来设置会话超时值:

But note that the later option sets the timeout value for the current session, this is not a global setting. 但请注意,后一个选项设置当前会话的超时值,这不是全局设置。

As another anwsers told, you can change in a Session Listener. 正如另一个告诉你的那样,你可以改变一个会话监听器。 But you can change it directly in your servlet, for example. 但是,您可以直接在servlet中更改它。

getRequest().getSession().setMaxInactiveInterval(123);
I need to give my user a web interface to change the session timeout interval. 我需要为我的用户提供一个Web界面来更改会话超时间隔。 So, different installations of the web application would be able to have different timeouts for their sessions, but their web.xml cannot be different. 因此,Web应用程序的不同安装可以为其会话设置不同的超时,但是它们的web.xml不能有所不同。

your question is simple, you need session timeout interval should be configurable at run time and configuration should be done through web interface and there shouldn't be overhead of restarting the server. 您的问题很简单,您需要会话超时间隔应该在运行时可配置,配置应该通过Web界面完成,不应该有重启服务器的开销。

I am extending Michaels answer to address your question. 我正在扩展迈克尔斯的答案来解决你的问题。

Logic : You need to store configured value in either .properties file or to database. 逻辑 :您需要将配置的值存储在.properties文件或数据库中。 On server start read that stored value and copy to a variable use that variable until server is UP. 在服务器上启动读取该存储的值并复制到变量使用该变量,直到服务器为UP。 As config is updated update variable also. 由于config也更新了更新变量。 Thats it. 而已。

Expaination Expaination

In MyHttpSessionListener class 1. create a static variable with name globalSessionTimeoutInterval. 在MyHttpSessionListener类中1.创建一个名为globalSessionTimeoutInterval的静态变量。

  1. create a static block(executed for only for first time of class is being accessed) and read timeout value from config.properties file and set value to globalSessionTimeoutInterval variable. 创建一个静态块(仅在第一次访问类时执行)并从config.properties文件读取超时值并将值设置为globalSessionTimeoutInterval变量。

  2. Now use that value to set maxInactiveInterval 现在使用该值设置maxInactiveInterval

  3. Now Web part ie, Admin configuration page 现在Web部分即管理员配置页面

    a. 一个。 Copy configured value to static variable globalSessionTimeoutInterval. 将配置的值复制到静态变量globalSessionTimeoutInterval。

    b. Write same value to config.properties file. 将相同的值写入config.properties文件。 (consider server is restarted then globalSessionTimeoutInterval will be loaded with value present in config.properties file) (考虑重新启动服务器,然后将使用config.properties文件中存在的值加载globalSessionTimeoutInterval)

  4. Alternative .properties file OR storing it into database. 备用.properties文件或将其存储到数据库中。 Choice is yours. 选择是你的。

Logical code for achieving the same 实现相同的逻辑代码

public class MyHttpSessionListener implements HttpSessionListener 
{
  public static Integer globalSessionTimeoutInterval = null;

  static
  {
      globalSessionTimeoutInterval =  Read value from .properties file or database;
  }
  public void sessionCreated(HttpSessionEvent event)
  {
      event.getSession().setMaxInactiveInterval(globalSessionTimeoutInterval);
  }

  public void sessionDestroyed(HttpSessionEvent event) {}

}

And in your Configuration Controller or Configuration servlet 在配置控制器或配置servlet中

String valueReceived = request.getParameter(timeoutValue);
if(valueReceived  != null)
{
    MyHttpSessionListener.globalSessionTimeoutInterval = Integer.parseInt(timeoutValue);
          //Store valueReceived to config.properties file or database
}

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

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