springmvc对所有请求的拦截

1.web.xml配置springmvc

2.springmvc配置文件将拦截的请求交由某个类来处理

3.处理拦截的类判断后选择是否放行。

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  <display-name>Archetype Created Web Application</display-name>
  
  <!-- 扫描加载配置文件 -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
classpath:spring/applicationContext-mvc.xml,
classpath:spring/applicationContext.xml,
classpath:spring/applicationContext-dataSource.xml
</param-value>
  </context-param>
  <!--log4j配置文件开始-->  
<context-param>  
   <param-name>log4jConfigLocation</param-name>  
   <param-value>classpath:log4j.properties</param-value>  
</context-param>  
<listener>  
   <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>  
</listener>
<!-- post方式提交 统一字符编码过滤器 -->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- springmvc拦截所有的请求交由各个处理器 -->
  <servlet>
    <servlet-name>springMvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring/applicationContext-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>springMvc</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

  <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

</web-app>

applicationContext-mvc.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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 扫描并注入 -->
<mvc:annotation-driven/>
<!-- 对静态文件的扫描,由Servlet来处理 -->
<mvc:default-servlet-handler/>
<!-- 访问拦截  -->  
  <mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**/**"/>                   <!-- 拦截器拦截的路径 -->
            <mvc:exclude-mapping path="/uploadFiles/**"/>  <!-- 拦截器不需要拦截的路径 -->
<bean class="com.fxb.filter.Loginfilter"/>
</mvc:interceptor>
</mvc:interceptors>

<!-- 指定扫描的位置 -->
<context:component-scan base-package="com.fxb.controller" />
 
<!-- 配置SpringMVC的视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>

Loginfilter.java

package com.fxb.filter;
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 Loginfilter extends HandlerInterceptorAdapter{
/**
* preHandle方法是进行处理器拦截用的,顾名思义,该方法将在Controller处理之前进行调用,
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("这里拦截所有的请求");
String login = "login";
if(login.equals("login")){
return true;            //放行
}else{
return false;          //拦截
}
}
/**
* 这个方法只会在当前这个Interceptor的preHandle方法返回值为true的时候才会执行。
*  postHandle是进行处理器拦截用的,它的执行时间是在处理器进行处理之 后, 也就是在Controller的方法调用之后执行。
*/
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
   
  }
   /**
    * 该方法也是需要当前对应的Interceptor的preHandle方法的返回值为true时才会执行。
    * 该方法将在整个请求完成之后,也就是DispatcherServlet渲染了视图执行, 这个方法的主要作用是用于清理资源的,
    */
    @Override
     public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
     
    }

}

项目路径:https://pan.baidu.com/s/1Y8AZf6_xIXClE6FVwcvfXA

猜你喜欢

转载自blog.csdn.net/fxbfxb111/article/details/80747464