Before and after the end of the separation project, the introduction of a Spring Cloud Gateway problem encountered!

SpringBoot actual electricity supplier item mall (25k + star) Address: github.com/macrozheng/...

Summary

With more and more complex infrastructure projects, the introduction of new technology, new problems are generated, this article will tell a front-end gateway calls the problem caused due.

Problems

I malllater upgrade the project to the micro-service architecture, adding a gateway system based on the Spring Cloud Gateway, a unified front-end should be called the gateway when calling from related services, the thought that the front end of a direct call Gateway lacks the problem, and later found to produce the situation can not be called , let's record this and Solutions.

And solve the problem recurs

Here we are with mall-swarmthe generated code as an example to demonstrate the problem and solved under.

  • First, we put mall-registry, mall-config, mall-gateway, mall-adminthese services in order to start up;

  • And then start the front end project mall-admin-web;

  • Visit the login page to log in operation, found it impossible to log in, OPTIONSrequest returns a status code 403, this we can not see what the problem is coming;

  • We point to open Consolea look at the report in the end what was wrong and found CORSthis crucial information, you can determine is to produce a cross-domain problem, the gateway does not support cross-domain;

  • Then as long as the gateway to support cross-domain can, in mall-gatewayadding to global cross-domain configuration:
/**
 * 全局跨域配置
 * 注意:前端从网关进行调用时需要配置
 * Created by macro on 2019/7/27.
 */
@Configuration
public class GlobalCorsConfig {

    @Bean
    public CorsWebFilter corsFilter() {
        CorsConfiguration config = new CorsConfiguration();
        config.addAllowedMethod("*");
        config.addAllowedOrigin("*");
        config.addAllowedHeader("*");

        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser());
        source.registerCorsConfiguration("/**", config);

        return new CorsWebFilter(source);
    }

}
复制代码
  • Restart the mall-gatewayservice, log the operation again, I found OPTIONSthe request although passed, but the POSTrequest Consolein it being given:

  • Analysis of this problem should be the mall-adminservice that duplicate set of questions to allow cross-domain filter, as long as the removal of mall-adminthe cross-domain configuration to global;
/**
 * 全局跨域配置
 * 注意:前端从网关进行调用时不需要配置
 * Created by macro on 2019/7/27.
 */
//@Configuration
public class GlobalCorsConfig {

    /**
     * 允许跨域调用的过滤器
     */
    @Bean
    public CorsFilter corsFilter() {
        CorsConfiguration config = new CorsConfiguration();
        //允许所有域名进行跨域调用
        config.addAllowedOrigin("*");
        //允许跨越发送cookie
        config.setAllowCredentials(true);
        //放行全部原始头信息
        config.addAllowedHeader("*");
        //允许所有请求方法跨域调用
        config.addAllowedMethod("*");
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", config);
        return new CorsFilter(source);
    }
}
复制代码
  • Restart mall-adminthe service discovery can be a normal logged.

to sum up

The current terminal will generate a cross-domain problems when calling the service through the gateway, the solution is global cross-domain configuration in the Gateway service, and related services if there is a cross-domain configuration should be removed.

project address

github.com/macrozheng/…

the public

mall project a full tutorial serialized in public concern number the first time to obtain.

No public picture

Guess you like

Origin juejin.im/post/5e13248e5188253ab849d125