springboot2中session超时,退到登录页面
生活随笔
收集整理的這篇文章主要介紹了
springboot2中session超时,退到登录页面
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
最近發現使用的工程居然沒有session超時機制,功能太欠缺了,現在把追加方法分享出來,里面有一些坑,大家自由使用。
1、首先在springboot中追加配置session的超時時間,注意springboot2的寫法發生了改變
springboot2寫法
server:servlet:session:timeout: 1800sspringboot1寫法
server:session:timeout: 1800s2、登錄成功接口中把用戶信息追加session中
public ResponseEntity loginGo(HttpServletRequest request,String userName, String password) {// 此處省略若干HttpSession session = request.getSession(true);session.setAttribute("username", user.getUserRemark()); }3、在WebMvcConfig中配置攔截規則和重定向規則
@Configuration public class WebMvcConfig implements WebMvcConfigurer {@Autowiredprivate LoginInterceptor loginInterceptor;@Overridepublic void addViewControllers(ViewControllerRegistry registry) {registry.addViewController("/login").setViewName("login");registry.addViewController("/loginOverTime").setViewName("loginOverTime");}@Overridepublic void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(loginInterceptor).addPathPatterns("/**") // 表示攔截所有的請求.excludePathPatterns("/login", "/loginOverTime", "/register", "/plugins/**", "/javascript/**", "/api/system/user/login","/img/**","/css/common/**");// 表示攔截所有的請求} }4、實現攔截器,先跳轉到超時頁面
這里采用先跳轉中轉頁面loginOverTime,然后再跳轉到登錄頁面,如果直接跳轉到登錄頁面只能在頁面的內部iframe中跳轉,無法這個頁面跳轉
5、在超時頁面讓用戶等待幾秒鐘,然后自動跳轉到login頁面,提升一下用戶體驗
{% extends 'common/layout' %} {% block head %} <link href="{{ request.contextPath }}/css/common/loginOverTime.css" rel="stylesheet" /> {% endblock %} {% block content %} <body class="body_bg" ><div class="show"><div id="page"><h1>抱歉,登錄超時~</h1><h2> </h2><font color="#666666">由于您長期未操作為了保證您的信息安全請重新登錄!</font><br /><br /><div align="center" style="color: #666666">將于<span>3</span>秒后跳轉至<a href="javascript:void(0)">登錄頁</a></div></div></div> </body> {% endblock %} {% block footer %} <script type="text/javascript">$(document).ready(function(){// 關閉二級菜單if(parent.window.closeSecondMenu != undefined){parent.window.closeSecondMenu();}// 讀秒顯示var second = 3;// 設置定時任務window.setInterval("changeTime()", 1000);// 修改時間changeTime = function(){// 時間自動減1second--;// 修改頁面上顯示$("span").html(second);// 判斷是否跳轉登陸頁if(second == 0){$("a").click();}}// 跳轉至登錄頁$("a").click(function(){//window.location.href="{{ request.contextPath }}/login";window.top.location="{{ request.contextPath }}/login";});}); </script> {% endblock %}這樣就實現了sesseion超時退出的問題,大功告成
總結
以上是生活随笔為你收集整理的springboot2中session超时,退到登录页面的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 第三十三期:使用wireshark抓包分
- 下一篇: 前端学习(237):IE条件注释法