日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

springboot2中session超时,退到登录页面

發布時間:2023/12/10 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 springboot2中session超时,退到登录页面 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

最近發現使用的工程居然沒有session超時機制,功能太欠缺了,現在把追加方法分享出來,里面有一些坑,大家自由使用。
1、首先在springboot中追加配置session的超時時間,注意springboot2的寫法發生了改變

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中配置攔截規則和重定向規則

@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中跳轉,無法這個頁面跳轉

@Component public class LoginInterceptor implements HandlerInterceptor {Logger logger = LoggerFactory.getLogger(LoginInterceptor.class);@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)throws Exception {// 獲取sessionHttpSession session = request.getSession(true);// 判斷用戶是否存在,不存在就跳轉到登錄界面if(session.getAttribute("user") == null){response.sendRedirect(request.getContextPath()+"/loginOverTime");return false;}else{session.setAttribute("user", session.getAttribute("user"));return true;}} }

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超时,退到登录页面的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。