我为我的 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 {
...
}
关于java - spring-boot拦截器没有拦截,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49055909/