springboot项目如何设置session的过期时间
作者:yyk的萌 时间:2022-12-31 23:45:27
这里我们只介绍springboot2.0的session时间设置
Duration转换字符串方式,默认为正,负以-开头,紧接着P,(字母不区分大小写)D :天 T:天和小时之间的分隔符 H :小时 M:分钟 S:秒 每个单位都必须是数字,且时分秒顺序不能乱。
例如PT10M,就是设置为10分钟,
下面这种方式是设置24小时的
错误的设置是下面这种方式,这种是不起效果的
server.servlet.session.timeout=30s
或者使用第二种方式
session1.setMaxInactiveInterval(60*60);//设置session一小时后失效
springboot设置session失效的几种方式
如果是1.5.6版本
这里 可以在application中加上bean文件
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class DemoApplication {undefined
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
//设置session过期时间
@Bean
public EmbeddedServletContainerCustomizer containerCustomizer() {
return new EmbeddedServletContainerCustomizer() {
public void customize(ConfigurableEmbeddedServletContainer container) {
container.setSessionTimeout(7200);// 单位为S
}
};
}
}
第二个
还可以设置
application.yml
server:
port: 8081
servlet:
session:
timeout: 60s
第三个
@RestController
public class HelloController {undefined
@PostMapping("test")
public Integer getTest(@RequestParam("nyy")String nn, HttpServletRequest httpServletRequest ){
HttpSession session = httpServletRequest.getSession();
session.setMaxInactiveInterval(60);
int maxInactiveInterval = session.getMaxInactiveInterval();
long lastAccessedTime = session.getLastAccessedTime();
return maxInactiveInterval;
}
}
来源:https://blog.csdn.net/weixin_44235861/article/details/105350031
标签:springboot,session,过期时间
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#简单输出日历的方法
2023-11-13 05:56:10
![](https://img.aspxhome.com/file/2023/3/68083_0s.jpg)
C#使用log4net记录日志
2022-12-04 03:08:35
![](https://img.aspxhome.com/file/2023/8/106448_0s.jpg)
MyBatis配置的应用与对比jdbc的优势
2023-08-27 07:03:47
![](https://img.aspxhome.com/file/2023/1/58281_0s.png)
Java数据类型分类与基本数据类型转换
2023-08-10 08:33:37
![](https://img.aspxhome.com/file/2023/0/72910_0s.png)
Mybatis-plus自定义SQL注入器查询@TableLogic逻辑删除后的数据详解
2023-04-09 22:36:45
解析springboot整合谷歌开源缓存框架Guava Cache原理
2023-11-07 13:24:23
![](https://img.aspxhome.com/file/2023/0/62030_0s.png)
C# ODP.NET 调用Oracle函数返回值时报错的一个解决方案
2021-10-03 01:54:19
![](https://img.aspxhome.com/file/2023/2/111092_0s.png)
C#操作CSV文件类实例
2023-04-29 13:49:44
springboot-dubbo cannot be cast to问题及解决
2022-06-27 14:46:51
![](https://img.aspxhome.com/file/2023/0/75470_0s.png)
修改maven本地仓库路径的方法
2022-08-09 13:44:16
C# 9.0新特性——只初始化设置器
2023-03-19 02:31:45
C#强制转换和尝试转换的方法
2021-10-10 10:48:23
Java 动态模拟操作系统进程调度算法
2023-04-10 15:09:20
![](https://img.aspxhome.com/file/2023/9/125669_0s.png)
C#实现12306自动登录的方法
2023-11-07 13:20:27
![](https://img.aspxhome.com/file/2023/0/100100_0s.jpg)
c#调用存储过程实现登录界面详解
2023-01-02 22:03:07
![](https://img.aspxhome.com/file/2023/5/80125_0s.gif)
详解在Java的Struts2框架中配置Action的方法
2023-08-01 11:01:40
深入理解C++中public、protected及private用法
2023-07-02 11:30:17
基于WPF实现简单放大镜效果
2022-02-15 23:19:12
![](https://img.aspxhome.com/file/2023/3/111123_0s.png)
C#中的委托介绍
2023-07-27 13:26:23
![](https://img.aspxhome.com/file/2023/6/77296_0s.png)
JUC循环屏障CyclicBarrier与CountDownLatch区别详解
2021-11-17 10:40:08
![](https://img.aspxhome.com/file/2023/0/88200_0s.png)