[英]spring-boot interceptor is not intercepting
我為我的 spring-boot 應用程序編寫了一個攔截器。 但是當我到達端點時,它執行得很好。攔截器無法攔截我的請求。 我哪里出錯或遺漏了什么?
下面是代碼
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
private static final String PATHS = "/services/api/**";
@Autowired
private AuthorizationInterceptor authInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(authInterceptor).addPathPatterns(PATHS);
}
}
這是攔截器的代碼:::
@Component
public class AuthorizationInterceptor implements HandlerInterceptor {
private static final Logger LOGGER = LoggerFactory.getLogger(AuthorizationInterceptor.class);
private static final String MSG_BAD_INPUT = "Very Bad Input";
private static final int MAX_URI_LENGTH = 4;
@Override
public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
throws Exception {
// TODO Auto-generated method stub
}
@Override
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
throws Exception {
// TODO Auto-generated method stub
}
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
System.out.println("Inside Prehandle:::::::------->");
this.checkURILength(request);
System.out.println("After checking:::::::::::---->");
return true;
}
private void checkURILength(HttpServletRequest request) {
if (request.getRequestURI().length() > MAX_URI_LENGTH) {
LOGGER.error("Request URI is too long");
throw new InvalidInputException(MSG_BAD_INPUT);
}
}
}
現在,當我點擊我的 spring-boot 應用程序的端點時,它工作正常
http://localhost:8181/services/api/companies
基本上它根本不調用前處理。 我錯過了什么????
你使用@EnableWebMvc
作為
@EnableWebMvc
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
...
}
在我的情況下,使用下面答案中描述的MappedInterceptor
是有效的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.