springboot2中session超時,退到登錄頁面方式
session超時退到登錄頁面
最近發(fā)現(xiàn)使用的工程居然沒有session超時機制,功能太欠缺了,現(xiàn)在把追加方法分享出來,里面有一些坑,大家自由使用。
1、首先在springboot中追加配置session的超時時間
注意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、實現(xiàn)攔截器,先跳轉(zhuǎn)到超時頁面
這里采用先跳轉(zhuǎn)中轉(zhuǎn)頁面loginOverTime,然后再跳轉(zhuǎn)到登錄頁面,如果直接跳轉(zhuǎn)到登錄頁面只能在頁面的內(nèi)部iframe中跳轉(zhuǎn),無法這個頁面跳轉(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、在超時頁面讓用戶等待幾秒鐘
然后自動跳轉(zhuǎn)到login頁面,提升一下用戶體驗
{% 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>抱歉,登錄超時~</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(){ ? ? ? ? // 關閉二級菜單 ? ? ? ? if(parent.window.closeSecondMenu != undefined){ ? ? ? ? ? ? parent.window.closeSecondMenu(); ? ? ? ? } ? ? ? ? // 讀秒顯示 ? ? ? ? var second = 3; ? ? ? ? // 設置定時任務 ? ? ? ? window.setInterval("changeTime()", 1000); ? ? ? ? // 修改時間 ? ? ? ? changeTime = function(){ ? ? ? ? ? ? // 時間自動減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 %}
這樣就實現(xiàn)了sesseion超時退出的問題,大功告成
session超時的問題
最近在做SpringBoot的項目,用到了session,發(fā)現(xiàn)放置好session后,過一會就失效了,用下面發(fā)語句獲取session失效時間,發(fā)現(xiàn)是60s
request.getSession().getMaxInactiveInterval();
去網(wǎng)上查找,發(fā)現(xiàn)大多解決問題的辦法是 在啟動類中main方法的下面加入以下方法來手動設置session失效時間
@Bean? public EmbeddedServletContainerCustomizer containerCustomizer(){? ? ? ? ?return new EmbeddedServletContainerCustomizer() {? ?@Override? ?public void customize(ConfigurableEmbeddedServletContainer Container) {? ? ? ? ?container.setSessionTimeout(1800);//單位為S? ? ? ? ? ? ? }? ? ? ? ?};? ? ?}?
但是社會在發(fā)展,時代在進步,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
這里重點解釋一下 PT5H 意思是設置session失效的時間是5小時
通過查看setTimeouot的方法,這里要求傳入Duration的實例
public void setTimeout(Duration timeout) { ? ?this.timeout = timeout; }
- Duration是在Java8中新增的,主要用來計算日期差值
- Duration是被final聲明的,并且是線程安全的
- Duration轉(zhuǎn)換字符串方式,默認為正,負以-開頭,緊接著P,以下字母不區(qū)分大小寫
- D :天 T:天和小時之間的分隔符 H :小時 M:分鐘 S:秒 每個單位都必須是數(shù)字,且時分秒順序不能亂
- 比如P2dt3m5s P3d pt3h
最后總結一下Duration最實用的一個功能其實是 between 方法,因為有很多時候我們需要計算兩個日期之間的天數(shù)或者小時數(shù),用這個就可以很方便的進行操作。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
解決偶現(xiàn)的MissingServletRequestParameterException異常問題
這篇文章主要介紹了解決偶現(xiàn)的MissingServletRequestParameterException問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10MyBatis學習教程(三)-MyBatis配置優(yōu)化
這篇文章主要介紹了MyBatis學習教程(三)-MyBatis配置優(yōu)化的相關資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-05-05AbstractQueuedSynchronizer內(nèi)部類Node使用講解
這篇文章主要為大家介紹了AbstractQueuedSynchronizer內(nèi)部類Node使用講解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-07-07