1. 程式人生 > >springcloud專案閘道器升級:gateway替代zuul以及解決gateway跨域訪問問題

springcloud專案閘道器升級:gateway替代zuul以及解決gateway跨域訪問問題

Fork me on Gitee
注: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 = "*";
    private 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);
    }
}