编程知识 cdmana.com

Spring cloud gateway unified configuration cross domain

One 、 problem

Cross domain : It means that the browser can't execute scripts of other websites . It's caused by the browser's homology strategy , It 's the browser js Safety restrictions imposed .

The same-origin policy : Means the agreement 、 domain name 、 All ports should be the same , One of the differences can lead to cross domain problems ;

Two 、 Method

There are generally two ways to solve it :

  • adopt nginx To reverse proxy the front end and back end to the same source , This applies to online .
  • In the development phase, you can go directly through Gateway Configuration to release restrictions uniformly , Such as :
@Configuration
public class LemallCorsConfiguration {
    @Bean
    public CorsWebFilter corsWebFilter(){
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();

        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedMethod("*");
        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.setAllowCredentials(true);

        source.registerCorsConfiguration("/**",corsConfiguration);
        return new CorsWebFilter(source);
    }
}

版权声明
本文为[Don't stay up late]所创,转载请带上原文链接,感谢
https://cdmana.com/2020/12/20201224114549532G.html

Scroll to Top