Spring5源碼解析-Spring中的處理攔截器

在Java的Web應(yīng)用程序中通常使用過濾器(即filter)來捕獲HTTP請求。但它們僅為webapps保留。Spring引入了一種新的方法來實現(xiàn),更通用,稱為處理程序攔截器。

本文將分3部分。第一部分來講Spring處理程序攔截器的理論概念。第二部分,說一說默認(rèn)的Spring攔截器。最后一部分老規(guī)矩,應(yīng)用實戰(zhàn),我們將寫我們自己的處理程序攔截器。


什么是Spring中的處理程序攔截器?

要了解Spring攔截器的作用,我們需要先解釋一下HTTP請求的執(zhí)行鏈。DispatcherServlet捕獲每個請求。調(diào)度員做的第一件事就是將接收到的URL和相應(yīng)的controller進(jìn)行映射(controller必須恰到好處地處理當(dāng)前的請求)。但是,在到達(dá)對應(yīng)的controller之前,請求可以被攔截器處理。這些攔截器就像過濾器。只有當(dāng)URL找到對應(yīng)于它們的映射時才調(diào)用它們。在通過攔截器(攔截器預(yù)處理,其實也可以說前置處理)進(jìn)行前置處理后,請求最終到達(dá)controller。之后,發(fā)送請求生成視圖。但是在這之前,攔截器還是有可能來再次處理它(攔截器后置處理)。只有在最后一次操作之后,視圖解析器才能捕獲數(shù)據(jù)并輸出視圖。

處理程序映射攔截器基于org.springframework.web.servlet.HandlerInterceptor接口。和之前簡要描述的那樣,它們可以在將其發(fā)送到控制器(方法前使用preHandle)之前或之后(方法后使用postHandle)攔截請求。preHandle方法返回一個布爾值,如果返回false,則可以在執(zhí)行鏈中執(zhí)行中斷請求處理。此接口中還有一個方法afterCompletion,只有在preHandler方法發(fā)送為true時才會在渲染視圖后調(diào)用它(完成請求處理后的回調(diào),即渲染視圖后)。

攔截器也可以在新線程中啟動。在這種情況下,攔截器必須實現(xiàn)org.springframework.web.servlet.AsyncHandlerInterceptor接口。它繼承HandlerInterceptor并提供一個方法afterConcurrentHandlingStarted。每次處理程序得到正確執(zhí)行時,都會調(diào)用此方法而不是調(diào)用postHandler()和afterCompletion()。它也可以對發(fā)送請求進(jìn)行異步處理。通過Spring源碼此方法注釋可以知道,這個方法的典型的應(yīng)用是可以用來清理本地線程變量。

/**
 * Extends {@code HandlerInterceptor} with a callback method invoked after the
 * start of asynchronous request handling.
 *
 * <p>When a handler starts an asynchronous request, the {@link DispatcherServlet}
 * exits without invoking {@code postHandle} and {@code afterCompletion} as it
 * normally does for a synchronous request, since the result of request handling
 * (e.g. ModelAndView) is likely not yet ready and will be produced concurrently
 * from another thread. In such scenarios, {@link #afterConcurrentHandlingStarted}
 * is invoked instead, allowing implementations to perform tasks such as cleaning
 * up thread-bound attributes before releasing the thread to the Servlet container.
 *
 * <p>When asynchronous handling completes, the request is dispatched to the
 * container for further processing. At this stage the {@code DispatcherServlet}
 * invokes {@code preHandle}, {@code postHandle}, and {@code afterCompletion}.
 * To distinguish between the initial request and the subsequent dispatch
 * after asynchronous handling completes, interceptors can check whether the
 * {@code javax.servlet.DispatcherType} of {@link javax.servlet.ServletRequest}
 * is {@code "REQUEST"} or {@code "ASYNC"}.
 *
 * <p>Note that {@code HandlerInterceptor} implementations may need to do work
 * when an async request times out or completes with a network error. For such
 * cases the Servlet container does not dispatch and therefore the
 * {@code postHandle} and {@code afterCompletion} methods will not be invoked.
 * Instead, interceptors can register to track an asynchronous request through
 * the {@code registerCallbackInterceptor} and {@code registerDeferredResultInterceptor}
 * methods on {@link org.springframework.web.context.request.async.WebAsyncManager
 * WebAsyncManager}. This can be done proactively on every request from
 * {@code preHandle} regardless of whether async request processing will start.
 *
 * @author Rossen Stoyanchev
 * @since 3.2
 * @see org.springframework.web.context.request.async.WebAsyncManager
 * @see org.springframework.web.context.request.async.CallableProcessingInterceptor
 * @see org.springframework.web.context.request.async.DeferredResultProcessingInterceptor
 */
