Spring Cloud Gateway 跨域以及报错解决
【请求网关转发时,报错503】注意application.yml中,配置routes时:spring:application:name: getway-servicecloud:gateway:routes:- id: pms-route #注意id前面的 - 要与routes对齐,不然调用服务时会出现503错误...
【请求网关转发时,报错503】
注意application.yml中,配置routes时:
spring: application: name: getway-service cloud: gateway: routes: - id: pms-route #注意id前面的 - 要与routes对齐,不然调用服务时会出现503错误 uri: lb://pms-service predicates: - Path=/pms/**
一定要保证对齐方式正确,不然通过gateway调用服务时,会出现503错误。
【跨域问题解决】:
一、配置好gateway之后,创建跨域配置类CorsConfig来实现跨域情况(推荐):
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.cors.CorsConfiguration; import org.springframework.web.cors.reactive.CorsWebFilter; import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.cors.CorsConfiguration; import org.springframework.web.cors.reactive.CorsWebFilter; import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource; import org.springframework.web.util.pattern.PathPatternParser; @Configuration public class CorsConfig { /** * 该访问配置跨域访问执行 * @return */ @Bean public CorsWebFilter corsWebFilter(){ UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource(); //cors跨域配置对象 CorsConfiguration corsConfiguration = new CorsConfiguration(); corsConfiguration.setAllowCredentials(true);//是否允许携带cookie corsConfiguration.addAllowedOrigin("*"); //允许跨域访问的域名,可填写具体域名,*代表允许所有访问 corsConfiguration.addAllowedMethod("*");//允许访问类型:get post 等,*代表所有类型 corsConfiguration.addAllowedHeader("*"); //配置源对象 urlBasedCorsConfigurationSource.registerCorsConfiguration("/**",corsConfiguration); //cors 过滤器对象 注意!CorsWebFilter不要导错包 return new CorsWebFilter(urlBasedCorsConfigurationSource); } }
二、通过配置文件实现:
spring: cloud: gateway: discovery: # 跨域 globalcors: corsConfigurations: '[/**]': allowedHeaders: "*" allowedOrigins: "*" allowedMethods: - GET POST DELETE PUT OPTION
【报错一】
has been blocked by CORS policy: Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.
解决:
在配置类CorsConfig中:
corsConfiguration.addAllowedOrigin("*") 这一项需要填写具体请求者的地址,不能用*来代替。
【报错二】
has been blocked by CORS policy: Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Credentials' header in the response is '' which must be 'true' when the request's credentials mode is 'include'. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.
解决:
在配置类CorsConfig中:
corsConfiguration.setAllowCredentials(true);//是否允许携带cookie
corsConfiguration.addAllowedHeader("*");这两项设置成这样既可。
更多推荐
所有评论(0)