|
@@ -0,0 +1,72 @@
|
|
|
|
+package com.galaxis.wms.filter;
|
|
|
|
+
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
|
+import org.springframework.cloud.gateway.filter.GatewayFilterChain;
|
|
|
|
+import org.springframework.cloud.gateway.filter.GlobalFilter;
|
|
|
|
+import org.springframework.cloud.gateway.route.Route;
|
|
|
|
+import org.springframework.cloud.gateway.support.NotFoundException;
|
|
|
|
+import org.springframework.cloud.gateway.support.ServerWebExchangeUtils;
|
|
|
|
+import org.springframework.core.Ordered;
|
|
|
|
+import org.springframework.http.server.reactive.ServerHttpRequest;
|
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
|
+import org.springframework.web.server.ServerWebExchange;
|
|
|
|
+import org.springframework.web.util.UriComponentsBuilder;
|
|
|
|
+import reactor.core.publisher.Mono;
|
|
|
|
+
|
|
|
|
+import java.net.URI;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * 全局认证请求
|
|
|
|
+ * <p>
|
|
|
|
+ * 作者:lizw <br/>
|
|
|
|
+ * 创建时间:2021/01/26 11:36 <br/>
|
|
|
|
+ */
|
|
|
|
+@Component
|
|
|
|
+@Slf4j
|
|
|
|
+public class AuthGlobalFilter implements GlobalFilter, Ordered {
|
|
|
|
+ private static final int ORDER = Ordered.LOWEST_PRECEDENCE - 1;
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
|
|
|
|
+ ServerHttpRequest request = exchange.getRequest();
|
|
|
|
+ final String path = request.getPath().value();
|
|
|
|
+ final String method = StringUtils.upperCase(request.getMethodValue());
|
|
|
|
+ log.debug(" -> [{}] | {}", method, path);
|
|
|
|
+ // 当前Route
|
|
|
|
+ final Route route = exchange.getAttribute(ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR);
|
|
|
|
+ final String routeId = route != null ? route.getId() : null;
|
|
|
|
+ if (route == null || StringUtils.isBlank(routeId)) {
|
|
|
|
+ throw NotFoundException.create(true, "404 Not Found");
|
|
|
|
+ }
|
|
|
|
+ // 获取原来请求路径
|
|
|
|
+// String requestPath = exchange.getAttribute(FilterDict.SYSTEM_REQUEST_PATH);
|
|
|
|
+
|
|
|
|
+// Optional<Route> route = Optional.of(exchange.getAttribute(GATEWAY_ROUTE_ATTR));
|
|
|
|
+ Route newRoute = Route.async()
|
|
|
|
+ .asyncPredicate(route.getPredicate())
|
|
|
|
+ .filters(route.getFilters())
|
|
|
|
+ .id(route.getId())
|
|
|
|
+ .order(route.getOrder())
|
|
|
|
+ .uri("http://127.0.0.1:6666").build();
|
|
|
|
+// ServerWebExchange mutateExchange = exchange.mutate().request(builder -> builder.uri(uri).build()).build();
|
|
|
|
+ exchange.getAttributes().put(ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR, newRoute);
|
|
|
|
+// exchange.getAttributes().put(FilterDict.SYSTEM_APP_IP_ADDR, serviceAddress[0]);
|
|
|
|
+
|
|
|
|
+ URI uri = UriComponentsBuilder.
|
|
|
|
+ newInstance().scheme(exchange.getRequest().getURI().getScheme()).
|
|
|
|
+ host("127.0.0.1").port("6666")
|
|
|
|
+ .path("/wms/api").query(exchange.getRequest().getURI().getRawQuery()).build(true)
|
|
|
|
+ .toUri();
|
|
|
|
+
|
|
|
|
+ exchange.getAttributes().put(ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR, uri);
|
|
|
|
+
|
|
|
|
+ log.debug(" -> routeId={} | [{}] | {}", routeId, method, path);
|
|
|
|
+ return chain.filter(exchange);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public int getOrder() {
|
|
|
|
+ return ORDER;
|
|
|
|
+ }
|
|
|
|
+}
|