Search code examples
javaspring-bootspring-securityjwt

Can multiple filters be added to Spring Security configuration?


This is an issue I've been struggling with for the past week, and I need answers before I go insane. I'm trying to do a specific task every time an IP connects to our system. Without going into much detail, my working solution is to implement this via our WebSecurityConfigurationAdapter, which I've proven reliably goes off when an IP connects. Following my efforts, the code looks something like this:

// this block of code was here originally
@Bean
public JwtAuthenticationFilter jwtAuthenticationFilter() {
    return new JwtAuthenticationFilter();
}

// this block is my addition
@Bean
public IPFilter ipFilter() {
    return new IPFilter();
}

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.//there's a HUGE bulk of configuring the HttpSecurity object here that I'm not gonna share

    // the jwt filter existed previously and worked fine. I have added ipFilter()
    http.addFilterBefore(jwtAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class);
    http.addFilterAfter(ipFilter(), JwtAuthenticationFilter.class);
}

My original plan was to try and implement this task into the existing Jwt filter, but various dev-related complications led to the idea of implementing a separate filter to add on top of this one. It seemed like a simple case of adding a line just like this one but with the new filter.

Well, five business days and 40 hours of lost dev time later, I'm still trying to figure out what's wrong. When I add this new filter, I get a bunch of (unhelpful) errors and the application generally just... does not work. I've narrowed my issue down to the act of adding this new filter, but the filter itself is extremely simplistic:

public class IPFilter extends OncePerRequestFilter {
    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        AuditLogger.log(AuditMessageType.IP_CONNECT, "Detecting connection from IP Address: " + request.getRemoteAddr());
        log.trace("Request from: " + request.getRemoteAddr());
    }
}

I am thus fully at a loss as to why, whenever I specify to add this filter to the stack, the system always crashes and burns. I've attempted to look up examples of this sort of thing online and have turned up nothing. This leads me to wonder if this is even possible to do, and if not then why so?


Solution

  • A coworker of mine decided to add filterChain.doFilter(request, response); to the code and that solved the issue. According to them, it was otherwise returning "empty 200 responses", something I wasn't aware of because I hadn't spotted that in the output logs.

    The reason why I hadn't put this in the code in the first place was that this problem was occurring while that line was in the code and I assumed it was unnecessary because the "filter" wasn't intended to filter anything and serve instead as an injection of logging logic.