springmvc拦截器 登录缓存失效然后跳转重新登录

1.新增配置文件dispatcher-servlet.xml  

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context-3.0.xsd
            http://www.springframework.org/schema/aop 
            http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
            http://www.springframework.org/schema/tx 
            http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
            http://www.springframework.org/schema/mvc 
            http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    <!--
        使Spring支持自动检测组件,如注解的Controller
    -->
    <context:component-scan base-package="glxt.jzrj.web" />

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/projectManage/" />
        <property name="suffix" value=".html" />
    </bean>
    
    <mvc:interceptors>
        <mvc:interceptor>
            <!--设置拦截的路径  /** 代表拦截所有-->
            <mvc:mapping path="/**" />
            <!-- 设置不需要拦截的路径  见下图 1-1-->
            <mvc:exclude-mapping path="/sysLogin/*" />
            <bean class="glxt.jzrj.web.AuthFilterInterceptor"><!-- 在这个类里面写拦截的代码 -->
                <!--outsideOfficeHoursPage属性指定页面的URL-->
                <property name="outsideOfficeHoursPage">
                    <value>/glxt/index.jsp
                    </value>
                </property>
            </bean>
        </mvc:interceptor>
    </mvc:interceptors>
    <bean id="messageSource"
        class="org.springframework.context.support.ResourceBundleMessageSource"
        p:basename="message">
    </bean>
</beans>

 

图1-1

2. 在application-context.xml引入新增的配置文件(该文件在web.xml里引入所以只需要在application-context.xml配置文件里面引入拦截器的配置文件就行,注意引入的路径  我这里设置的是classpath 根据个人项目而定)

3.新增拦截代码(新增一个类)代码如下

package glxt.jzrj.web;

import glxt.jzrj.units.CommonUtils;

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

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class AuthFilterInterceptor extends HandlerInterceptorAdapter {
    // 继承HandlerInterceptorAdapter类

    private String outsideOfficeHoursPage;// outsideOfficeHoursPage属性指定错误提示页面的URL

    // 重写 preHandle()方法,在业务处理器处理请求之前对该请求进行拦截处理
    public boolean preHandle(HttpServletRequest request,HttpServletResponse response, Object handler) throws Exception {
        String urlString = request.getRequestURI();
        System.out.println(urlString);
        String userId = request.getSession().getAttribute("userId")+"";
        System.out.println(userId);
        if(CommonUtils.isNotEmpty(userId) && !"null".equals(userId)){
            return true;
        } else {
            response.sendRedirect(outsideOfficeHoursPage); // 返回提示页面
            return false;
        }
    }

    public void postHandle(HttpServletRequest request,HttpServletResponse response, Object o, ModelAndView mav)
            throws Exception {
    }

    public void afterCompletion(HttpServletRequest request,HttpServletResponse response, Object o, Exception excptn)
            throws Exception {
    }

    public String getOutsideOfficeHoursPage() {
        return outsideOfficeHoursPage;
    }

    public void setOutsideOfficeHoursPage(String outsideOfficeHoursPage) {
        this.outsideOfficeHoursPage = outsideOfficeHoursPage;
    }

}

 

注意:路径需要仔细查看 以及 防止出错,由于环境存在差异如有建议请留言评论

 http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd  只有3.2及以上才会有

<mvc:exclude-mapping path="/sysLogin/*" />这个标签

根据个人需要调整版本


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值