Python 官方文档:入门教程 => 点击学习
目录这里我们只介绍SpringBoot2.0的session时间设置下面这种方式是设置24小时的或者使用第二种方式springboot设置session失效的几种方式如果是1.5.6
Duration转换字符串方式,默认为正,负以-开头,紧接着P,(字母不区分大小写)D :天 T:天和小时之间的分隔符 H :小时 M:分钟 S:秒 每个单位都必须是数字,且时分秒顺序不能乱。
例如PT10M,就是设置为10分钟,
错误的设置是下面这种方式,这种是不起效果的
server.servlet.session.timeout=30s
session1.setMaxInactiveInterval(60*60);//设置session一小时后失效
这里 可以在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;
}
}
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。
--结束END--
本文标题: springboot项目如何设置session的过期时间
本文链接: https://www.lsjlt.com/news/163928.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0