聲明:原創文章,轉載請注明出處。http://www.lxweimin.com/p/0ff5c1fbf0cd
本文中的代碼詳見:https://github.com/hawkingfoo/springboot-interceptor
一、概述
攔截器的使用場景越來越多,尤其是面向切片編程流行之后。那通常攔截器可以做什么呢?
之前我們在Agent介紹中,提到過統計函數的調用耗時。這個思路其實和AOP的環繞增強如出一轍。
那一般來說,場景如下:
- 函數增強:比如對一個函數進行參數檢查,或者結果過濾等。甚至可以對函數就行權限認證。
- 性能監控:統計函數性能。
- 日志打點:比如在用戶登錄函數之前,打點統計PV等信息。
以及其他等等。
二、Spring的攔截器
無論是SpringMVC或者SpringBoot中,關于攔截器不得不提:
org.springframework.web.servlet.handler.HandlerInterceptorAdapter
public abstract class HandlerInterceptorAdapter implements AsyncHandlerInterceptor {
// 在目標方法執行前執行
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
return true;
}
// 在目標方法執行后執行,但在請求返回前,我們仍然可以對 ModelAndView進行修改
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)
throws Exception {}
// 在請求已經返回之后執行
@Override
public void afterCompletion(
HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {}
// 用來處理異步請求, 當Controller中有異步請求方法的時候會觸發該方法
@Override
public void afterConcurrentHandlingStarted(
HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {}
}
三、實現一個用于驗證簡單權限的攔截器
1、自定義一個權限注解 @Auth
@Inherited
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Auth {
String user() default "";
}
-
@Inherited
:在使用此自定義注解時,如果注解在類上面時,子類會自動繼承此注解,否則,子類不會繼承此注解。這里一定要記住,使用Inherited聲明出來的注解,只有在類上使用時才會有效,對方法,屬性等其他無效。 -
@Target
:表示此注解可以放置的位置。常見的位置有:TYPE=枚舉或注解上,FIELD=字段上,METHOD=方法上,PARAMETER=函數形參列表中,CONSTRUCTOR=構造函數上,LOCAL_VARIABLE=局部變量上 等等其他位置。 -
@Retention
:此注解的生命周期。常見的有:SOURCE=源碼時期;CLASS=字節碼時期(已編譯);RUNTIME=運行時期,通常是用這個的時候要多。 -
@Documentd
:生成注解文檔。
2、在Controller的方法上添加注解
上一步添加完注解后,之后要在你所使用的方法上添加相關注解,如下。
@RestController
@EnableAutoConfiguration
public class DemoController {
@Auth(user = "admin")
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String sayHello() {
return "hello world.";
}
}
3、實現攔截器功能
需求:我們在用戶通過/hello這個URI訪問時,對其進行驗證,如果為admin則放行,否則拒絕,假設用戶的身份在URL參數中。
思路:因此我們要在執行sayHello()
之前,對用戶做出驗證。如果其身份與注解中的身份相同,則放行。因此我們要在preHandle()
中做文章。
難點:我們怎么拿到Controller 方法上的@Auth這個注解呢?看PreHandle()
的三個參數,貌似也沒有哪個可以提供Controller類中的注解。
其實,第三個參數handler,一般情況下其類型為:org.springframework.web.method.HandlerMethod
類型,而這里面含有注解的相關信息。
為什么這么說呢?
在SpringBoot中,注解的默認類型為函數級,而在SpringMVC其默認類型為Controller對象級別。
因此,如果在SpringMVC中需要在dispatcher-servlet.xml中配置:
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
,這樣其類型才為HandlerMethod
。
我們看下具體實現邏輯:
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("preHandle");
if (!handler.getClass().isAssignableFrom(HandlerMethod.class)) {
System.out.println("cat cast handler to HandlerMethod.class");
return true;
}
// 獲取注解
Auth auth = ((HandlerMethod) handler).getMethod().getAnnotation(Auth.class);
if (auth == null) {
System.out.println("cant find @Auth in this uri:" + request.getRequestURI());
return true;
}
// 從參數中取出用戶身份并驗證
String admin = auth.user();
if (!admin.equals(request.getParameter("user"))) {
System.out.println("permission denied");
response.setStatus(403);
return false;
}
return true;
}
其實實現邏輯就兩點:從參數中取出身份,和注解中的進行比對。
4、配置攔截器
那怎么讓剛才的這個攔截器生效呢?
這個時候,需要我們配置:WebMvcConfigurerAdapter
具體實現如下:
@Configuration
public class ConfigAdapter extends WebMvcConfigurerAdapter {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/hello");
}
}
注意:這里有兩點需要注意,一個是@Configuration
這個注解,這樣才能讓SpringBoot服務發現這個配置;另一個是配置匹配項,這里是對"/hello"這個進行攔截。("/**"是對所有的訪問攔截)
四、運行
訪問 http://127.0.0.1:8080/hello?user=admin就可以看到結果啦。