欢迎访问Spring Cloud中国社区

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

Spring Cloud gateway替代zuul以及解决gateway跨域访问问题

admin · 26天前 · 624 ·

Spring Cloud Gateway建立在Spring Framework 5,Project Reactor和Spring Boot 2之上,使用非阻塞API。 Websockets得到支持,并且由于它与Spring紧密集成,所以将会是一个更好的 开发 体验。

1.添加maven依赖:

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-webflux</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.springframework.cloud</groupId>
  7. <artifactId>spring-cloud-starter-gateway</artifactId>
  8. </dependency>
  9. <dependency>
  10. <groupId>org.springframework.cloud</groupId>
  11. <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
  12. </dependency>

gateway是用过webflux实现,在项目中请不要引用webmvc,否则初始化会报错

2.配置过滤器:

  1. public class loginfiler implements GlobalFilter {
  2. @Override
  3. public Mono<Void> filter(ServerWebExchange serverWebExchange, GatewayFilterChain gatewayFilterChain) {
  4. 。。。
  5. return gatewayFilterChain.filter(serverWebExchange.mutate().request(build).build());
  6. }
  7. }

在这里可以实现记录日志和访问权限校验等

3.如果是前后端分离的项目,需要增加配置以解决跨域问题:

  1. package com.gameley.config;
  2. import org.springframework.cloud.client.discovery.DiscoveryClient;
  3. import org.springframework.cloud.gateway.discovery.DiscoveryClientRouteDefinitionLocator;
  4. import org.springframework.cloud.gateway.route.RouteDefinitionLocator;
  5. import org.springframework.context.annotation.Bean;
  6. import org.springframework.context.annotation.Configuration;
  7. import org.springframework.http.HttpHeaders;
  8. import org.springframework.http.HttpMethod;
  9. import org.springframework.http.HttpStatus;
  10. import org.springframework.http.server.reactive.ServerHttpRequest;
  11. import org.springframework.http.server.reactive.ServerHttpResponse;
  12. import org.springframework.web.cors.reactive.CorsUtils;
  13. import org.springframework.web.server.ServerWebExchange;
  14. import org.springframework.web.server.WebFilter;
  15. import org.springframework.web.server.WebFilterChain;
  16. import reactor.core.publisher.Mono;
  17. /**
  18. * @author wwmxd
  19. * @create 2018/3/22.
  20. */
  21. @Configuration
  22. public class RouteConfiguration {
  23. //这里为支持的请求头,如果有自定义的header字段请自己添加(不知道为什么不能使用*)
  24. private static final String ALLOWED_HEADERS = "x-requested-with, authorization, Content-Type, Authorization, credential, X-XSRF-TOKEN,token,username,client";
  25. private static final String ALLOWED_METHODS = "*";
  26. private static final String ALLOWED_ORIGIN = "*";
  27. private static final String ALLOWED_Expose = "*";
  28. private static final String MAX_AGE = "18000L";
  29. @Bean
  30. public WebFilter corsFilter() {
  31. return (ServerWebExchange ctx, WebFilterChain chain) -> {
  32. ServerHttpRequest request = ctx.getRequest();
  33. if (CorsUtils.isCorsRequest(request)) {
  34. ServerHttpResponse response = ctx.getResponse();
  35. HttpHeaders headers = response.getHeaders();
  36. headers.add("Access-Control-Allow-Origin", ALLOWED_ORIGIN);
  37. headers.add("Access-Control-Allow-Methods", ALLOWED_METHODS);
  38. headers.add("Access-Control-Max-Age", MAX_AGE);
  39. headers.add("Access-Control-Allow-Headers", ALLOWED_HEADERS);
  40. headers.add("Access-Control-Expose-Headers", ALLOWED_Expose);
  41. headers.add("Access-Control-Allow-Credentials", "true");
  42. if (request.getMethod() == HttpMethod.OPTIONS) {
  43. response.setStatusCode(HttpStatus.OK);
  44. return Mono.empty();
  45. }
  46. }
  47. return chain.filter(ctx);
  48. };
  49. }
  50. /**
  51. *
  52. *如果使用了注册中心(如:Eureka),进行控制则需要增加如下配置
  53. */
  54. @Bean
  55. public RouteDefinitionLocator discoveryClientRouteDefinitionLocator(DiscoveryClient discoveryClient) {
  56. return new DiscoveryClientRouteDefinitionLocator(discoveryClient);
  57. }
  58. }

原文:https://blog.csdn.net/qq_36752632/article/details/79656716