springcloud项目网关升级:gateway替代zuul以及解决gateway跨域访问问题
2018-03-22 17:05
3639 查看
注:Spring Cloud Gateway建立在Spring Framework 5,Project Reactor和Spring Boot 2之上,使用非阻塞API。 Websockets得到支持,并且由于它与Spring紧密集成,所以将会是一个更好的 开发 体验。
本文只讲述搭建过程,具体源码解析可以参考:http://www.iocoder.cn/categories/Spring-Cloud-Gateway/
1.添加maven依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency>
注:gateway是用过webflux实现,在项目中请不要引用webmvc,否则初始化会报错
2.配置过滤器:
public class loginfiler implements GlobalFilter { @Override public Mono<Void> filter(ServerWebExchange serverWebExchange, GatewayFilterChain gatewayFilterChain) { 。。。 return gatewayFilterChain.filter(serverWebExchange.mutate().request(build).build()); } }
注:在这里可以实现记录日志和访问权限校验等
3.如果是前后端分离的项目,需要增加配置以解决跨域问题:
package com.gameley.config; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.cloud.gateway.discovery.DiscoveryClientRouteDefinitionLocator; import org.springframework.cloud.gateway.route.RouteDefinitionLocator; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.HttpStatus; import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.http.server.reactive.ServerHttpResponse; import org.springframework.web.cors.reactive.CorsUtils; import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.WebFilter; import org.springframework.web.server.WebFilterChain; import reactor.core.publisher.Mono; /** * @author wwmxd * @create 2018/3/22. */ @Configuration public class RouteConfiguration { //这里为支持的请求头,如果有自定义的header字段请自己添加(不知道为什么不能使用*) private static final String ALLOWED_HEADERS = "x-requested-with, authorization, Content-Type, Authorization, credential, X-XSRF-TOKEN,token,username,client"; private static final String ALLOWED_METHODS = "*"; private static final String ALLOWED_ORIGIN = "*"; p b6ca rivate static final String ALLOWED_Expose = "*"; private static final String MAX_AGE = "18000L"; @Bean public WebFilter corsFilter() { return (ServerWebExchange ctx, WebFilterChain chain) -> { ServerHttpRequest request = ctx.getRequest(); if (CorsUtils.isCorsRequest(request)) { ServerHttpResponse response = ctx.getResponse(); HttpHeaders headers = response.getHeaders(); headers.add("Access-Control-Allow-Origin", ALLOWED_ORIGIN); headers.add("Access-Control-Allow-Methods", ALLOWED_METHODS); headers.add("Access-Control-Max-Age", MAX_AGE); headers.add("Access-Control-Allow-Headers", ALLOWED_HEADERS); headers.add("Access-Control-Expose-Headers", ALLOWED_Expose); headers.add("Access-Control-Allow-Credentials", "true"); if (request.getMethod() == HttpMethod.OPTIONS) { response.setStatusCode(HttpStatus.OK); return Mono.empty(); } } return chain.filter(ctx); }; } /** * *如果使用了注册中心(如:Eureka),进行控制则需要增加如下配置 */ @Bean public RouteDefinitionLocator discoveryClientRouteDefinitionLocator(DiscoveryClient discoveryClient) { return new DiscoveryClientRouteDefinitionLocator(discoveryClient); } }
相关文章推荐
- 解决ajax和spring的跨域访问问题
- [置顶] spring boot项目实战:跨域问题解决
- Android访问Tomcat错误以及mp3player项目乱码问题解决
- [置顶] spring boot项目实战:跨域问题解决
- [置顶] spring boot项目实战:跨域问题解决
- [置顶] spring boot项目实战:跨域问题解决
- springcloud- maven 模块化搭建soa项目,以及打包的解决办法
- Spring Cloud eureka 注册服务发现,访问管理页面是xml问题解决
- [置顶] spring boot项目实战:跨域问题解决
- Android访问Tomcat错误以及mp3player项目乱码问题解决
- 解决SpringBoot项目打成jar包访问不了jsp模板的问题: SpringBoot项目集成jsp模板打成war包 然后通过java -jar 命令启动访问
- 四、Spring Cloud系列:问题-随机端口启动项目无法访问
- Spring Cloud 项目出现 Options Forbidden 403 问题解决
- [置顶] spring boot项目实战:跨域问题解决
- spring项目中解决跨域问题
- [置顶] spring boot项目实战:跨域问题解决
- SpringCloud-网关gateway-zuul
- [置顶] spring boot项目实战:跨域问题解决
- [置顶] spring boot项目实战:跨域问题解决
- [置顶] spring boot项目实战:跨域问题解决