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

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時(shí)間:2023/12/10 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 springboot2中session超时,退到登录页面 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

最近發(fā)現(xiàn)使用的工程居然沒(méi)有session超時(shí)機(jī)制,功能太欠缺了,現(xiàn)在把追加方法分享出來(lái),里面有一些坑,大家自由使用。
1、首先在springboot中追加配置session的超時(shí)時(shí)間,注意springboot2的寫(xiě)法發(fā)生了改變

springboot2寫(xiě)法

server:servlet:session:timeout: 1800s

springboot1寫(xiě)法

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 {@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("/**") // 表示攔截所有的請(qǐng)求.excludePathPatterns("/login", "/loginOverTime", "/register", "/plugins/**", "/javascript/**", "/api/system/user/login","/img/**","/css/common/**");// 表示攔截所有的請(qǐng)求} }

4、實(shí)現(xiàn)攔截器,先跳轉(zhuǎn)到超時(shí)頁(yè)面
這里采用先跳轉(zhuǎn)中轉(zhuǎn)頁(yè)面loginOverTime,然后再跳轉(zhuǎn)到登錄頁(yè)面,如果直接跳轉(zhuǎn)到登錄頁(yè)面只能在頁(yè)面的內(nèi)部iframe中跳轉(zhuǎn),無(wú)法這個(gè)頁(yè)面跳轉(zhuǎn)

@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);// 判斷用戶是否存在,不存在就跳轉(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í)頁(yè)面讓用戶等待幾秒鐘,然后自動(dòng)跳轉(zhuǎn)到login頁(yè)面,提升一下用戶體驗(yàn)

{% 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>抱歉,登錄超時(shí)~</h1><h2> </h2><font color="#666666">由于您長(zhǎng)期未操作為了保證您的信息安全請(qǐng)重新登錄!</font><br /><br /><div align="center" style="color: #666666">將于<span>3</span>秒后跳轉(zhuǎn)至<a href="javascript:void(0)">登錄頁(yè)</a></div></div></div> </body> {% endblock %} {% block footer %} <script type="text/javascript">$(document).ready(function(){// 關(guān)閉二級(jí)菜單if(parent.window.closeSecondMenu != undefined){parent.window.closeSecondMenu();}// 讀秒顯示var second = 3;// 設(shè)置定時(shí)任務(wù)window.setInterval("changeTime()", 1000);// 修改時(shí)間changeTime = function(){// 時(shí)間自動(dòng)減1second--;// 修改頁(yè)面上顯示$("span").html(second);// 判斷是否跳轉(zhuǎn)登陸頁(yè)if(second == 0){$("a").click();}}// 跳轉(zhuǎn)至登錄頁(yè)$("a").click(function(){//window.location.href="{{ request.contextPath }}/login";window.top.location="{{ request.contextPath }}/login";});}); </script> {% endblock %}

這樣就實(shí)現(xiàn)了sesseion超時(shí)退出的問(wèn)題,大功告成

總結(jié)

以上是生活随笔為你收集整理的springboot2中session超时,退到登录页面的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。