Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get request header in a Filter

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
 }
like image 933
royB Avatar asked Sep 03 '18 12:09

royB


People also ask

How to search for requests with a given response header?

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.

How do I use the <scanheaders> element in request filtering rules?

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.

How do I create a filtering rule for HTTP requests?

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.

How to configure general request-filter options in IIS?

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.


1 Answers

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() {
}
}
like image 62
Alexander Petrov Avatar answered Oct 23 '22 06:10

Alexander Petrov