springboot2中session超時(shí),退到登錄頁(yè)面方式
session超時(shí)退到登錄頁(yè)面
最近發(fā)現(xiàn)使用的工程居然沒(méi)有session超時(shí)機(jī)制,功能太欠缺了,現(xiàn)在把追加方法分享出來(lái),里面有一些坑,大家自由使用。
1、首先在springboot中追加配置session的超時(shí)時(shí)間
注意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("/**") // 表示攔截所有的請(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);
? ? @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、在超時(shí)頁(yè)面讓用戶等待幾秒鐘
然后自動(dòng)跳轉(zhuǎn)到login頁(yè)面,提升一下用戶體驗(yàn)
{% 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>抱歉,登錄超時(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)" rel="external nofollow" >登錄頁(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)減1
? ? ? ? ? ? second--;
? ? ? ? ? ? // 修改頁(yè)面上顯示
? ? ? ? ? ? $("span").html(second);
? ? ? ? ? ? // 判斷是否跳轉(zhuǎn)登陸頁(yè)
? ? ? ? ? ? if(second == 0){
? ? ? ? ? ? ? ? $("a").click();
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? // 跳轉(zhuǎn)至登錄頁(yè)
? ? ? ? $("a").click(function(){
? ? ? ? ? ? //window.location.href="{{ request.contextPath }}/login" rel="external nofollow" ;
? ? ? ? ? ? window.top.location="{{ request.contextPath }}/login";
? ? ? ? });
? ? });
</script>
{% endblock %}這樣就實(shí)現(xiàn)了sesseion超時(shí)退出的問(wèn)題,大功告成
session超時(shí)的問(wèn)題
最近在做SpringBoot的項(xiàng)目,用到了session,發(fā)現(xiàn)放置好session后,過(guò)一會(huì)就失效了,用下面發(fā)語(yǔ)句獲取session失效時(shí)間,發(fā)現(xiàn)是60s
request.getSession().getMaxInactiveInterval();
去網(wǎng)上查找,發(fā)現(xiàn)大多解決問(wèn)題的辦法是 在啟動(dòng)類中main方法的下面加入以下方法來(lái)手動(dòng)設(shè)置session失效時(shí)間
@Bean?
public EmbeddedServletContainerCustomizer containerCustomizer(){?
? ? ? ?return new EmbeddedServletContainerCustomizer() {?
?@Override?
?public void customize(ConfigurableEmbeddedServletContainer Container) {?
? ? ? ?container.setSessionTimeout(1800);//單位為S?
? ? ? ? ? ? }?
? ? ? ?};?
? ?}?但是社會(huì)在發(fā)展,時(shí)代在進(jìn)步,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
這里重點(diǎn)解釋一下 PT5H 意思是設(shè)置session失效的時(shí)間是5小時(shí)
通過(guò)查看setTimeouot的方法,這里要求傳入Duration的實(shí)例
public void setTimeout(Duration timeout) {
? ?this.timeout = timeout;
}- Duration是在Java8中新增的,主要用來(lái)計(jì)算日期差值
- Duration是被final聲明的,并且是線程安全的
- Duration轉(zhuǎn)換字符串方式,默認(rèn)為正,負(fù)以-開(kāi)頭,緊接著P,以下字母不區(qū)分大小寫
- D :天 T:天和小時(shí)之間的分隔符 H :小時(shí) M:分鐘 S:秒 每個(gè)單位都必須是數(shù)字,且時(shí)分秒順序不能亂
- 比如P2dt3m5s P3d pt3h
最后總結(jié)一下Duration最實(shí)用的一個(gè)功能其實(shí)是 between 方法,因?yàn)橛泻芏鄷r(shí)候我們需要計(jì)算兩個(gè)日期之間的天數(shù)或者小時(shí)數(shù),用這個(gè)就可以很方便的進(jìn)行操作。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
解決偶現(xiàn)的MissingServletRequestParameterException異常問(wèn)題
這篇文章主要介紹了解決偶現(xiàn)的MissingServletRequestParameterException問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10
MyBatis學(xué)習(xí)教程(三)-MyBatis配置優(yōu)化
這篇文章主要介紹了MyBatis學(xué)習(xí)教程(三)-MyBatis配置優(yōu)化的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-05-05
如何基于JWT實(shí)現(xiàn)接口的授權(quán)訪問(wèn)詳解
授權(quán)是最常見(jiàn)的JWT使用場(chǎng)景,下面這篇文章主要給大家介紹了關(guān)于如何基于JWT實(shí)現(xiàn)接口的授權(quán)訪問(wèn)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-02-02
AbstractQueuedSynchronizer內(nèi)部類Node使用講解
這篇文章主要為大家介紹了AbstractQueuedSynchronizer內(nèi)部類Node使用講解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
Java中十進(jìn)制和十六進(jìn)制的相互轉(zhuǎn)換方法
下面小編就為大家?guī)?lái)一篇Java中十進(jìn)制和十六進(jìn)制的相互轉(zhuǎn)換方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-08-08
重新認(rèn)識(shí)Java中的ThreadLocal
ThreadLocal是JDK包提供的,它提供線程本地變量,如果創(chuàng)建一個(gè)ThreadLocal變量,那么訪問(wèn)這個(gè)變量的每個(gè)線程都會(huì)有這個(gè)變量的一個(gè)副本,在實(shí)際多線程操作的時(shí)候,操作的是自己本地內(nèi)存中的變量,從而規(guī)避了線程安全問(wèn)題2021-05-05
SpringAOP 設(shè)置注入的實(shí)現(xiàn)步驟
這篇文章主要介紹了SpringAOP 設(shè)置注入的實(shí)現(xiàn)步驟,幫助大家更好的理解和學(xué)習(xí)使用Spring框架,感興趣的朋友可以了解下2021-05-05
Struts攔截器實(shí)現(xiàn)攔截未登陸用戶實(shí)例解析
這篇文章主要介紹了Struts攔截器實(shí)現(xiàn)攔截未登陸用戶實(shí)例解析,分享了相關(guān)代碼示例,小編覺(jué)得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-02-02

