Q:

Suppress Vary:* response headers generated by Spring Boot

Our Spring Boot app generates Vary response headers for the transfer of static resources (also for REST API calls by the way). I would like to suppress this, but I find no configuration option to do this.
The following headers are generated:
Vary:Origin
Vary:Access-Control-Request-Method
Vary:Access-Control-Request-Headers

Setting and playing with various cache properties spring.resources.cache.* was not successful. The spring.resources.cache property is described in https://docs.spring.io/spring-boot/docs/current/reference/html/appendix-application-properties.html.
Is there a valid options to supress the generation of theses headers?

A:

Let's check this hack to supress every header in HttpServletResponse.
By default it is not possible to remove header from HttpServletResponse, the interface doesn't support remove header or something else.
Using a custom HttpFilter it is possible to supress adding headers by doing a litle hack. 
In the filter chain you can add an HttpServletResponseWrapper and overrite the addHeader and setHeader methods. Now it's the custom logic which haeder are set in HttpServletResponse. Make sure that your filter is one of the first filter wthin the chain.
Check this example @Component if you are using Spring Boot (this trick is also possible with Java EE, therefor please use Java EE filter spec to enable the filters):
package com.example.demo;

import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import java.io.IOException;

@Component
public class VaryHeaderFilter implements Filter {

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        chain.doFilter(request, new HttpServletResponseWrapper((HttpServletResponse) response) {
            @Override
            public void addHeader(String name, String value) {
                if (!HttpHeaders.VARY.equals(name)) {
                    super.addHeader(name, value);
                }
            }

            @Override
            public void setHeader(String name, String value) {
                if (!HttpHeaders.VARY.equals(name)) {
                    super.setHeader(name, value);
                }
            }
        });
    }
}

