I have used spring cloud to build a multiple microservice,and i use a API-Gateway implemented using Spring Cloud Netfix's Zuul Server to route the requests to our micro services ,the gateway config like this:
- application.yml:
server:
port: 8021
ribbon:
ConnectTimeout: 3000
ReadTimeout: 60000
zuul:
ignoredServices: "*"
add-proxy-headers: true
#prefix: /v1
routes:
m_test:
path: /api/testService/**
sensitiveHeaders: "*"
url: http://127.0.0.1:4008/testService/
eureka:
instance:
hostname: gateway
client:
registerWithEureka: true
fetchRegistry: true
serviceUrl:
defaultZone: http://127.0.0.1:8761/eureka/
- CorsFilter.java:
@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
public class CorsFilter implements Filter {
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse) res;
HttpServletRequest request = (HttpServletRequest) req;
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "POST, PUT, GET, OPTIONS, DELETE");
response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization,Content-length");
response.setHeader("Access-Control-Max-Age", "1800");
Map<String, String> map = getHeadersInfo(request);
if (request.getMethod().equalsIgnoreCase("OPTIONS")) {
response.setStatus(HttpServletResponse.SC_OK);
} else {
chain.doFilter(request, response);
}
}
public void init(FilterConfig filterConfig) {
}
public void destroy() {
}
private Map<String, String> getHeadersInfo(HttpServletRequest request) {
Map<String, String> map = new HashMap<>();
Enumeration headerNames = request.getHeaderNames();
while (headerNames.hasMoreElements()) {
String key = (String) headerNames.nextElement();
String value = request.getHeader(key);
map.put(key, value);
}
return map;
}
}
- GatewaySystemApplication.java:
@SpringBootApplication
@EnableZuulProxy
@EnableEurekaClient
public class GatewaySystemApplication {
public static void main(String[] args) throws Exception {
//SpringApplication.run(GatewaySystemApplication.class, args);
new SpringApplicationBuilder(GatewaySystemApplication.class).web(true).run(args);
}
}
The browser response headers are like this: passing through gateway layer response headers
And the response header of my request miss the content-length, but it has the content-length when i directly invoke backend service.directly response headers