springboot項目如何設置session的過期時間
更新時間:2022年01月27日 08:57:40 作者:yyk的萌
這篇文章主要介紹了springboot項目如何設置session的過期時間,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
這里我們只介紹springboot2.0的session時間設置
Duration轉換字符串方式,默認為正,負以-開頭,緊接著P,(字母不區(qū)分大小寫)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; } }
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
ShardingSphere數據分片算法及測試實戰(zhàn)
這篇文章主要為大家介紹了ShardingSphere數據分片算法及測試實戰(zhàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-03-03MyBatis自定義TypeHandler如何解決字段映射問題
這篇文章主要介紹了MyBatis自定義TypeHandler如何解決字段映射問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12restTemplate發(fā)送get與post請求并且?guī)祮栴}
這篇文章主要介紹了restTemplate發(fā)送get與post請求并且?guī)祮栴},具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07