스프링MVC 차단기와 필터 요약

차단기: 접근하는 URL을 차단 처리합니다
사용처: 권한 검증, 부호 설정 등
spring-mvc.xml 파일의 구성:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.1.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
            http://www.springframework.org/schema/tx
            ">


  <!-- -->
  <mvc:interceptors>
    <!-- url -->
    <mvc:interceptor>
      <mvc:mapping path="/test.do"/>
      <bean class="com.hbut.interceptor.TestInterceptor"/>
    </mvc:interceptor>
    <mvc:interceptor>
      <!-- -->
      <mvc:mapping path="/test/×/"/>
      <bean class="com.hbut.interceptor.TestInterceptor1"/>
    </mvc:interceptor>

    <mvc:interceptor>
      <!-- -->
      <mvc:mapping path="/test/×"/>
      <bean class="com.hbut.interceptor.TestInterceptor2"/>
    </mvc:interceptor>

  </mvc:interceptors>
모든 URL을 차단합니다

 <mvc:interceptors>
   <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/>
 </mvc:interceptors>
java 코드

package com.hbut.interceptor;

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

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

/**
 * @Author XiJun.Gong
 * @DATE 2016/6/1.
 * aim:  com.hbut.interceptor
 */
public class TestInterceptor implements HandlerInterceptor {

  @Override
  public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
    
   //todo  code 
   System.out.println("preHandle"); 
    return true; //todo  false , control 
  }

  @Override
  public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
    System.out.println("postHandle"); //todo  , 
  }

  @Override
  public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
    System.out.println("afterCompletion"); //todo  
  }
}

또 다른 차단기: 대동소이

package com.hbut.interceptor;

import org.springframework.ui.ModelMap;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.context.request.WebRequestInterceptor;

/**
 * @Author XiJun.Gong
 * @DATE 2016/6/1.
 * aim:  com.hbut.interceptor
 */
public class Test2Interceptor implements WebRequestInterceptor {
  @Override
  public void preHandle(WebRequest webRequest) throws Exception {
  }

  @Override
  public void postHandle(WebRequest webRequest, ModelMap modelMap) throws Exception {

  }

  @Override
  public void afterCompletion(WebRequest webRequest, Exception e) throws Exception {

  }
}

필터: servlet 용기에 의존하고 리셋 함수를 사용하여 필터 범위가 넓습니다.
차단기: 프레임 용기, 예를 들어spring, mybatis에 의존하여 유연성
이상은 본문의 전체 내용입니다. 여러분의 학습에 도움이 되고 저희를 많이 응원해 주십시오.

좋은 웹페이지 즐겨찾기