springboot2中session超時(shí),退到登錄頁面方式
session超時(shí)退到登錄頁面
最近發(fā)現(xiàn)使用的工程居然沒有session超時(shí)機(jī)制,功能太欠缺了,現(xiàn)在把追加方法分享出來,里面有一些坑,大家自由使用。
1、首先在springboot中追加配置session的超時(shí)時(shí)間
注意springboot2的寫法發(fā)生了改變
springboot2寫法
server: ? servlet: ? ? session: ? ? ? timeout: 1800s
springboot1寫法
server: ? ? session: ? ? ? timeout: 1800s
2、登錄成功接口中把用戶信息追加session中
public ResponseEntity loginGo(HttpServletRequest request,String userName, String password) { ?? ?// 此處省略若干 ?? ?HttpSession session = request.getSession(true); ?? ?session.setAttribute("username", user.getUserRemark()); }
3、在WebMvcConfig中配置攔截規(guī)則和重定向規(guī)則
@Configuration public class WebMvcConfig implements WebMvcConfigurer { ? ? @Autowired ? ? private LoginInterceptor loginInterceptor; ? ? @Override ? ? public void addViewControllers(ViewControllerRegistry registry) { ? ? ? ? registry.addViewController("/login").setViewName("login"); ? ? ? ? registry.addViewController("/loginOverTime").setViewName("loginOverTime"); ? ? } ? ? @Override ? ? public void addInterceptors(InterceptorRegistry registry) { ? ? ? ? registry.addInterceptor(loginInterceptor) ? ? ? ? ? ? ? ? .addPathPatterns("/**") // 表示攔截所有的請求 ? ? ? ? ? ? ? ? .excludePathPatterns("/login", "/loginOverTime", "/register", "/plugins/**", "/javascript/**", "/api/system/user/login","/img/**","/css/common/**"); ? ? ? ? ? ? ? ? // 表示攔截所有的請求 ? ? } }
4、實(shí)現(xiàn)攔截器,先跳轉(zhuǎn)到超時(shí)頁面
這里采用先跳轉(zhuǎn)中轉(zhuǎn)頁面loginOverTime,然后再跳轉(zhuǎn)到登錄頁面,如果直接跳轉(zhuǎn)到登錄頁面只能在頁面的內(nèi)部iframe中跳轉(zhuǎn),無法這個(gè)頁面跳轉(zhuǎn)
@Component public class LoginInterceptor implements HandlerInterceptor { ? ? Logger logger = LoggerFactory.getLogger(LoginInterceptor.class); ? ? @Override ? ? public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) ? ? ? ? ? ? throws Exception { ? ? ? ? // 獲取session ? ? ? ? HttpSession session = request.getSession(true); ? ? ? ? // 判斷用戶是否存在,不存在就跳轉(zhuǎn)到登錄界面 ? ? ? ? if(session.getAttribute("user") == null){ ? ? ? ? ? ? response.sendRedirect(request.getContextPath()+"/loginOverTime"); ? ? ? ? ? ? return false; ? ? ? ? }else{ ? ? ? ? ? ? session.setAttribute("user", session.getAttribute("user")); ? ? ? ? ? ? return true; ? ? ? ? } ? ? } }
5、在超時(shí)頁面讓用戶等待幾秒鐘
然后自動跳轉(zhuǎn)到login頁面,提升一下用戶體驗(yàn)
{% extends 'common/layout' %} {% block head %} <link href="{{ request.contextPath }}/css/common/loginOverTime.css" rel="external nofollow" rel="stylesheet" /> {% endblock %} {% block content %} <body class="body_bg" > ?? ?<div class="show"> ?? ??? ?<div id="page"> ?? ??? ??? ?<h1>抱歉,登錄超時(shí)~</h1> ?? ??? ??? ?<h2> </h2> ?? ??? ??? ?<font color="#666666">由于您長期未操作為了保證您的信息安全請重新登錄!</font><br /><br /> ?? ??? ??? ?<div align="center" style="color: #666666"> ?? ??? ??? ??? ?將于<span>3</span>秒后跳轉(zhuǎn)至<a href="javascript:void(0)" rel="external nofollow" >登錄頁</a> ?? ??? ??? ?</div> ?? ??? ?</div> ?? ?</div> </body> {% endblock %} {% block footer %} <script type="text/javascript"> ? ? $(document).ready(function(){ ? ? ? ? // 關(guān)閉二級菜單 ? ? ? ? if(parent.window.closeSecondMenu != undefined){ ? ? ? ? ? ? parent.window.closeSecondMenu(); ? ? ? ? } ? ? ? ? // 讀秒顯示 ? ? ? ? var second = 3; ? ? ? ? // 設(shè)置定時(shí)任務(wù) ? ? ? ? window.setInterval("changeTime()", 1000); ? ? ? ? // 修改時(shí)間 ? ? ? ? changeTime = function(){ ? ? ? ? ? ? // 時(shí)間自動減1 ? ? ? ? ? ? second--; ? ? ? ? ? ? // 修改頁面上顯示 ? ? ? ? ? ? $("span").html(second); ? ? ? ? ? ? // 判斷是否跳轉(zhuǎn)登陸頁 ? ? ? ? ? ? if(second == 0){ ? ? ? ? ? ? ? ? $("a").click(); ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? // 跳轉(zhuǎn)至登錄頁 ? ? ? ? $("a").click(function(){ ? ? ? ? ? ? //window.location.href="{{ request.contextPath }}/login" rel="external nofollow" ; ? ? ? ? ? ? window.top.location="{{ request.contextPath }}/login"; ? ? ? ? }); ? ? }); </script> {% endblock %}
這樣就實(shí)現(xiàn)了sesseion超時(shí)退出的問題,大功告成
session超時(shí)的問題
最近在做SpringBoot的項(xiàng)目,用到了session,發(fā)現(xiàn)放置好session后,過一會就失效了,用下面發(fā)語句獲取session失效時(shí)間,發(fā)現(xiàn)是60s
request.getSession().getMaxInactiveInterval();
去網(wǎng)上查找,發(fā)現(xiàn)大多解決問題的辦法是 在啟動類中main方法的下面加入以下方法來手動設(shè)置session失效時(shí)間
@Bean? public EmbeddedServletContainerCustomizer containerCustomizer(){? ? ? ? ?return new EmbeddedServletContainerCustomizer() {? ?@Override? ?public void customize(ConfigurableEmbeddedServletContainer Container) {? ? ? ? ?container.setSessionTimeout(1800);//單位為S? ? ? ? ? ? ? }? ? ? ? ?};? ? ?}?
但是社會在發(fā)展,時(shí)代在進(jìn)步,SpringBoot2.0以后已經(jīng)不支持這種方式了
ps:可以在pom文件中查看你的SpringBooot版本。
<parent> ? ? <groupId>org.springframework.boot</groupId> ? ? <artifactId>spring-boot-starter-parent</artifactId> ? ? <version>2.0.4.RELEASE</version> ? ? <relativePath/> <!-- lookup parent from repository --> </parent>
SpringBoot2.0以后的版本只需要在application.properties中加入以下配置就好
server.servlet.session.timeout = PT5H
這里重點(diǎn)解釋一下 PT5H 意思是設(shè)置session失效的時(shí)間是5小時(shí)
通過查看setTimeouot的方法,這里要求傳入Duration的實(shí)例
public void setTimeout(Duration timeout) { ? ?this.timeout = timeout; }
- Duration是在Java8中新增的,主要用來計(jì)算日期差值
- Duration是被final聲明的,并且是線程安全的
- Duration轉(zhuǎn)換字符串方式,默認(rèn)為正,負(fù)以-開頭,緊接著P,以下字母不區(qū)分大小寫
- D :天 T:天和小時(shí)之間的分隔符 H :小時(shí) M:分鐘 S:秒 每個(gè)單位都必須是數(shù)字,且時(shí)分秒順序不能亂
- 比如P2dt3m5s P3d pt3h
最后總結(jié)一下Duration最實(shí)用的一個(gè)功能其實(shí)是 between 方法,因?yàn)橛泻芏鄷r(shí)候我們需要計(jì)算兩個(gè)日期之間的天數(shù)或者小時(shí)數(shù),用這個(gè)就可以很方便的進(jìn)行操作。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
解決偶現(xiàn)的MissingServletRequestParameterException異常問題
這篇文章主要介紹了解決偶現(xiàn)的MissingServletRequestParameterException問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10MyBatis學(xué)習(xí)教程(三)-MyBatis配置優(yōu)化
這篇文章主要介紹了MyBatis學(xué)習(xí)教程(三)-MyBatis配置優(yōu)化的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-05-05如何基于JWT實(shí)現(xiàn)接口的授權(quán)訪問詳解
授權(quán)是最常見的JWT使用場景,下面這篇文章主要給大家介紹了關(guān)于如何基于JWT實(shí)現(xiàn)接口的授權(quán)訪問的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-02-02AbstractQueuedSynchronizer內(nèi)部類Node使用講解
這篇文章主要為大家介紹了AbstractQueuedSynchronizer內(nèi)部類Node使用講解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07Java中十進(jìn)制和十六進(jìn)制的相互轉(zhuǎn)換方法
下面小編就為大家?guī)硪黄狫ava中十進(jìn)制和十六進(jìn)制的相互轉(zhuǎn)換方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-08-08SpringAOP 設(shè)置注入的實(shí)現(xiàn)步驟
這篇文章主要介紹了SpringAOP 設(shè)置注入的實(shí)現(xiàn)步驟,幫助大家更好的理解和學(xué)習(xí)使用Spring框架,感興趣的朋友可以了解下2021-05-05Struts攔截器實(shí)現(xiàn)攔截未登陸用戶實(shí)例解析
這篇文章主要介紹了Struts攔截器實(shí)現(xiàn)攔截未登陸用戶實(shí)例解析,分享了相關(guān)代碼示例,小編覺得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-02-02