我正在研究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);
}
仍然面临同样的问题。
请提出建议。