public interface AsyncHandlerInterceptor extends HandlerInterceptor {
    /**
     * Called instead of {@code postHandle} and {@code afterCompletion}, when
     * the a handler is being executed concurrently.
     * <p>Implementations may use the provided request and response but should
     * avoid modifying them in ways that would conflict with the concurrent
     * execution of the handler. A typical use of this method would be to
     * clean up thread-local variables.
     * @param request the current request
     * @param response the current response
     * @param handler the handler (or {@link HandlerMethod}) that started async
     * execution, for type and/or instance examination
     * @throws Exception in case of errors
     */
    void afterConcurrentHandlingStarted(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception;
}


攔截器和過濾器之間的區(qū)別

攔截器看起來很像servlet過濾器,為什么Spring不采用默認(rèn)的Java解決方案?這其中主要區(qū)別就是兩者的作用域的問題。過濾器只能在servlet容器下使用。而我們的Spring容器不一定運行在web環(huán)境中,在這種情況下過濾器就不好使了,而攔截器依然可以在Spring容器中調(diào)用。

Spring通過攔截器為請求提供了一個更細(xì)粒度的控制。就像我們之前看到的那樣,它們可以在controller對請求處理之前或之后被調(diào)用,也可以在將渲染視圖呈現(xiàn)給用戶之后被調(diào)用。如果是過濾器的話,只能在將響應(yīng)返回給最終用戶之前使用它們。

下一個不同之處在于中斷鏈執(zhí)行的難易程度。攔截器可以通過在preHandler()方法內(nèi)返回false來簡單實現(xiàn)。而在過濾器的情況下,它就變得復(fù)雜了,因為它必須處理請求和響應(yīng)對象來引發(fā)中斷,需要一些額外的動作,比如如將用戶重定向到錯誤頁面。


什么是默認(rèn)的Spring攔截器?

Spring主要將攔截器用于切換操作。比如我們最常用的功能之一是區(qū)域設(shè)置更改(也就是本地化更改)。請查看org.springframework.web.servlet.i18n.LocaleChangeInterceptor類中源碼,可以通過我們所定義的語言環(huán)境解析器來對HTTP請求進(jìn)行分析來實現(xiàn)。所有區(qū)域設(shè)置解析器都會分析請求元素(headers,Cookie),以確定向用戶提供哪種本地化語言設(shè)置。

另一個本地攔截器是org.springframework.web.servlet.theme.ThemeChangeInterceptor,它允許更改視圖的主題(見此類的注釋)。它還使用主題解析器更精確地來知道要使用的主題(參照下面preHandle方法)。它的解析器也基于請求分析(cookie,會話或參數(shù))。

/**
 * Interceptor that allows for changing the current theme on every request,
 * via a configurable request parameter (default parameter name: "theme").
 *
 * @author Juergen Hoeller
 * @since 20.06.2003
 * @see org.springframework.web.servlet.ThemeResolver
 */
public class ThemeChangeInterceptor extends HandlerInterceptorAdapter {
    /**
     * Default name of the theme specification parameter: "theme".
     */
    public static final String DEFAULT_PARAM_NAME = "theme";
    private String paramName = DEFAULT_PARAM_NAME;
    /**
     * Set the name of the parameter that contains a theme specification
     * in a theme change request. Default is "theme".
     */
    public void setParamName(String paramName) {
        this.paramName = paramName;
    }
    /**
     * Return the name of the parameter that contains a theme specification
     * in a theme change request.
     */
    public String getParamName() {
        return this.paramName;
    }
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws ServletException {
        String newTheme = request.getParameter(this.paramName);
        if (newTheme != null) {
            ThemeResolver themeResolver = RequestContextUtils.getThemeResolver(request);
            if (themeResolver == null) {
                throw new IllegalStateException("No ThemeResolver found: not in a DispatcherServlet request?");
            }
            themeResolver.setThemeName(request, response, newTheme);
        }
        // Proceed in any case.
        return true;
    }
}


在Spring中自定義處理程序攔截器

我們寫一個例子來簡單實現(xiàn)HandlerInterceptor。一個樂透彩票的場景,這個自定義的攔截器將分析每個請求,并決定是否是彩票的“l(fā)ottery winner”。為了簡化代碼邏輯,只有用于生成一個隨機(jī)數(shù)并通過取模判斷是否返回0的請求。

public class LotteryInterceptor implements HandlerInterceptor {

