猿问

springboot2 拦截器无效

在网上找了好多,基本上都一个样,按照一步一步来,都是无效,下面是我的配置:
Config:
importcom.example.demo.interceptor.OneInterceptor;importorg.springframework.context.annotation.Configuration;importorg.springframework.web.servlet.config.annotation.InterceptorRegistry;importorg.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@ConfigurationpublicclassInterceptorConfigimplementsWebMvcConfigurer{
@Override
publicvoidaddInterceptors(InterceptorRegistryregistry){
registry.addInterceptor(newOneInterceptor()).addPathPatterns("/**");
}
}
OneInterceptor:
importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importorg.springframework.stereotype.Component;importorg.springframework.web.servlet.HandlerInterceptor;importorg.springframework.web.servlet.ModelAndView;
importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;
@ComponentpublicclassOneInterceptorimplementsHandlerInterceptor{
privateLoggerlogger=LoggerFactory.getLogger(OneInterceptor.class);
@Override
publicbooleanpreHandle(HttpServletRequestrequest,HttpServletResponseresponse,Objectobject)throwsException{
logger.debug("拦截。。。");
returnfalse;
}
@Override
publicvoidpostHandle(HttpServletRequestrequest,HttpServletResponseresponse,Objecthandler,ModelAndViewmodelAndView){
System.err.println("post");
}
@Override
publicvoidafterCompletion(HttpServletRequestrequest,HttpServletResponseresponse,Objecthandler,Exceptionex){
System.err.println("after");
}
}
无论访问什么路径都不会走拦截器,不知道哪里配置错了,求大神解答!
隔江千里
浏览 2025回答 2
2回答

慕虎7371278

你这配置没问题的,你看看日志有没有logger.debug("拦截。。。");另外,声明OneInterceptor为组件之后,不需要new,使用的时候注入就行
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答