Adding an HTTP Header to the request in a servlet filter

Extend HttpServletRequestWrapper, override the header getters to return the parameters as well: public class AddParamsToHeader extends HttpServletRequestWrapper { public AddParamsToHeader(HttpServletRequest request) { super(request); } public String getHeader(String name) { String header = super.getHeader(name); return (header != null) ? header : super.getParameter(name); // Note: you can’t use getParameterValues() here. } public Enumeration getHeaderNames() { List<String> names … Read more

How to read request.getInputStream() multiple times

Working code based on the accepted answer. public class CustomHttpServletRequestWrapper extends HttpServletRequestWrapper { private static final Logger logger = Logger.getLogger(CustomHttpServletRequestWrapper.class); private final String body; public CustomHttpServletRequestWrapper(HttpServletRequest request) { super(request); StringBuilder stringBuilder = new StringBuilder(); BufferedReader bufferedReader = null; try { InputStream inputStream = request.getInputStream(); if (inputStream != null) { bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); char[] … Read more

Looking for an example for inserting content into the response using a servlet filter

The codebase I am using, calls the getOutputStream method, instead of getWriter when it processes the response, so the examples included in the other answer doesn’t help. Here is a more complete answer that works with both the OutputStream and the PrintWriter, even erroring correctly, if the writer is accessed twice. This is derived from … Read more

Servlet vs Filter

Use a Filter when you want to filter and/or modify requests based on specific conditions. Use a Servlet when you want to control, preprocess and/or postprocess requests. The Java EE tutorial mentions the following about filters: A filter is an object that can transform the header and content (or both) of a request or response. … Read more

How to define servlet filter order of execution using annotations in WAR

You can indeed not define the filter execution order using @WebFilter annotation. However, to minimize the web.xml usage, it’s sufficient to annotate all filters with just a filterName so that you don’t need the <filter> definition, but just a <filter-mapping> definition in the desired order. For example, @WebFilter(filterName=”filter1″) public class Filter1 implements Filter {} @WebFilter(filterName=”filter2″) … Read more

Can I exclude some concrete urls from inside ?

The standard Servlet API doesn’t support this facility. You may want either to use a rewrite-URL filter for this like Tuckey’s one (which is much similar Apache HTTPD’s mod_rewrite), or to add a check in the doFilter() method of the Filter listening on /*. String path = ((HttpServletRequest) request).getRequestURI(); if (path.startsWith(“/specialpath/”)) { chain.doFilter(request, response); // … Read more

How can I add a filter class in Spring Boot?

If you want to setup a third-party filter you can use FilterRegistrationBean. For example, the equivalent of web.xml: <filter> <filter-name>SomeFilter</filter-name> <filter-class>com.somecompany.SomeFilter</filter-class> </filter> <filter-mapping> <filter-name>SomeFilter</filter-name> <url-pattern>/url/*</url-pattern> <init-param> <param-name>paramName</param-name> <param-value>paramValue</param-value> </init-param> </filter-mapping> These will be the two beans in your @Configuration file: @Bean public FilterRegistrationBean someFilterRegistration() { FilterRegistrationBean registration = new FilterRegistrationBean(); registration.setFilter(someFilter()); registration.addUrlPatterns(“/url/*”); registration.addInitParameter(“paramName”, “paramValue”); registration.setName(“someFilter”); … Read more

Modify request parameter with servlet filter

As you’ve noted HttpServletRequest does not have a setParameter method. This is deliberate, since the class represents the request as it came from the client, and modifying the parameter would not represent that. One solution is to use the HttpServletRequestWrapper class, which allows you to wrap one request with another. You can subclass that, and … Read more

How to read and copy the HTTP servlet response output stream content for logging

You need to create a Filter wherein you wrap the ServletResponse argument with a custom HttpServletResponseWrapper implementation wherein you override the getOutputStream() and getWriter() to return a custom ServletOutputStream implementation wherein you copy the written byte(s) in the base abstract OutputStream#write(int b) method. Then, you pass the wrapped custom HttpServletResponseWrapper to the FilterChain#doFilter() call instead … Read more