解决办法:
1、增加配置 :
package com.caacitc.duty.configuration;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
@Component
@Order(1)
public class CorsConfig2 implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
// TODO Auto-generated method stub
}
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse) res;
HttpServletRequest reqs = (HttpServletRequest) req;
//// System.out.println(reqs.toString());
// System.out.println(reqs.getRequestURI());
// System.out.println(reqs.changeSessionId());
// System.out.println(reqs.getCookies()[0].getPath());
// System.out.println(“print:”+reqs.getHeader(“Origin”));
// response.setHeader(“Access-Control-Allow-Origin”, reqs.getHeader(“Origin”));
response.setHeader(“Access-Control-Allow-Origin”, reqs.getHeader(“Origin”));
response.setHeader(“Access-Control-Allow-Credentials”, “true”);
response.setHeader(“Access-Control-Allow-Methods”, “PUT,POST, GET, OPTIONS, DELETE”);
response.setHeader(“Access-Control-Max-Age”, “3600”);
response.setHeader(“Access-Control-Allow-Headers”,
“Origin, No-Cache, X-Requested-With, If-Modified-Since,”
+ " Pragma, Last-Modified, Cache-Control, Expires, Content-Type, "
+ "X-E4M-With
2、如果是通过Nginx(前端页面)—>web容器层—>网关各个微服务的模式:且利用zuul的转发功能实现web容器层向网关路由,还需要增加以下配置:
zuul:
sensitive-headers:
ignored-headers: Access-Control-Allow-Credentials, Access-Control-Allow-Origin
routes:
gateway:
path: /forward/**
sensitiveHeaders:
serviceId: ${application.gateway}
如果不增加,Access-Control-Allow-Credentials,
Access-Control-Allow-Origin,会有多个导致转发失败。