    public static final String ATTR_NAME = "lottery_winner";
    private static final Logger LOGGER = LoggerFactory.getLogger(LotteryInterceptor.class);

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception exception) throws Exception {
        LOGGER.debug("[LotteryInterceptor] afterCompletion");

    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView view) throws Exception {
        LOGGER.debug("[LotteryInterceptor] postHandle");

    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        LOGGER.debug("[LotteryInterceptor] preHandle");
        if (request.getSession().getAttribute(ATTR_NAME) == null) {
            Random random = new Random();
            int i = random.nextInt(10);
            request.getSession().setAttribute(ATTR_NAME, i%2 == 0);
        }
        return true;
    }

}

關(guān)于相應(yīng)controller中要展示的信息:

@Controller
public class TestController {
        private static final Logger LOGGER = LoggerFactory.getLogger(TestController.class);
    @RequestMapping(value = "/test", method = RequestMethod.GET)
    public String test(HttpServletRequest request) {
        LOGGER.debug("Controller asks, are you a lottery winner ? "+request.getSession().getAttribute(LotteryInterceptor.ATTR_NAME));
        return "test";
    }
}

如果我們嘗試訪問/test,我們將看不到攔截器的日志,因為它沒有在配置中定義。如果我們是使用注解來配置的webapp。我們需要將下面這個配置添加到應(yīng)用程序的上下文文件中(Springboot配置個相應(yīng)的bean就可):

<mvc:interceptors>
    <bean class="com.waitingforcode.interceptors.LotteryInterceptor" />
</mvc:interceptors>

現(xiàn)在我們可以訪問/ test頁面并檢查日志:

[LotteryInterceptor] preHandle
Controller asks, are you a lottery winner ? false
[LotteryInterceptor] postHandle
[LotteryInterceptor] afterCompletion

總結(jié)一下,攔截器是一種可以應(yīng)用到整個Spring生態(tài)系統(tǒng)中的servlet過濾器。它們可以在請求之前或之后啟動,也可以在視圖呈現(xiàn)之后啟動。它們也可以通過AsyncHandlerInterceptor接口的實現(xiàn)達(dá)到異步處理的效果。

原文:Spring5源碼解析-Spring中的處理攔截器
極樂科技:知乎專欄

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,533評論 19 139
  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 47,261評論 6 342
  • spring mvc 工作機(jī)制(原理): DispatcherServlet主要用作職責(zé)調(diào)度工作,本身主要用于控制...
    java大濕兄閱讀 1,974評論 5 24
  • 什么是Spring Spring是一個開源的Java EE開發(fā)框架。Spring框架的核心功能可以應(yīng)用在任何Jav...
    jemmm閱讀 16,766評論 1 133
  • 2016年的最后一天。 2016年,跟之前的每一年都幾乎相似,卻又完全不同。希臘哲人早就說過了:人永遠(yuǎn)無法踏進(jìn)同一...
    珠海兔子閱讀 404評論 0 1

友情鏈接更多精彩內(nèi)容