-1

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);
    }
    }
BugKiller
  • 94
  • 1
  • 10

1 Answers1

-3

Well, you aren't setting Content-Length.

(FWIW, your Content-Type is bogus as well).

Julian Reschke
  • 40,156
  • 8
  • 95
  • 98
  • I use this 'String contentType=Files.probeContentType(Paths.get(filepath))' to get the contentType,and the browser response content-type is change to like this 'Content-Type application/zip;charset=UTF-8' ,but there is still no 'content-length' in response headers. ps:thank you for your answer – BugKiller Oct 21 '16 at 08:23