Best Practice to skip URL pattern's from getting applied servlet filter

30 views Asked by At

What could be the best practice for Excluding a list of URL patterns from below servlet filter?

Use case: Create a whitelist of URLs that could be skipped through the below Http servlet filter.

@WebFilter(urlPatterns = "/*")
public class SanitizeReqestParamFilter implements Filter {
  @Override
  public void init(FilterConfig filterConfig) throws ServletException {}
  @Override
  public void destroy() {}
  @Override
  public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
      throws ServletException, IOException {
    // logic for sanitize the request params.
    filterChain.doFilter(servletRequest, servletResponse);
  }
}

Note: Currently, I have a list of URLs in an enum and check if the request URL is present in that list or not.

I would like to use some other method something like,

  1. adding this list on filter registration so that it could exclude those URLs.
  2. Or have some custom annotation on the servlet that could help to identify skipped URLs.
0

There are 0 answers