Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
sl-gateway
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
zhouwei
sl-gateway
Commits
e8c85f2b
Commit
e8c85f2b
authored
Apr 15, 2025
by
zhouwei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
target 错误时返回对应应答
parent
502c0997
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
37 additions
and
1 deletion
+37
-1
FailoverGatewayFilterFactory.java
...yan/securitylink/filter/FailoverGatewayFilterFactory.java
+37
-1
No files found.
src/main/java/com/nanyan/securitylink/filter/FailoverGatewayFilterFactory.java
View file @
e8c85f2b
...
...
@@ -347,7 +347,26 @@ public class FailoverGatewayFilterFactory extends AbstractGatewayFilterFactory<F
exchange
.
getResponse
().
getHeaders
().
putAll
(
clientResponse
.
headers
().
asHttpHeaders
());
return
clientResponse
.
bodyToMono
(
DataBuffer
.
class
)
.
flatMap
(
buffer
->
exchange
.
getResponse
().
writeWith
(
Mono
.
just
(
buffer
)))
.
flatMap
(
body
->
{
log
.
info
(
"Target request response: status={}, body={}"
,
responseStatus
,
body
);
DataBuffer
body1
=
body
;
// 检查 HTTP 状态码
if
(!
responseStatus
.
is2xxSuccessful
())
{
String
errorMessage
=
String
.
format
(
"Target request failed with status: %s, body: %s"
,
responseStatus
.
value
(),
body
);
log
.
error
(
errorMessage
);
return
writeErrorResponse
(
exchange
,
HttpStatus
.
BAD_GATEWAY
,
errorMessage
);
}
try
{
return
writeResponse
(
exchange
,
responseStatus
,
body
);
}
catch
(
Exception
e
)
{
String
errorMessage
=
String
.
format
(
"Failed to process target response: %s, body: %s"
,
e
.
getMessage
(),
body
);
log
.
error
(
errorMessage
);
return
writeErrorResponse
(
exchange
,
HttpStatus
.
BAD_GATEWAY
,
errorMessage
);
}
})
.
onErrorResume
(
throwable
->
{
log
.
info
(
"Error handling response: {}"
,
throwable
.
getMessage
());
exchange
.
getResponse
().
setStatusCode
(
HttpStatus
.
INTERNAL_SERVER_ERROR
);
...
...
@@ -358,6 +377,23 @@ public class FailoverGatewayFilterFactory extends AbstractGatewayFilterFactory<F
});
}
// 辅助方法:写入正常响应
private
Mono
<
Void
>
writeResponse
(
ServerWebExchange
exchange
,
HttpStatus
status
,
DataBuffer
buffer
)
{
exchange
.
getResponse
().
setStatusCode
(
status
);
return
exchange
.
getResponse
().
writeWith
(
Mono
.
just
(
buffer
));
}
// 辅助方法:写入错误响应
private
Mono
<
Void
>
writeErrorResponse
(
ServerWebExchange
exchange
,
HttpStatus
status
,
String
message
)
{
exchange
.
getResponse
().
setStatusCode
(
status
);
JSONObject
errorResponse
=
new
JSONObject
();
errorResponse
.
put
(
"status"
,
status
.
value
());
errorResponse
.
put
(
"message"
,
message
);
DataBuffer
buffer
=
exchange
.
getResponse
().
bufferFactory
()
.
wrap
(
errorResponse
.
toJSONString
().
getBytes
(
StandardCharsets
.
UTF_8
));
return
exchange
.
getResponse
().
writeWith
(
Mono
.
just
(
buffer
));
}
private
boolean
requiresRequestBody
(
HttpMethod
method
)
{
return
method
==
HttpMethod
.
POST
||
method
==
HttpMethod
.
PUT
||
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment