Spring安全定制过滤器两次被REST控制器调用

时间:2020-04-02 08:32:09

标签: java spring spring-security spring-filter

我实现了一个自定义的Spring Security Filter,以具有一个自定义的Authentication系统。效果很好。

要配置我的过滤器,我使用了以下配置:

@Override
protected void configure(HttpSecurity http) throws Exception {


    http.antMatcher("/api/**")
            .csrf()
            .disable()
            .headers()
            .frameOptions()
            .disable()
            .and()
            .sessionManagement()
            .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
            .and()
            .authorizeRequests()
            .antMatchers("/api/**")
            .authenticated().and().addFilterBefore(new MyTokenAuthFilter(), AbstractPreAuthenticatedProcessingFilter.class);

如果我尝试在此过滤器中放置一个断点,则会看到对于每个REST请求,方法doFilterInternal都会被调用两次。 奇怪..有什么建议吗?

1 个答案:

答案 0 :(得分:0)

对于那些遇到此问题的人,我找到了原因:

我将自定义过滤器声明为@Component。

这不是必需的,在我看来,这将产生双重过滤器注册。