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超时退出的问题,大功告成

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