CORS:飞行前响应中Access-Control-Allow-Headers不允许请求标头字段access-control-allow-origin

时间:2019-12-23 10:39:52

标签: spring-boot cors

我正在研究Spring Boot应用程序。通过REST API与UI调用进行通信。

我正在以请求类型POST调用API。 它给了我一个例外:

Request header field access-control-allow-origin is not allowed by Access-Control-Allow-Headers in preflight response.

我添加了以下过滤器实现:

@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {

      HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;

        response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With, remember-me");

        chain.doFilter(req, res);
}

仍然面临同样的问题。

请提出建议。

0 个答案:

没有答案