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

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

本文將分3部分。第一部分來講Spring處理程序攔截器的理論概念。第二部分,說一說默認的Spring攔截器。最后一部分老規矩,應用實戰,我們將寫我們自己的處理程序攔截器。


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

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

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

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

/**
 * 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;
}


攔截器和過濾器之間的區別

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

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

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


什么是默認的Spring攔截器?

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

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

/**
 * 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中自定義處理程序攔截器

我們寫一個例子來簡單實現HandlerInterceptor。一個樂透彩票的場景,這個自定義的攔截器將分析每個請求,并決定是否是彩票的“lottery winner”。為了簡化代碼邏輯,只有用于生成一個隨機數并通過取模判斷是否返回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;
    }

}

關于相應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。我們需要將下面這個配置添加到應用程序的上下文文件中(Springboot配置個相應的bean就可):

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

現在我們可以訪問/ test頁面并檢查日志:

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

總結一下,攔截器是一種可以應用到整個Spring生態系統中的servlet過濾器。它們可以在請求之前或之后啟動,也可以在視圖呈現之后啟動。它們也可以通過AsyncHandlerInterceptor接口的實現達到異步處理的效果。

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

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 230,182評論 6 543
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 99,489評論 3 429
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 178,290評論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,776評論 1 317
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,510評論 6 412
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,866評論 1 328
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,860評論 3 447
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 43,036評論 0 290
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,585評論 1 336
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 41,331評論 3 358
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,536評論 1 374
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,058評論 5 363
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,754評論 3 349
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,154評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,469評論 1 295
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,273評論 3 399
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,505評論 2 379

推薦閱讀更多精彩內容

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