This article mainly introduces SpringCloud Gateway cross-domain configuration code examples. The example code is introduced in this article in detail, which has certain reference learning value for everyone's learning or work. Friends who need it can refer to it.
Springboot version: 2.1.
SpringCloud version: Greenwich.SR2
yml configuration:
spring: cloud: gateway: globalcors: cors-configurations: '[/**]': # Allow to carry authentication information # Allow cross-domain sources (website domain name/ip), set * to all # Allow the head field in cross-domain requests, set * to all # Allow cross-domain methods, default to GET and OPTIONS, set * to all # Cross-domain validity period allow-credentials: true allowed-origins: - "http://localhost:13009" - "http://localhost:13010" allowed-headers: "*" allowed-methods: - OPTIONS - GET - POST max-age: 3600 # Allow response head information # Only 6 of the following are allowed by default: # Cache-Control # Content-Language # Content-Type # Expires # Last-Modified # Pragma #exposed-headers:
Configuration class:
Many people on the Internet say that this configuration is invalid, but I tested it and it was OK. If it is really invalid, you can manually assemble the Cros configuration:
package ; import ; import ; import ; import ; import ; import ; import ; import ; /** * @author roger yang * @date 11/21/2019 */ @Configuration @ConditionalOnBean() public class CorsAutoConfiguration { @Autowired private GlobalCorsProperties globalCorsProperties; @Bean public CorsWebFilter corsFilter() { UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser()); ().forEach((path,corsConfiguration)->(path, corsConfiguration)); return new CorsWebFilter(source); } }
Of course, we recommend cross-domain processing in middleware such as Nginx, and business services should pay attention to business.
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.