当session失效,页面跳转到登陆界面的处理

每个系统页面操作过程中都有一个session,session可以存放少量的用户信息,供我们的页面操作使用。当session超时失效的时候我们就要重新往session中写入登陆的用户信息,而这个写入的操作一般写在在用户成功登陆系统的时候,所以当session失效时,我们页面中所有的操作都要监听,然后跳转到登陆的界面重新登陆。

1、设置session有效时间

在web.xml里面设置:

<!-- session失效时间 -->
    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>

2、增加一个拦截器,拦截到后台的请求

package com.ht.spring.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class SystemSessionInterceptor implements HandlerInterceptor{

    @Override
    public void afterCompletion(HttpServletRequest arg0,
            HttpServletResponse arg1, Object arg2, Exception arg3)
            throws Exception {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
            Object arg2, ModelAndView arg3) throws Exception {
        // TODO Auto-generated method stub
        
    }

    @Override
    public boolean preHandle(HttpServletRequest req, HttpServletResponse res,
            Object arg2) throws Exception {
        HttpSession session = req.getSession(true);
        String userCode = (String) session.getAttribute("userCodeModel");
        String path = req.getSession().getServletContext().getContextPath() + "/jsps/login.jsp";
        /*if(userCode == null || "".equals(userCode)){
            res.sendRedirect(path);
            return false;
            throw new SessionTimeoutException();
        }else{
            return true;
        }*/
        /*if(req.getHeader("x-requested-with")!=null
                &&req.getHeader("x-requested-with").equalsIgnoreCase("XMLHttpRequest")){
            res.setHeader("sessionStatus", "timeout");
        }
        res.getWriter().print("timeout");
        return true;*/
        if(userCode == null || "".equals(userCode)){
            if(req.getHeader("x-requested-with")!=null
                    &&req.getHeader("x-requested-with").equalsIgnoreCase("XMLHttpRequest")){
                res.setHeader("sessionStatus", "timeout");
            }else{
                res.sendRedirect(path);
            }
            return false;
        }
        return true;
    }

}
3、在spring-mvc.xml里面加这个拦截器的拦截过滤

<!-- session失效拦截 -->
    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/**" /><!-- 拦截的目录-->
            <mvc:exclude-mapping path="/user/login.do" /><!-- 不需要拦截的请求-->
            <mvc:exclude-mapping path="/user/loginAgain.do" />
            <mvc:exclude-mapping path="/jsps" />
            <mvc:exclude-mapping path="/jquery-easyui-1.5.3" />
            <mvc:exclude-mapping path="/content" />
            <mvc:exclude-mapping path="/WEB-INF" /> 
            <bean class="com.ht.spring.interceptor.SystemSessionInterceptor"></bean><!-- 拦截器的加载类-->
        </mvc:interceptor>
    </mvc:interceptors>

4、增加一个异常处理的js,在每一个页面加以引用

$.ajaxSetup({
    error:function(XMLHttpRequest, textStatus, errorThrown){
        if(XMLHttpRequest.status==403){
            alert("您没有权限访问此资源或者进行操作");
            return false;
        }
    },
    complete:function(XMLHttpRequest, textStatus){
        var sessionStatus = XMLHttpRequest.getResponseHeader("sessionStatus");
        if(sessionStatus == "timeout"){
             $.messager.alert("系统提示", "登录超时,请重新登录");
             setTimeout(function(){
                 var top = getTopWinow();
                //$.messager.alert("系统提示", "登录超时,请重新登录");
                var curPath = window.document.location.href;
                var pathName = window.document.location.pathname;
                var pos = curPath.indexOf(pathName);
                var lacalhostPath = curPath.substring(0,pos);
                var prjName = pathName.substring(0,pathName.substr(1).indexOf("/")+1);
                //top.location.href="${pageContext.request.contextPath}"+"/jsps/login.jsp";
                top.location.href = lacalhostPath + prjName +"/jsps/login.jsp";
             },1000);
        }
    }
});
function getTopWinow(){
    var p =window;
    while(p != p.parent){
        p = p.parent;
    }
    return p;
}
 

 

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,这是一个比较常见的需求,下面是一个简单的实现过程: 1. 在登录页面(login.jsp)中,用户输入用户名和密码,点击登录按钮,提交表单数据到后台的登录接口(login.do)。 2. 后台接收到请求,验证用户名和密码是否正确,如果正确则在Session中保存用户信息(比如用户名),并重定向到欢迎页面(welcome.jsp)。 3. 在欢迎页面(welcome.jsp)中,从Session中获取保存的用户信息,并输出欢迎消息。 下面是一个示例代码: login.jsp: ```html <form action="login.do" method="post"> <label>用户名:</label> <input type="text" name="username"><br> <label>密码:</label> <input type="password" name="password"><br> <input type="submit" value="登录"> </form> ``` login.do: ```java // 接收登录请求 String username = request.getParameter("username"); String password = request.getParameter("password"); // 验证用户名和密码是否正确(这里省略验证逻辑) if ("admin".equals(username) && "123456".equals(password)) { // 登录成功,保存用户信息到Session HttpSession session = request.getSession(); session.setAttribute("username", username); // 重定向到欢迎页面 response.sendRedirect("welcome.jsp"); } else { // 登录失败,跳转登录页面 response.sendRedirect("login.jsp"); } ``` welcome.jsp: ```html <% // 从Session中获取保存的用户信息 String username = (String) session.getAttribute("username"); %> <h1>欢迎 <%= username %>!</h1> ``` 上面的代码只是一个简单的示例,实际应用中还需要考虑更多的安全性和用户体验问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

w_iceh

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值