Spring boot @WebFilter annotation is used to configure web filters that intercept and filter requests and responses based on the request url in the spring boot application. The @WebFilter will not work if the annotation @ServletComponentScan is not configured in the spring boot application. Use the annotations @WebFilter and @ServletComponentScan combined to make the spring boot filter work in the application.

The spring boot @WebFilter may not work due to incorrect parameter configuration in the annotation. The request url pattern should be set up according to the guidelines. The annotation @WebFilter may not work properly if the request url pattern is wrong or does not fit the pattern. The recommended url pattern should be defined in the spring boot annotation @WebFilter.

If you configure the request url pattern according to the guideline, the spring boot @WebFilter annotation will work. In addition, the @ServletComponentScan spring boot annotation should be used to scan all of the servlet components in the spring boot application.



Example of @WebFilter

The annotation @WebFilter should be used to configure a web filter in the spring boot application. The example below shows a simple web filter using the annotation @WebFilter.

package com.yawintutor;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;

@WebFilter("/test")
public class MyURLFilter implements Filter{

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterchain)
			throws IOException, ServletException {
		System.out.println("START doFilter2");
		filterchain.doFilter(request, response);
		System.out.println("END   doFilter2");
	}
}
package com.yawintutor;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

	@RequestMapping(value = "/test", method = RequestMethod.GET)
	public String testFilter() {
		System.out.println("Executing testFilter Method");
		return "TEST OK";
	}
}

Output

Executing testFilter Method


Solution 1

The spring boot filter is executed using the default spring boot application main class. In the spring boot application context, the @WebFilter is not scanned and loaded. The annotation @ServletComponentScan should be used in the spring boot application main class. The following example shows how to include the annotation @ServletComponentScan in the main method of a spring boot application. The @ServletComponentScan annotation will scan any servlet-related classes like as filters and so on when the spring boot application is started.

SpringBootFilterApplication.java

package com.yawintutor;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;

@SpringBootApplication
@ServletComponentScan
public class SpringBootFilterApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringBootFilterApplication.class, args);
	}
}

Output

START doFilter2
Executing testFilter Method
END doFilter2


Solution 2

In the annotation @WebFilter, the request url pattern is missing or invalid. As seen in the sample below, the request url pattern should be appended to the @WebFilter parameter. The “/” should be the first character in the request URI. It is necessary to specify the relative url. In the request url, the domain name should not be included. In the spring boot annotation @WebFilter, check the request url pattern. Correct the request url pattern if the url is incorrect.

MyURLFilter.java

package com.yawintutor;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;

@WebFilter("/test")
public class MyURLFilter implements Filter{

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterchain)
			throws IOException, ServletException {
		System.out.println("START doFilter2");
		filterchain.doFilter(request, response);
		System.out.println("END   doFilter2");
	}
}


Solution 3

If you want to use multiple url patterns in the annotation @WebFilter parameter, they must be separated by a comma. The pattern for multiple urls should match the guidelines. The first character in the request URI should be “/.” The request url pattern should be set as the prefix of the request url’s sub url. The request url pattern must be enclosed using the curly braces.

MyURLFilter.java

package com.yawintutor;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;

@WebFilter( {"/test","/hello"} )
public class MyURLFilter implements Filter{

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterchain)
			throws IOException, ServletException {
		System.out.println("START doFilter2");
		filterchain.doFilter(request, response);
		System.out.println("END   doFilter2");
	}
}


Solution 4

In the spring boot application, the invalid request url pattern will not work. In the request url pattern, regular expression patterns like * and? will not work. Only the “beginning of” match will match the request url pattern. The other patterns aren’t going to work.

@WebFilter( {"/test","/hell*"} )

In the spring boot application, the pattern “/hell*” will not work. In the annotation @WebFilter, the regular expression pattern is not supported in the request url. In the annotation @WebFilter, the example pattern below will work.

@WebFilter( {"/test","/hello/*"} )
@WebFilter( {"/*"} )

MyURLFilter.java

package com.yawintutor;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;

@WebFilter( {"/test","/hello/*"} )
public class MyURLFilter implements Filter{

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterchain)
			throws IOException, ServletException {
		System.out.println("START doFilter2");
		filterchain.doFilter(request, response);
		System.out.println("END   doFilter2");
	}
}



Leave a Reply