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 mall
later 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-swarm
the generated code as an example to demonstrate the problem and solved under.
-
First, we put
mall-registry
,mall-config
,mall-gateway
,mall-admin
these 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,
OPTIONS
request returns a status code403
, this we can not see what the problem is coming;
- We point to open
Console
a look at the report in the end what was wrong and foundCORS
this 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-gateway
adding 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-gateway
service, log the operation again, I foundOPTIONS
the request although passed, but thePOST
requestConsole
in it being given:
- Analysis of this problem should be the
mall-admin
service that duplicate set of questions to allow cross-domain filter, as long as the removal ofmall-admin
the 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-admin
the 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
the public
mall project a full tutorial serialized in public concern number the first time to obtain.