javaspring-bootspring-security

Filter executing twice


here is the filter class. Using @Component annotation because I want to read the value auth.key from application.yml file.

@Component
public class StaticKeyAuthFilter implements Filter {

    @Value("${auth.key}")
    private String authKey;

    @Override
    public void doFilter(ServletRequest servletRequest,
                         ServletResponse servletResponse,
                         FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;

        String authorization = request.getHeader("Authorization");
        System.out.println("StaticKeyAuthFilter:" + request.getRequestURI() );

        if (authorization == null || !authorization.equals(authKey)) {
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
            return;
        }

        filterChain.doFilter(request, response);

    }
}

Next, I am registering it in the config class.

@Configuration
public class ProjectConfig {
    @Autowired
    private StaticKeyAuthFilter staticKeyAuthFilter;
    
    @Bean
    SecurityFilterChain configure(HttpSecurity httpSecurity) throws Exception {

        httpSecurity.authorizeHttpRequests(c -> {
            c.anyRequest().permitAll();
        });


        httpSecurity.addFilterAt(staticKeyAuthFilter, BasicAuthenticationFilter.class);
        return httpSecurity.build();
    }
}

Now, when I visit any controller I get the following output:

StaticKeyAuthFilter:/category/bb
StaticKeyAuthFilter:/category/bb

As you can see, it is executing twice. what am I doing wrong?


Solution

  • As soon as you register a Filter bean as a component, it's already added to the regular filter chain. In addition, you manually added it to the SecurityFilterChain as well, which causes it to be registered and called twice.

    Potential solutions: