Merge remote-tracking branch 'origin/master'
This commit is contained in:
commit
0073e62c59
@ -0,0 +1,84 @@
|
|||||||
|
package com.wmyun.gateway.filter.dev;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.Getter;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.cloud.gateway.filter.GatewayFilter;
|
||||||
|
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
|
||||||
|
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Description: TODO
|
||||||
|
* @Date: 2025/3/4 14:17
|
||||||
|
* @Created: by ZZSLL
|
||||||
|
*/
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class RealIpGatewayFilterFactory extends AbstractGatewayFilterFactory<RealIpGatewayFilterFactory.Config> {
|
||||||
|
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(RealIpGatewayFilterFactory.class);
|
||||||
|
|
||||||
|
public RealIpGatewayFilterFactory() {
|
||||||
|
super(Config.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public GatewayFilter apply(Config config) {
|
||||||
|
return (exchange, chain) -> {
|
||||||
|
// 获取 X-Real-IP 的值
|
||||||
|
ServerHttpRequest request = exchange.getRequest();
|
||||||
|
String realIp = request.getHeaders().getFirst("X-Real-IP");
|
||||||
|
|
||||||
|
logger.info("realIp: {}", realIp);
|
||||||
|
|
||||||
|
if (realIp != null) {
|
||||||
|
// 记录原始请求地址
|
||||||
|
URI originalUri = request.getURI();
|
||||||
|
logger.info("Original request URI: {}", originalUri);
|
||||||
|
String rawQuery = originalUri.getRawQuery();
|
||||||
|
|
||||||
|
// 构建新的 URI
|
||||||
|
URI modifiedUri = URI.create(
|
||||||
|
originalUri.getScheme() + "://" + realIp + ":" + config.getPort() + originalUri.getRawPath() + (rawQuery != null ? "?" + rawQuery : "")
|
||||||
|
);
|
||||||
|
|
||||||
|
// 记录修改后的请求地址
|
||||||
|
logger.info("Modified request URI: {}", modifiedUri);
|
||||||
|
|
||||||
|
// 修改请求
|
||||||
|
ServerHttpRequest modifiedRequest = request.mutate()
|
||||||
|
.uri(modifiedUri)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
return chain.filter(exchange.mutate().request(modifiedRequest).build());
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果没有 X-Real-IP,继续使用原始请求
|
||||||
|
logger.info("No X-Real-IP header found, using original request URI: {}", request.getURI());
|
||||||
|
return chain.filter(exchange);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public List<String> shortcutFieldOrder() {
|
||||||
|
return List.of("port");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static class Config {
|
||||||
|
private int port;
|
||||||
|
|
||||||
|
public int getPort() {
|
||||||
|
return port;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPort(int port) {
|
||||||
|
this.port = port;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -25,12 +25,14 @@ spring:
|
|||||||
- Path=/admin-api/system/**
|
- Path=/admin-api/system/**
|
||||||
filters:
|
filters:
|
||||||
- RewritePath=/admin-api/system/v3/api-docs, /v3/api-docs # 配置,保证转发到 /v3/api-docs
|
- RewritePath=/admin-api/system/v3/api-docs, /v3/api-docs # 配置,保证转发到 /v3/api-docs
|
||||||
|
- RealIp=48081
|
||||||
- id: system-app-api # 路由的编号
|
- id: system-app-api # 路由的编号
|
||||||
uri: grayLb://system-server
|
uri: grayLb://system-server
|
||||||
predicates: # 断言,作为路由的匹配条件,对应 RouteDefinition 数组
|
predicates: # 断言,作为路由的匹配条件,对应 RouteDefinition 数组
|
||||||
- Path=/app-api/system/**
|
- Path=/app-api/system/**
|
||||||
filters:
|
filters:
|
||||||
- RewritePath=/app-api/system/v3/api-docs, /v3/api-docs
|
- RewritePath=/app-api/system/v3/api-docs, /v3/api-docs
|
||||||
|
- RealIp=48081
|
||||||
## infra-server 服务
|
## infra-server 服务
|
||||||
- id: infra-admin-api # 路由的编号
|
- id: infra-admin-api # 路由的编号
|
||||||
uri: grayLb://infra-server
|
uri: grayLb://infra-server
|
||||||
@ -38,12 +40,14 @@ spring:
|
|||||||
- Path=/admin-api/infra/**
|
- Path=/admin-api/infra/**
|
||||||
filters:
|
filters:
|
||||||
- RewritePath=/admin-api/infra/v3/api-docs, /v3/api-docs
|
- RewritePath=/admin-api/infra/v3/api-docs, /v3/api-docs
|
||||||
|
- RealIp=48082
|
||||||
- id: infra-app-api # 路由的编号
|
- id: infra-app-api # 路由的编号
|
||||||
uri: grayLb://infra-server
|
uri: grayLb://infra-server
|
||||||
predicates: # 断言,作为路由的匹配条件,对应 RouteDefinition 数组
|
predicates: # 断言,作为路由的匹配条件,对应 RouteDefinition 数组
|
||||||
- Path=/app-api/infra/**
|
- Path=/app-api/infra/**
|
||||||
filters:
|
filters:
|
||||||
- RewritePath=/app-api/infra/v3/api-docs, /v3/api-docs
|
- RewritePath=/app-api/infra/v3/api-docs, /v3/api-docs
|
||||||
|
- RealIp=48082
|
||||||
- id: infra-spring-boot-admin # 路由的编号(Spring Boot Admin)
|
- id: infra-spring-boot-admin # 路由的编号(Spring Boot Admin)
|
||||||
uri: grayLb://infra-server
|
uri: grayLb://infra-server
|
||||||
predicates: # 断言,作为路由的匹配条件,对应 RouteDefinition 数组
|
predicates: # 断言,作为路由的匹配条件,对应 RouteDefinition 数组
|
||||||
|
Loading…
Reference in New Issue
Block a user