感谢@dur询问我是否正在使用Spring Boot,这使我找到了解决方案。
这使我开始考虑Spring Boot如何自动为我们自动创建bean,最终成为这里的罪魁祸首。事实证明,我的JwtAuthenticationFilter
类是由Spring Boot自动放入过滤器链中的,但是当我在安全配置中显式声明它时,它也被包含在安全过滤器链中。因此,尽管我正确地排除/auth/token
了ignoring()
安全性配置中的方法,但这还不足以阻止过滤器在Spring Boot本身的上下文中发生。解决方案是配置一个显式阻止Spring Boot添加它的bean。
@Bean
public RegistrationBean jwtAuthFilterRegister(JwtAuthenticationFilter filter) {
FilterRegistrationBean registrationBean = new FilterRegistrationBean(filter);
registrationBean.setEnabled(false);
return registrationBean;
}