Commit c241d04b by 叶立庆

feat(gateway): 添加请求信息打印功能并优化签名验证

在WebRequestSignFilter中添加打印请求信息的功能,包括IP、路径、方法和参数
优化JSON请求体解析,使用properties()替代fields()
1 parent a2392223
# Compiled class file
*.class
# Log file
*.log
# BlueJ files
*.ctxt
# Mobile Tools for Java (J2ME)
.mtj.tmp/
# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
replay_pid*
# Maven
target/
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
pom.xml.next
release.properties
dependency-reduced-pom.xml
buildNumber.properties
.mvn/timing.properties
# https://github.com/takari/maven-wrapper#usage-without-binary-jar
.mvn/wrapper/maven-wrapper.jar
# Eclipse
.metadata
bin/
tmp/
*.tmp
*.bak
*.swp
*~.nib
local.properties
.settings/
.loadpath
.recommenders
# External tool builders
.externalToolBuilders/
# Locally stored "Eclipse launch configurations"
*.launch
# PyDev specific (Python IDE for Eclipse)
*.pydevproject
# CDT-specific (C/C++ Development Tooling)
.cproject
# CDT- autotools
.autotools
# Java annotation processor (APT)
.factorypath
# PDT-specific (PHP Development Tools)
.buildpath
# sbteclipse plugin
.target
# Tern plugin
.tern-project
# TeXlipse plugin
.texlipse
# STS (Spring Tool Suite)
.springBeans
# Code Recommenders
.recommenders/
# Annotation Processing
.apt_generated/
.apt_generated_test/
# Scala IDE specific (Scala & Java IDE for Eclipse)
.cache-main
.scala_dependencies
.worksheet
# IntelliJ IDEA
.idea/
*.iws
*.iml
*.ipr
# NetBeans
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/
# VS Code
.vscode/
# Mac
.DS_Store
# Windows
Thumbs.db
ehthumbs.db
Desktop.ini
# Linux
*~
# Temporary files
*.tmp
*.temp
# Application specific
application-local.yml
application-local.properties
# Logs
logs/
*.log
# Spring Boot
spring.log
# Docker
.dockerignore
# Node modules (if using frontend tools)
node_modules/
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# Environment variables
.env
.env.local
.env.development.local
.env.test.local
.env.production.local
\ No newline at end of file
......@@ -13,23 +13,24 @@
<component name="MarkdownSettingsMigration">
<option name="stateVersion" value="1" />
</component>
<component name="ProjectColorInfo"><![CDATA[{
"associatedIndex": 6
}]]></component>
<component name="ProjectColorInfo">{
&quot;associatedIndex&quot;: 6
}</component>
<component name="ProjectId" id="32pBYWXUMUj7WWS63YLYY598llH" />
<component name="ProjectLevelVcsManager" settingsEditedManually="true" />
<component name="ProjectViewState">
<option name="showLibraryContents" value="true" />
</component>
<component name="PropertiesComponent"><![CDATA[{
"keyToString": {
"Application.GscMain.executor": "Debug",
"RunOnceActivity.OpenProjectViewOnStart": "true",
"RunOnceActivity.ShowReadmeOnStart": "true",
"last_opened_file_path": "D:/files/java/development/mySpace/demo/gsc-master-v1",
"kotlin-language-version-configured": "true",
"last_opened_file_path": "D:/files/java/development/workspace_scrm/git/gsc-gateway",
"project.structure.last.edited": "Project",
"project.structure.proportion": "0.0",
"project.structure.side.proportion": "0.0",
"settings.editor.selected.configurable": "project.propVCSSupport.DirectoryMappings"
"settings.editor.selected.configurable": "project.propVCSSupport.Mappings"
}
}]]></component>
<component name="RunManager">
......@@ -57,24 +58,6 @@
<configuration>C:\Users\Sandul\AppData\Roaming\Subversion</configuration>
<supportedVersion>125</supportedVersion>
</component>
<component name="SvnFileUrlMappingImpl">
<option name="myMappingRoots">
<list>
<SvnCopyRootSimple>
<option name="myVcsRoot" value="D:\files\java\development\workspace_scrm\gsc\trunk\src\gsc-gateway" />
<option name="myCopyRoot" value="D:\files\java\development\workspace_scrm\gsc\trunk\src\gsc-gateway" />
</SvnCopyRootSimple>
</list>
</option>
<option name="myMoreRealMappingRoots">
<list>
<SvnCopyRootSimple>
<option name="myVcsRoot" value="D:\files\java\development\workspace_scrm\gsc\trunk\src\gsc-gateway" />
<option name="myCopyRoot" value="D:\files\java\development\workspace_scrm\gsc\trunk\src\gsc-gateway" />
</SvnCopyRootSimple>
</list>
</option>
</component>
<component name="TaskManager">
<task active="true" id="Default" summary="Default task">
<changelist id="6dd290ef-5416-4e2c-8ef6-02093e7e62f0" name="Changes" comment="" />
......
......@@ -49,6 +49,9 @@ public class WebRequestSignFilter implements GlobalFilter, Ordered {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
// 打印请求信息
printRequestInfo(exchange);
// 1. 获取请求信息
String path = exchange.getRequest().getPath().toString();
// 2. 检查是否在白名单中
......@@ -132,6 +135,7 @@ public class WebRequestSignFilter implements GlobalFilter, Ordered {
dataBuffer.read(bytes);
DataBufferUtils.release(dataBuffer);
Map<String, Object> paramMap = parseRequestBody(bytes, contentType);
log.info("请求体参数:{}", paramMap);
params.putAll(paramMap);
if (!SignUtil.verifySign(params, getSecret(appId))) {
log.info("签名验证失败");
......@@ -210,7 +214,7 @@ public class WebRequestSignFilter implements GlobalFilter, Ordered {
private Map<String, Object> parseJsonBody(byte[] bytes) throws IOException {
Map<String, Object> result = new HashMap<>();
JsonNode root = objectMapper.readTree(bytes);
root.fields().forEachRemaining(entry -> {
root.properties().forEach(entry -> {
// 排除空值字段
if (entry.getValue().isNull()) {
return;
......@@ -280,6 +284,58 @@ public class WebRequestSignFilter implements GlobalFilter, Ordered {
return signConfigProperties.getKayPairs().get(appId);
}
/**
* 打印请求信息
* @param exchange
*/
private void printRequestInfo(ServerWebExchange exchange) {
try {
// 获取请求IP
String clientIp = getClientIp(exchange);
// 获取请求路径
String path = exchange.getRequest().getPath().toString();
// 获取请求方法
String method = exchange.getRequest().getMethod().toString();
// 获取请求头
HttpHeaders headers = exchange.getRequest().getHeaders();
// 获取请求参数
MultiValueMap<String, String> queryParams = exchange.getRequest().getQueryParams();
log.info("=== 请求信息 ===");
log.info("请求IP: {}", clientIp);
log.info("请求路径: {}", path);
log.info("请求方法: {}", method);
log.info("请求头: {}", headers);
log.info("请求参数: {}", queryParams);
} catch (Exception e) {
log.error("打印请求信息异常", e);
}
}
/**
* 获取客户端IP
* @param exchange
* @return
*/
private String getClientIp(ServerWebExchange exchange) {
String xForwardedFor = exchange.getRequest().getHeaders().getFirst("X-Forwarded-For");
if (StringUtils.isNotEmpty(xForwardedFor)) {
return xForwardedFor.split(",")[0].trim();
}
String xRealIp = exchange.getRequest().getHeaders().getFirst("X-Real-IP");
if (StringUtils.isNotEmpty(xRealIp)) {
return xRealIp;
}
return exchange.getRequest().getRemoteAddress() != null ?
exchange.getRequest().getRemoteAddress().getAddress().getHostAddress() : "unknown";
}
@Override
public int getOrder() {
......
......@@ -14,7 +14,7 @@ spring:
# nacos注册中心
discovery:
# 命名空间id(必须使用命名空间id)
namespace: b4196e36-e204-4e07-8612-f51b17401d6e
namespace: 8d17506b-8b43-4a3f-bf38-912a06ddf22e
# 分组
group: DEFAULT_GROUP
# nacos服务地址,配置文件中的NACOS_HOST和NACOS_PORT
......@@ -26,7 +26,7 @@ spring:
watch-delay: 1000
config:
# 命名空间(必须使用命名空间id)
namespace: b4196e36-e204-4e07-8612-f51b17401d6e
namespace: 8d17506b-8b43-4a3f-bf38-912a06ddf22e
# 分组
group: DEFAULT_GROUP
# nacos配置服务地址,配置文件中的NACOS_HOST和NACOS_PORT
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!