欢迎访问Spring Cloud中国社区

我们致力于成为中国最专业的Spring Boot和Spring Cloud交流社区。推荐使用Github直接登录,欢迎加微信号Software_King进入社区微信交流群。若发现网站bug欢迎反馈!

Spring Cloud Gateway中处理跨域请求

xujin · 17天前 · 382 ·

Spring Cloud Gateway中处理跨域请求

Gateway的cors配置不一样,需要增加webFilter

  1. import org.springframework.boot.SpringApplication;
  2. import org.springframework.boot.autoconfigure.SpringBootApplication;
  3. import org.springframework.cloud.client.discovery.DiscoveryClient;
  4. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  5. import org.springframework.cloud.gateway.discovery.DiscoveryClientRouteDefinitionLocator;
  6. import org.springframework.cloud.gateway.discovery.DiscoveryLocatorProperties;
  7. import org.springframework.cloud.gateway.route.RouteDefinitionLocator;
  8. import org.springframework.context.annotation.Bean;
  9. import org.springframework.http.HttpHeaders;
  10. import org.springframework.http.HttpMethod;
  11. import org.springframework.http.HttpStatus;
  12. import org.springframework.http.codec.ServerCodecConfigurer;
  13. import org.springframework.http.codec.support.DefaultServerCodecConfigurer;
  14. import org.springframework.http.server.reactive.ServerHttpRequest;
  15. import org.springframework.http.server.reactive.ServerHttpResponse;
  16. import org.springframework.web.cors.reactive.CorsUtils;
  17. import org.springframework.web.server.ServerWebExchange;
  18. import org.springframework.web.server.WebFilter;
  19. import org.springframework.web.server.WebFilterChain;
  20. import reactor.core.publisher.Mono;
  21. @SpringBootApplication
  22. @EnableDiscoveryClient
  23. public class GatewayStarter {
  24. private static final String ALL = "*";
  25. private static final String MAX_AGE = "18000L";
  26. public static void main(String[] args) {
  27. SpringApplication.run(GatewayStarter.class, args);
  28. }
  29. @Bean
  30. public RouteDefinitionLocator discoveryClientRouteDefinitionLocator(DiscoveryClient discoveryClient,
  31. DiscoveryLocatorProperties properties) {
  32. return new DiscoveryClientRouteDefinitionLocator(discoveryClient, properties);
  33. }
  34. @Bean
  35. public ServerCodecConfigurer serverCodecConfigurer() {
  36. return new DefaultServerCodecConfigurer();
  37. }
  38. @Bean
  39. public WebFilter corsFilter() {
  40. return (ServerWebExchange ctx, WebFilterChain chain) -> {
  41. ServerHttpRequest request = ctx.getRequest();
  42. if (!CorsUtils.isCorsRequest(request)) {
  43. return chain.filter(ctx);
  44. }
  45. HttpHeaders requestHeaders = request.getHeaders();
  46. ServerHttpResponse response = ctx.getResponse();
  47. HttpMethod requestMethod = requestHeaders.getAccessControlRequestMethod();
  48. HttpHeaders headers = response.getHeaders();
  49. headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, requestHeaders.getOrigin());
  50. headers.addAll(HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS, requestHeaders.getAccessControlRequestHeaders());
  51. if (requestMethod != null) {
  52. headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS, requestMethod.name());
  53. }
  54. headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS, "true");
  55. headers.add(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, ALL);
  56. headers.add(HttpHeaders.ACCESS_CONTROL_MAX_AGE, MAX_AGE);
  57. if (request.getMethod() == HttpMethod.OPTIONS) {
  58. response.setStatusCode(HttpStatus.OK);
  59. return Mono.empty();
  60. }
  61. return chain.filter(ctx);
  62. };
  63. }
  64. }