在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容器不一定運(yùn)行在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中的處理攔截器
極樂科技:知乎專欄