-
Notifications
You must be signed in to change notification settings - Fork 97
perf: 优化获取客户端IP地址的逻辑 #358
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
perf: 优化获取客户端IP地址的逻辑 #358
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| package com.jmal.clouddisk.util; | ||
|
|
||
| import jakarta.servlet.http.HttpServletRequest; | ||
|
|
||
| import java.util.regex.Pattern; | ||
|
|
||
| public final class IPUtil { | ||
|
|
||
| private IPUtil() { | ||
| } | ||
|
|
||
| public static final int REGION_LENGTH = 5; | ||
| public static final Pattern SPLIT_PATTERN = Pattern.compile("\\|"); | ||
|
|
||
| private static final String[] IP_HEADERS = { | ||
| "CF-Connecting-IP", | ||
| "X-Real-IP", | ||
| "X-Forwarded-For", | ||
| "Proxy-Client-IP", | ||
| "WL-Proxy-Client-IP" | ||
| }; | ||
|
|
||
| /** | ||
| * 获取客户端真实IP | ||
| * 自动处理大小写不敏感的请求头 | ||
| * | ||
| * @param request HTTP请求对象 | ||
| * @return 客户端IP地址 | ||
| */ | ||
| public static String getClientIP(HttpServletRequest request) { | ||
| for (String header : IP_HEADERS) { | ||
| String ip = request.getHeader(header); | ||
| if (ip == null) { | ||
| continue; | ||
| } | ||
| String firstIp = extractFirstIp(ip); | ||
| if (isValidIp(firstIp)) { | ||
| return firstIp; | ||
| } | ||
| } | ||
| return request.getRemoteAddr(); | ||
| } | ||
|
|
||
| /** | ||
| * 提取第一个IP(处理逗号分隔的多IP情况) | ||
| */ | ||
| private static String extractFirstIp(String ip) { | ||
| if (ip.contains(",")) { | ||
| int commaIndex = ip.indexOf(','); | ||
| return ip.substring(0, commaIndex).trim(); | ||
| } | ||
| return ip.trim(); | ||
| } | ||
|
|
||
| /** | ||
| * 检查IP是否有效 | ||
| */ | ||
| private static boolean isValidIp(String ip) { | ||
| return ip != null | ||
| && !ip.isEmpty() | ||
| && !"unknown".equalsIgnoreCase(ip); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
当前的IP验证逻辑 (
isValidIp) 过于宽松,只检查了字符串是否为非空且不等于 'unknown'。这可能允许格式不正确的字符串(例如,恶意注入的文本)被当作有效的IP地址处理和记录,存在潜在的日志注入风险。为了增强安全性和数据准确性,建议使用
hutool库中的NetUtil.isIP()方法来进行更严格的IP地址格式验证。这将确保只接受有效的IPv4或IPv6地址。为了方便阅读,建议在文件顶部添加
import cn.hutool.core.net.NetUtil;,然后在代码中使用NetUtil.isIP(ip)。