Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
dj
/
YouJia
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit ffcea896
authored
Oct 09, 2026
by
叶立庆
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
优化日志打印、返回时间字段、测试环境配置
1 parent
a7c4258d
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
21 additions
and
4 deletions
youjia-framework/src/main/java/com/youjia/framework/util/WebUtils.java
youjia-framework/src/main/java/com/youjia/framework/web/RequestLoggingFilter.java
youjia-server/src/main/resources/application-test.yml
youjia-framework/src/main/java/com/youjia/framework/util/WebUtils.java
View file @
ffcea89
...
@@ -21,6 +21,10 @@ public final class WebUtils {
...
@@ -21,6 +21,10 @@ public final class WebUtils {
/**
/**
* 获取客户端真实 IP(多级代理场景取第一个非 unknown 值)。
* 获取客户端真实 IP(多级代理场景取第一个非 unknown 值)。
*
* <p>依赖 {@link RequestContextHolder} 中的请求对象,仅可在请求线程内、且
* {@code RequestContextFilter} 已执行之后调用(如 Controller / Service 层)。
* 在过滤器中 {@code RequestContextHolder} 可能尚未设置,请改用 {@link #getClientIp(HttpServletRequest)}。</p>
*/
*/
public
static
String
getClientIp
()
{
public
static
String
getClientIp
()
{
ServletRequestAttributes
attributes
=
ServletRequestAttributes
attributes
=
...
@@ -28,7 +32,17 @@ public final class WebUtils {
...
@@ -28,7 +32,17 @@ public final class WebUtils {
if
(
attributes
==
null
)
{
if
(
attributes
==
null
)
{
return
null
;
return
null
;
}
}
HttpServletRequest
request
=
attributes
.
getRequest
();
return
getClientIp
(
attributes
.
getRequest
());
}
/**
* 直接从请求对象解析客户端真实 IP(适用于已持有 {@link HttpServletRequest} 的场景,
* 例如过滤器中 {@code RequestContextHolder} 尚未被设置的阶段)。
*/
public
static
String
getClientIp
(
HttpServletRequest
request
)
{
if
(
request
==
null
)
{
return
null
;
}
for
(
String
header
:
IP_HEADERS
)
{
for
(
String
header
:
IP_HEADERS
)
{
String
ip
=
request
.
getHeader
(
header
);
String
ip
=
request
.
getHeader
(
header
);
if
(
StringUtils
.
hasText
(
ip
)
&&
!
UNKNOWN
.
equalsIgnoreCase
(
ip
))
{
if
(
StringUtils
.
hasText
(
ip
)
&&
!
UNKNOWN
.
equalsIgnoreCase
(
ip
))
{
...
...
youjia-framework/src/main/java/com/youjia/framework/web/RequestLoggingFilter.java
View file @
ffcea89
package
com
.
youjia
.
framework
.
web
;
package
com
.
youjia
.
framework
.
web
;
import
com.youjia.framework.util.WebUtils
;
import
jakarta.servlet.FilterChain
;
import
jakarta.servlet.FilterChain
;
import
jakarta.servlet.ServletException
;
import
jakarta.servlet.ServletException
;
import
jakarta.servlet.http.HttpServletRequest
;
import
jakarta.servlet.http.HttpServletRequest
;
...
@@ -90,7 +91,9 @@ public class RequestLoggingFilter extends OncePerRequestFilter {
...
@@ -90,7 +91,9 @@ public class RequestLoggingFilter extends OncePerRequestFilter {
private
void
logEnter
(
HttpServletRequest
request
)
{
private
void
logEnter
(
HttpServletRequest
request
)
{
StringBuilder
sb
=
new
StringBuilder
();
StringBuilder
sb
=
new
StringBuilder
();
// 增加请求ip打印
sb
.
append
(
"[请求进入] "
)
sb
.
append
(
"[请求进入] "
)
.
append
(
" | ip="
).
append
(
WebUtils
.
getClientIp
(
request
)).
append
(
' '
)
.
append
(
request
.
getMethod
()).
append
(
' '
)
.
append
(
request
.
getMethod
()).
append
(
' '
)
.
append
(
request
.
getRequestURI
());
.
append
(
request
.
getRequestURI
());
if
(
properties
.
isIncludeQueryString
()
&&
request
.
getQueryString
()
!=
null
)
{
if
(
properties
.
isIncludeQueryString
()
&&
request
.
getQueryString
()
!=
null
)
{
...
...
youjia-server/src/main/resources/application-test.yml
View file @
ffcea89
...
@@ -14,15 +14,15 @@ spring:
...
@@ -14,15 +14,15 @@ spring:
-
nacos:youjia-test.yaml
-
nacos:youjia-test.yaml
cloud
:
cloud
:
nacos
:
nacos
:
server-addr
:
${NACOS_SERVER_ADDR:192.168.
3.102:8868
}
server-addr
:
${NACOS_SERVER_ADDR:192.168.
2.11:28080
}
username
:
${NACOS_USERNAME:nacos}
username
:
${NACOS_USERNAME:nacos}
password
:
${NACOS_PASSWORD:nacos}
password
:
${NACOS_PASSWORD:nacos}
discovery
:
discovery
:
# 建议在 Nacos 上为 test 建独立命名空间,并把「命名空间 ID」填到这里(或 NACOS_NAMESPACE)
# 建议在 Nacos 上为 test 建独立命名空间,并把「命名空间 ID」填到这里(或 NACOS_NAMESPACE)
namespace
:
${NACOS_NAMESPACE:}
namespace
:
${NACOS_NAMESPACE:
e8fb0ade-536c-4cd9-9657-9c904b683fb5
}
group
:
${NACOS_GROUP:DEFAULT_GROUP}
group
:
${NACOS_GROUP:DEFAULT_GROUP}
config
:
config
:
namespace
:
${NACOS_NAMESPACE:}
namespace
:
${NACOS_NAMESPACE:
e8fb0ade-536c-4cd9-9657-9c904b683fb5
}
group
:
${NACOS_GROUP:DEFAULT_GROUP}
group
:
${NACOS_GROUP:DEFAULT_GROUP}
youjia
:
youjia
:
...
...
Write
Preview
Markdown
is supported
Attach a file
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 post a comment