From 9dc682ff03c684c30cfcee384c255c11f53dd2b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=96=AF=E7=8B=82=E7=9A=84=E7=8B=AE=E5=AD=90Li?= <15040126243@163.com> Date: Fri, 1 Aug 2025 12:47:49 +0800 Subject: [PATCH] =?UTF-8?q?fix=20=E4=BF=AE=E5=A4=8D=20=E6=9C=80=E6=96=B0?= =?UTF-8?q?=E7=89=88=E6=9C=ACgateway=E4=B8=8D=E4=BC=A0=E9=80=92X-Forwarded?= =?UTF-8?q?-Prefix=E8=AF=B7=E6=B1=82=E5=A4=B4=E5=AF=BC=E8=87=B4doc?= =?UTF-8?q?=E6=96=87=E6=A1=A3=E6=97=A0=E6=B3=95=E8=8E=B7=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../StripPrefixGatewayFilterFactory.java | 115 ++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 ruoyi-gateway/src/main/java/org/springframework/cloud/gateway/filter/factory/StripPrefixGatewayFilterFactory.java diff --git a/ruoyi-gateway/src/main/java/org/springframework/cloud/gateway/filter/factory/StripPrefixGatewayFilterFactory.java b/ruoyi-gateway/src/main/java/org/springframework/cloud/gateway/filter/factory/StripPrefixGatewayFilterFactory.java new file mode 100644 index 00000000..45bf5e01 --- /dev/null +++ b/ruoyi-gateway/src/main/java/org/springframework/cloud/gateway/filter/factory/StripPrefixGatewayFilterFactory.java @@ -0,0 +1,115 @@ +/* + * Copyright 2013-2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.gateway.filter.factory; + +import org.springframework.cloud.gateway.filter.GatewayFilter; +import org.springframework.cloud.gateway.filter.GatewayFilterChain; +import org.springframework.http.server.reactive.ServerHttpRequest; +import org.springframework.util.StringUtils; +import org.springframework.web.server.ServerWebExchange; +import reactor.core.publisher.Mono; + +import java.util.Arrays; +import java.util.List; + +import static org.springframework.cloud.gateway.support.GatewayToStringStyler.filterToStringCreator; +import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR; +import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.addOriginalRequestUrl; + +/** + * This filter removes the first part of the path, known as the prefix, from the request + * before sending it downstream. + * + * @author Ryan Baxter + */ +public class StripPrefixGatewayFilterFactory + extends AbstractGatewayFilterFactory { + + /** + * Parts key. + */ + public static final String PARTS_KEY = "parts"; + + public StripPrefixGatewayFilterFactory() { + super(Config.class); + } + + @Override + public List shortcutFieldOrder() { + return Arrays.asList(PARTS_KEY); + } + + @Override + public GatewayFilter apply(Config config) { + return new GatewayFilter() { + @Override + public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) { + ServerHttpRequest request = exchange.getRequest(); + addOriginalRequestUrl(exchange, request.getURI()); + String path = request.getURI().getRawPath(); + String[] originalParts = StringUtils.tokenizeToStringArray(path, "/"); + + // all new paths start with / + StringBuilder newPath = new StringBuilder("/"); + for (int i = 0; i < originalParts.length; i++) { + if (i >= config.getParts()) { + // only append slash if this is the second part or greater + if (newPath.length() > 1) { + newPath.append('/'); + } + newPath.append(originalParts[i]); + } + } + if (newPath.length() > 1 && path.endsWith("/")) { + newPath.append('/'); + } + // 增加doc前缀传递 + String prefix = "/" + originalParts[config.getParts() - 1]; + + ServerHttpRequest newRequest = request.mutate() + .header("X-Forwarded-Prefix", prefix) + .path(newPath.toString()) + .build(); + + exchange.getAttributes().put(GATEWAY_REQUEST_URL_ATTR, newRequest.getURI()); + + return chain.filter(exchange.mutate().request(newRequest).build()); + } + + @Override + public String toString() { + return filterToStringCreator(StripPrefixGatewayFilterFactory.this).append("parts", config.getParts()) + .toString(); + } + }; + } + + public static class Config { + + private int parts = 1; + + public int getParts() { + return parts; + } + + public void setParts(int parts) { + this.parts = parts; + } + + } + +}