I registered a Filter in Spring boot and been trying to get and Header from the request but getting null.
@Component
@Order(1)
public class ExampleFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws... {
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
//null**
final String header = httpServletRequest.getHeader(HEADER);
}
BUT
@GetMapping(value = "example")
public ResponseEntity<Example> example( @RequestHeader(HEADER) String header) {
... the header is NOT null
}
For example it is possible to search for requests with a given response header like this has-response-header:<header> For a quick way to see filtering options without looking up the reference you can type ctrl + space into the filter field in order to see the available options.
The <scanHeaders> element of the <filteringRule> element defines a collection of HTTP headers that a request filtering rule will scan for strings that are specified in the <denyStrings> collection. The <scanHeaders> element contains a series of <add> elements, each of which specifies a unique HTTP header to add to the collection.
Enter a friendly name for the filtering rule in the Name field. Select Scan url if you want the filtering rule to scan the URL stub for the request. Select Scan query string if you want the filtering rule to scan the query string for the request. Enter any HTTP headers to scan in the Scan Headers collection.
To configure general request-filter options by using the UI. Open IIS Manager and select the level for which you want to configure request filter. In Features View, double-click Request Filtering. In the Actions pane, click Edit Feature Settings. In the Edit Request Filtering Settings dialog, edit the settings as desired, and then click OK.
Please check my example here
@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
public class CustomFilter implements Filter {
public CustomFilter() {
}
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse) res;
HttpServletRequest request = (HttpServletRequest) req;
HttpServletRequest httpRequest = (HttpServletRequest) request;
Enumeration<String> headerNames = httpRequest.getHeaderNames();
if (headerNames != null) {
while (headerNames.hasMoreElements()) {
String name = headerNames.nextElement();
System.out.println("Header: " + name + " value:" + httpRequest.getHeader(name));
}
}
chain.doFilter(req, res);
}
@Override
public void init(FilterConfig filterConfig) {
}
@Override
public void destroy() {
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With