SpringBoot中的攔截器Interceptor(隨筆)
1)定義攔截器:定義一個攔截器類實作HandlerInterceptor介面,重寫preHangle()方法;
public class OneInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//獲得攔截的uri并輸出
System.out.println("這里經過了攔截器"+request.getRequestURI());
return true;
}
}
2)注冊攔截器:定義一個類繼承自WebMvcConfigurationSupport,重寫addInterceptors()方法,并在該類上使用@Configuration注解,表明該類為配置類,
@Configuration //表明當前類為配置類
public class OneInterceptorConfig extends WebMvcConfigurationSupport {
//注冊攔截器
@Override
protected void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new OneInterceptor())
// .addPathPatterns("/rjxy/**"); //攔截/rjxy相關的uri請求
.excludePathPatterns("/rjxy/**"); //指定不進行攔截的uri,但攔截其他uri請求
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/274709.html
標籤:java
