Custom header param – Spring Boot

Problem solved.

I have a CORS Filter in my application. I had to add my custom header param to the

Access-Control-Allow-Headers

list to allow it. After that, the endpoint was consumed and worked as expected.

This is how my Filter is now:

@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
public class SimpleCORSFilter implements Filter {

    private final Log logger = LogFactory.getLog(this.getClass());

    @Override
    public void init(FilterConfig fc) throws ServletException {
        logger.info("SimpleCORSFilter loaded");
    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
        HttpServletResponse response = (HttpServletResponse) resp;
        HttpServletRequest request = (HttpServletRequest) req;
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, PUT");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", 
                "x-requested-with, authorization, Content-Type, " +
                "Authorization, credential, X-XSRF-TOKEN, idEmpresa");

        if ("OPTIONS".equalsIgnoreCase(request.getMethod())) {
            response.setStatus(HttpServletResponse.SC_OK);
        } else {
            chain.doFilter(req, resp);
        }

    }

    @Override
    public void destroy() {
    }
}

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top