springboot2中session超时,退到登录页面
最近發(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: 1800sspringboot1寫(xiě)法
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中配置攔截規(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)
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)題。
- 上一篇: 第三十三期:使用wireshark抓包分
- 下一篇: 英国文化影响管理风格_文化如何影响用户体