今天用springboot寫了一個註解攔截器,一直不生效,網上各種方式我都嘗試過了,都不起作用,找了好久總算知道是什麼原因了
我的annotation:
@Inherited
@Target({
ElementType.TYPE, ElementType.METHOD
})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface SignVerify {
}
我的intercept:
@Component
public class SodaJsSignInterceptor extends HandlerInterceptorAdapter {
private static final MinaSyncConfiguration config = ZKConfigurationFactory.getConfig(MinaSyncConfiguration.class);
private static final String SIGN_PARAM_NAME = "sign";
private static final Logger LOGGER = LoggerFactory.getLogger(DevController.class);
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
HandlerMethod handlerMethod = (HandlerMethod) handler;
Method method = handlerMethod.getMethod();
SignVerify signVerify = method.getAnnotation(SignVerify.class);
if (signVerify == null) {
return true;
}
return false;
}
}
我的config,問題就是出在config上面:
@Configuration
public class SodaJsSignConfig extends WebMvcConfigurationSupport {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new SodaJsSignInterceptor()).addPathPatterns("/**");
super.addInterceptors(registry);
}
}
到這裏所以操作都沒有問題,該加的註解我也都加了,最後我發現我有兩個攔截器,所以我寫了兩個config
當我把兩個config的內容寫到一個config裏面以後,攔截器就生效了。。。。