About cross-domain problem please read my another article: blog.csdn.net/fsjwin/arti…

Here’s another encoding method (not nginx’s solution, mind you) that tells the browser it’s ready for cross-domain access.

1. Add Filter to the gateway service

@Component
public class MyCorsConfiguration {

    / * * * *@returnCorsWebFilter has the Spring framework to provide */ for webfilter worthy
    @Bean
    public CorsWebFilter corsWebFilter(a){

        /** * returns a CorsWebFilter, which needs to pass in a parameter, which is the interface, can be directly new interface is an excuse to use its implementation class to process */
        UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();

        / / build CorsConfiguration
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.addAllowedMethod("*");
        corsConfiguration.addAllowedHeader("*");
        // Allow cookies to cross domains
        corsConfiguration.setAllowCredentials(true);

        urlBasedCorsConfigurationSource.registerCorsConfiguration("/ * *",corsConfiguration);
        return newCorsWebFilter(urlBasedCorsConfigurationSource); }}Copy the code

CorsWebFilter: a spring-provided filter that passes through the filter before the request arrives and when it is returned, and is put into the Spring container to take effect.

2. Add Filter to the gateway service

There are two cross-domain accesses

  1. When the request reaches the Gateway, the gateway tells the browser that it can cross domains:
  2. When the browser makes a real request, it can cross domains because cross-domains is a policy of the browser.



    This is a coded way to fool the browser solution, using nginx proxy in production.