SpringBoot e-Commerce project mall (25K + STAR) address: github.com/macrozheng/…

Abstract

As project architectures become more complex, new technologies are introduced, and new problems arise, this article describes a front-end invocation problem caused by gateways.

problems

After my Mall project was upgraded to microservice architecture, I joined the Gateway system based on Spring Cloud Gateway. When the front-end invokes relevant services, it should be called uniformly from the Gateway. I thought there was no problem with the front-end directly invoking the Gateway, but later I found that it would be impossible to invoke. Let’s take note of this problem and how to solve it.

Problem recurrence and resolution

Here we take the code in mall-swarm as an example to demonstrate the generation and solution of this problem.

  • First of all, we will start the services of mall-registry, mall-config, mall-gateway and mall-admin successively.

  • Then start the front-end project mall-admin-web;

  • The status code 403 is returned in the OPTIONS request. So far, we can’t see what the problem is.

  • It opens at ourConsoleLet’s see what we got wrong. Got itCORSThis key information can be identified as a cross-domain problem, the gateway does not support cross-domain;

  • The next step is to make the gateway cross-domain, inmall-gatewayAdd global cross-domain configuration to
/** * Created by macro on 2019/7/27
@Configuration
public class GlobalCorsConfig {

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

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

        return newCorsWebFilter(source); }}Copy the code
  • restartmall-gatewayService, log in again and discoverOPTIONSThe request was granted, butPOSTRequest inConsoleError:

  • Analyzing the problem, it should bemall-adminServices are repeatedly set up to allow cross-domain filters, as long as removedmall-adminGlobal cross-domain configuration can be;
** * Created by macro on 2019/7/27. */
//@Configuration
public class GlobalCorsConfig {

    /** * filters that allow cross-domain calls */
    @Bean
    public CorsFilter corsFilter(a) {
        CorsConfiguration config = new CorsConfiguration();
        // Allow all domain names to make cross-domain calls
        config.addAllowedOrigin("*");
        // Allow cookies to be sent across
        config.setAllowCredentials(true);
        // Release all original headers
        config.addAllowedHeader("*");
        // Allow all request methods to be invoked across domains
        config.addAllowedMethod("*");
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/ * *", config);
        return newCorsFilter(source); }}Copy the code
  • Restart themall-adminAfter the service, you can log in normally.

conclusion

The cross-domain problem occurs when the current application invokes services through the gateway. The solution is to configure global cross-domain configuration in the gateway service and remove the cross-domain configuration in related services.

The project address

Github.com/macrozheng/…

The public,

Mall project full set of learning tutorials serialized, attention to the public number the first time access.