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/*" />这个标签
根据个人需要调整版本