title: Spring differentiates path with and without trailing slash when applying filters
body: <p>I'm working on a Spring Boot application and I want to apply some filters in some specific URLs, for this I'm implementing the <code>Filter</code> interface (code below) and using <code>FilterRegistrationBean</code> where I have the method <code>setUrlPatterns</code> to define the endpoints that use the filter. I've created an example application from scratch and I set up my filter for <code>/hello</code>, but the filter is not applied for <code>/hello/</code>.</p>
  <p>It makes the necessity of change the line from
  <code>filterRegistrationBean.setUrlPatterns(List.of(&quot;/hello&quot;));</code>
  to
  <code>filterRegistrationBean.setUrlPatterns(List.of(&quot;/hello&quot;, &quot;/hello/&quot;));</code>. It resolves my problem, but I don't want to duplicate the path only to include the trailing slash.</p>
  <p><strong>Is there a better way to do this instead write <code>List.of(&quot;/hello&quot;, &quot;/hello/&quot;)</code>?</strong></p>
  <p>Code:</p>
  <pre><code>import javax.servlet.*;
  import java.io.IOException;
  
  public class LogFilter implements Filter {
  
      @Override
      public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
          System.out.println(&quot;-----------------FILTER-----------------&quot;);
          chain.doFilter(request, response);
      }
  
      // ... other methods
  }
  </code></pre>
  <pre><code>import com.example.demo.config.filters.LogFilter;
  import org.springframework.boot.web.servlet.FilterRegistrationBean;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  
  import java.util.List;
  
  @Configuration
  public class WebConfig {
  
      @Bean
      public FilterRegistrationBean&lt;LogFilter&gt; logFilter() {
          var filterRegistrationBean = new FilterRegistrationBean&lt;&gt;(new LogFilter());
          filterRegistrationBean.setUrlPatterns(List.of(&quot;/hello&quot;));
          return filterRegistrationBean;
      }
  }
  </code></pre>
  <pre><code>import org.springframework.web.bind.annotation.GetMapping;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RestController;
  
  @RestController()
  @RequestMapping(&quot;/hello&quot;)
  public class HelloController {
  
      @GetMapping
      public String hello() {
          return &quot;Hello&quot;;
      }
  }
  </code></pre>
  <p>Research:</p>
  <ul>
  <li>There is another way to define filters, enabling <code>@ServletComponentScan</code>, create a class that implements <code>Filter</code> and annotate it with <code>@WebFilter(urlPatterns = {&quot;/hello&quot;})</code>, but the result is the same.</li>
  <li>I could use <code>/hello/*</code>, it will consider the path with and without trailing slash, but i don't want to apply the filter for everything else like <code>/hello/abc/</code>, I want to apply the filter only exactly for <code>/hello</code> (and <code>/hello/</code>).</li>
  </ul>
  <p>I did debug on the embeded Tomcat and I found the <a href="https://github.com/apache/tomcat/blob/main/java/org/apache/catalina/core/ApplicationFilterFactory.java#L190" rel="nofollow noreferrer">ApplicationFilterFactory</a> where it uses some logic to decide when apply filter or not in a path, it looks duplicate the path is the unique solution, but in this example I'm using only one path, imagine you have 20..30 paths for this filter, once duplicated to include trailing slash it becomes 40..60, so I'm trying to find another solution for this (I'm not sure if there is another solution because of this <code>ApplicationFilterFactory</code> implementation on the embeded tomcat).</p>