[英]spring-boot interceptor is not intercepting
I have written an interceptor for my spring-boot app.我为我的 spring-boot 应用程序编写了一个拦截器。 But when I hit the endpoint, its executing fine.The interceptor is not able to intercept my request.
但是当我到达端点时,它执行得很好。拦截器无法拦截我的请求。 Where I am going wrong or missing something?
我哪里出错或遗漏了什么?
Below is the Code下面是代码
@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);
}
}
Here is the code for Interceptor:::这是拦截器的代码:::
@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);
}
}
}
Now when I hit the endpoint for my spring-boot app say, its working fine现在,当我点击我的 spring-boot 应用程序的端点时,它工作正常
http://localhost:8181/services/api/companies
Basically its not at all calling the prehandle.基本上它根本不调用前处理。 What am I missing????
我错过了什么????
Did you used @EnableWebMvc
as你使用
@EnableWebMvc
作为
@EnableWebMvc
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
...
}
In my case the use of a MappedInterceptor
as described in the answer below worked.在我的情况下,使用下面答案中描述的
MappedInterceptor
是有效的。
https://stackoverflow.com/a/35948730/1705048 https://stackoverflow.com/a/35948730/1705048
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.