springboot項(xiàng)目如何設(shè)置session的過期時(shí)間
這里我們只介紹springboot2.0的session時(shí)間設(shè)置
Duration轉(zhuǎn)換字符串方式,默認(rèn)為正,負(fù)以-開頭,緊接著P,(字母不區(qū)分大小寫)D :天 T:天和小時(shí)之間的分隔符 H :小時(shí) M:分鐘 S:秒 每個(gè)單位都必須是數(shù)字,且時(shí)分秒順序不能亂。
例如PT10M,就是設(shè)置為10分鐘,
下面這種方式是設(shè)置24小時(shí)的
錯(cuò)誤的設(shè)置是下面這種方式,這種是不起效果的
server.servlet.session.timeout=30s
或者使用第二種方式
session1.setMaxInactiveInterval(60*60);//設(shè)置session一小時(shí)后失效
springboot設(shè)置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); } //設(shè)置session過期時(shí)間 @Bean public EmbeddedServletContainerCustomizer containerCustomizer() { return new EmbeddedServletContainerCustomizer() { public void customize(ConfigurableEmbeddedServletContainer container) { container.setSessionTimeout(7200);// 單位為S } }; } }
第二個(gè)
還可以設(shè)置
application.yml
server: port: 8081 servlet: session: timeout: 60s
第三個(gè)
@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; } }
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
ShardingSphere數(shù)據(jù)分片算法及測試實(shí)戰(zhàn)
這篇文章主要為大家介紹了ShardingSphere數(shù)據(jù)分片算法及測試實(shí)戰(zhàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03MyBatis自定義TypeHandler如何解決字段映射問題
這篇文章主要介紹了MyBatis自定義TypeHandler如何解決字段映射問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12使用javaweb項(xiàng)目對數(shù)據(jù)庫增、刪、改、查操作的實(shí)現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于使用javaweb項(xiàng)目對數(shù)據(jù)庫增、刪、改、查操作的實(shí)現(xiàn)方法,avaWeb是指使用Java語言進(jìn)行Web應(yīng)用程序開發(fā)的技術(shù),可以利用Java編寫一些動(dòng)態(tài)網(wǎng)頁、交互式網(wǎng)頁、企業(yè)級應(yīng)用程序等,需要的朋友可以參考下2023-07-07restTemplate發(fā)送get與post請求并且?guī)?shù)問題
這篇文章主要介紹了restTemplate發(fā)送get與post請求并且?guī)?shù)問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07springboot如何連接兩個(gè)數(shù)據(jù)庫(多個(gè))
這篇文章主要介紹了springboot如何連接兩個(gè)數(shù)據(jù)庫(多個(gè)),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01