How SpringCloud zuul Gateway Solves Cross Domain Problems

  • 2021-10-11 18:32:20
  • OfStack

Cross-domain

In SpringCloud, zuul and springboot must be configured at the same time to realize gateway processing across domains

Solve Access to XMLHttpRequest at 'http://192.168. 2.173: 8001/energy-base/groupType/getPageByType? timestamp=1557886425725' from origin 'http://localhost: 3000' has been blocked by CORS policy: The 'Access-Control-Allow-Origin' header contains contains multiple N.

Access-Control-Allow-Origin can only have 1 value solution

SpringBoot code


@Configuration
public class CorsConfig {
    @Bean
    public CorsFilter corsFilter() {
        final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        final CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(true); //  Allow cookies Cross-domain 
        config.addAllowedOrigin("*");// # That allows requests to be submitted to this server URI , * Indicates that all are allowed, in the SpringMVC If set to * Is automatically converted to the Origin
        config.addAllowedHeader("*");// # Header information allowed to access ,* Indicates all 
        config.setMaxAge(7200L);//  The cache time (in seconds) of the preview request, that is, during this time period, the same cross-domain request will not be previewed again 
        config.addAllowedMethod("*");//  The method that allows the request to be submitted, * Indicates that all are allowed 
        source.registerCorsConfiguration("/**", config);
        return new CorsFilter(source);
    }
}

zuul yml Configuration


zuul:
  # Header information that needs to be ignored is no longer propagated to other services 
  sensitive-headers: Access-Control-Allow-Origin
  ignored-headers: Access-Control-Allow-Origin,H-APP-Id,Token,APPToken
  max:
    host:
      connections: 5000 # Maximum request time 
  host:  # Wait 
    socket-timeout-millis: 60000
    connect-timeout-millis: 60000

Pits encountered in SpringCloud zuul

Recently, the author is researching and using SpringCloud and Zuul as the gateway of micro-service. In this micro-service gateway, we integrate rights management and routing forwarding to the back end. The micro-services in the back end are developed by SpringBoot and Python Django.

However, some service calls will return the following message

{"timestamp":"2020-09-07 11:45:38","status":500,
"error":"Internal Server Error","exception":"com.netflix.zuul.exception.ZuulException",
"message":"GENERAL"
}

The above error message does not appear every time, and not every back-end API will appear;

For those services that have a long return time.

The first reaction is to modify the connection time and read timeout time of the fuse.


ribbon.ConnectTimeout=60000
ribbon.ReadTimeout=60000

Moreover, all the configurations of the author are correct, but the results still do not take effect. Then the author upgraded the version of SpringCloud Zuul to the latest version, but still did not solve the problem.

Later, it was discovered that the dependence of spring-retry was not added to pom. xml, so the configured parameters of retry were not effective, and the log did not indicate the lack of dependence of spring-retry. Personally, I think this zuul code needs to be improved for 1 time. As long as the parameters of retry are filled in, it is necessary to judge whether there is dependence of spring-retry.

With the dependency of spring-retry, the problem is solved!


<dependency>
    <groupId>org.springframework.retry</groupId>
    <artifactId>spring-retry</artifactId>
</dependency>
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
</dependency>

Related articles: