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 {

    @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、實現攔截器,先跳轉到超時頁面
這裏採用先跳轉中轉頁面loginOverTime,然後再跳轉到登錄頁面,如果直接跳轉到登錄頁面只能在頁面的內部iframe中跳轉,無法這個頁面跳轉

@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);
        // 判斷用戶是否存在,不存在就跳轉到登錄界面
        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(){
            // 時間自動減1
            second--;
            // 修改頁面上顯示
            $("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超時退出的問題,大功告成

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章