I would do something like this:
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
@Component
public class MyFilter implements Filter{
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
String header = httpServletRequest.getHeader("YourHeader");
// do more stuff with it
filterChain.doFilter(httpServletRequest, servletResponse);
}
@Bean
public FilterRegistrationBean<MyFilter> myFilterRegistration(){
FilterRegistrationBean<MyFilter> registrationBean
= new FilterRegistrationBean<>();
registrationBean.setFilter(new MyFilter());
registrationBean.addUrlPatterns("/registerUser/*");
return registrationBean;
}
}
MyFilter is a filter that does whatever is needed with the request and its headers. It needs to be typecasted to HttpServletRequest because a base ServletRequest doesn't know nothing about headers. So depending on the application you may need to do type check.
Then I define a bean of type FilterRegistrationBean that registers MyFilter and uses an url pattern to apply it only in some cases, like it seems you need.