首页 > 其他分享 >SpringBoot设置Session失效时间

SpringBoot设置Session失效时间

时间:2024-05-23 10:51:01浏览次数:11  
标签:session SpringBoot Session 过期 long manager sessionTimeout context 失效

springboot的yml文件中设置session的过期时间

 #Session超时时间设置,单位是秒,默认是30分钟
  servlet:
    session:
      timeout: 3600s

当过期时间是大于1分钟的时候是没有什么问题的,但是如果设置过期时间小于1分钟,就会失效。

  servlet:
    session:
      timeout: 10

因为SpringBoot在TomcatServletWebServerFactory为session的过期时间做了优化。

Session的配置configureSession

private void configureSession(Context context) {
        long sessionTimeout = this.getSessionTimeoutInMinutes();
        context.setSessionTimeout((int)sessionTimeout);
        Boolean httpOnly = this.getSession().getCookie().getHttpOnly();
        if (httpOnly != null) {
            context.setUseHttpOnly(httpOnly);
        }

        if (this.getSession().isPersistent()) {
            Manager manager = context.getManager();
            if (manager == null) {
                manager = new StandardManager();
                context.setManager((Manager)manager);
            }

            this.configurePersistSession((Manager)manager);
        } else {
            context.addLifecycleListener(new TomcatServletWebServerFactory.DisablePersistSessionListener());
        }

    }

我们看到long sessionTimeout = this.getSessionTimeoutInMinutes();过期时间取自getSessionTimeoutInMinutes方法。

    private long getSessionTimeoutInMinutes() {
        Duration sessionTimeout = this.getSession().getTimeout();
        return this.isZeroOrLess(sessionTimeout) ? 0L : Math.max(sessionTimeout.toMinutes(), 1L);
    }

在这里对sessionTimeout 进行判断,首先判断是否为0或者更少isZeroOrLess方法

    private boolean isZeroOrLess(Duration sessionTimeout) {
        return sessionTimeout == null || sessionTimeout.isNegative() || sessionTimeout.isZero();
    }

sessionTimeout转换为分钟sessionTimeout.toMinutes()

    public long toMinutes() {
        return seconds / SECONDS_PER_MINUTE;
    }

    /**
     * Seconds per minute.
     */
    static final int SECONDS_PER_MINUTE = 60;

将转换后的值和1L进行比较

    public static long max(long a, long b) {
        return (a >= b) ? a : b;
    }

如果该值小于1分钟,则按1分钟算。
最后返回的是三目运算的结果。

标签:session,SpringBoot,Session,过期,long,manager,sessionTimeout,context,失效
From: https://www.cnblogs.com/leepandar/p/18207896

相关文章

  • SpringBoot发送邮件(二)
    不需要在application.yml中进行配置jdk:8POM依赖<!--发送邮件--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-mail</artifactId></dependency>工具类/***邮件发送消息*/@UtilityCl......
  • SpringBoot发送邮件(三)
    jdk:17springboot:3.0+POM依赖<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-mail</artifactId></dependency><dependency><groupId>javax.mail</group......
  • SpringBoot动态定时任务
    其实SchedulingConfigurer实现方法很简单,只需要实现SchedulingConfigurer并重写configureTasks方法,在启动类必须加上@EnableScheduling注解即可。@Configuration@EnableScheduling@Slf4jpublicclassRuleTaskimplementsSchedulingConfigurer{privatevolatileSch......
  • SpringBoot发送邮件(一)
    POM依赖<!--发送邮件--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-mail</artifactId></dependency>邮箱配置mail:host:smtp.partner.outlook.cn#发送邮件服务器user......
  • 【SpringBoot】服务停止数据源的关闭时机
    1 前言微服务中我们会用到数据源,数据源中其实就是管理我们的数据库连接,对于数据库而言,连接数是很珍贵的资源,所以释放无用或者长时间空闲的连接显得很重要。那么对于微服务比如我们的SpringBoot当服务启动的时候会初始化数据源,那么停止的时候,是如何关闭数据源,释放连接的呢?这......
  • 在springboot项目中,打包本地的外部jar包,到运行的jar包中
    1、配置依赖<dependency><groupId>com.genesyslab</groupId><artifactId>genesyslab</artifactId><version>1.0.0</version><scope>system</scope><systemPath>${project.basedir}/src/main/re......
  • springboot开启热部署
    一、依赖在SpringBoot中启用热部署通常涉及使用SpringBootDevTools依赖和配置。以下是如何在SpringBoot项目中启用热部署的步骤:在pom.xml中添加SpringBootDevTools依赖:<dependencies><!--其他依赖--><dependency><groupId>org.springframework.b......
  • springboot中执行完某些逻辑后,才算bean加载完,applicationContext才加载完毕
    核心思想实现InitializingBean接口,重写afterPropertiesSet方法范例代码importlombok.extern.slf4j.Slf4j;importorg.springframework.beans.factory.InitializingBean;importorg.springframework.stereotype.Component;@Slf4j@ComponentpublicclassDemoimplementsI......
  • LLM-文心一言:MySQL索引失效的场景
    MySQL中的索引是加速数据检索的关键工具,但在某些情况下,即使存在索引,MySQL也可能不会使用它,这被称为“索引失效”。以下是MySQL索引可能失效的一些常见场景:查询条件中使用函数或表达式:如果在查询条件中对索引列使用了函数或表达式,MySQL可能无法使用该索引。例如,WHEREYEAR(date_......
  • springboot集成logback-spring.xml日志文件
    logback-spring.xml:<!--Logbackconfiguration.Seehttp://logback.qos.ch/manual/index.html--><configurationscan="true"scanPeriod="10seconds"><springPropertyscope="context"name="logLevel"s......