2.5 Gateway跨域配置(CORS Configuration)
    image.png
    通过yml配置的方式
    https://docs.spring.io/spring-cloud-gateway/docs/current/reference/html/#cors-configuration

    1. spring:
    2. cloud:
    3. gateway:
    4. globalcors:
    5. cors-configurations:
    6. '[/**]':
    7. allowedOrigins: "*"
    8. allowedMethods:
    9. - GET
    10. - POST
    11. - DELETE
    12. - PUT
    13. - OPTION

    通过java配置的方式

    1. @Configuration
    2. public class CorsConfig {
    3. @Bean
    4. public CorsWebFilter corsFilter() {
    5. CorsConfiguration config = new CorsConfiguration();
    6. config.addAllowedMethod("*");
    7. config.addAllowedOrigin("*");
    8. config.addAllowedHeader("*");
    9. UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser());
    10. source.registerCorsConfiguration("/**", config);
    11. return new CorsWebFilter(source);
    12. }
    13. }