springboot加拦截器想必大家都很熟悉,我直接上两个代码
有问题的代码
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
private static final Logger log = LoggerFactory.getLogger(InterceptorConfig.class);
@Autowired
@Lazy
UserService userService;
@Override
public void addInterceptors(InterceptorRegistry registry) {
log.info("=====================添加拦截器=======================");
// 这种写法居然不行
String[] inPatterns = new String[]{"/page/**, /crop/**"};
String[] exPatterns = new String[]{"/auth/**", "/swagger-resources/**", "doc.html"};
registry.addInterceptor(new AuthenticationInterceptor())
.addPathPatterns(inPatterns)
.excludePathPatterns(exPatterns);
}
@Bean
public AuthenticationInterceptor authenticationInterceptor() {
return new AuthenticationInterceptor(userService);
}
}
这个代码,我的请求始终无法被拦截
请求是/page/test/aaa
下面是可用的代码。 区别只在于addPathPatterns直接传入多个字符串。
@Override
public void addInterceptors(InterceptorRegistry registry) {
log.info("=====================添加拦截器=======================");
registry.addInterceptor(authenticationInterceptor())
.addPathPatterns("/page/**","/crop/**")
.excludePathPatterns("/auth/**", "/swagger-resources/**", "doc.html");
}
这个就可以正常拦截到。
我可以保证其他代码没有任何改动。
环境:java8 + springboot 2.3.2.RELEASE
百思不得其解啊~~~~~~~~~~~