添加ip白名单功能

This commit is contained in:
lu7213 committed 2026-11-16 10:30:21 -05:00
1 parent c12b777a42
commit 4d656ef63c
20 files changed
+436 -6

No files matched your search

@@ -0,0 +1,130 @@
package com.ruoyi.system.security;
import com.ruoyi.common.exception.CustomException;
import java.net.InetAddress;
import java.util.LinkedHashSet;
import java.util.Set;
/**
* 商户应用共用的来源 IP 白名单。只解析 IP 字面量,不做 DNS 查询,不接受 URL、主机名或正则。
* IPv4 的 * 匹配一个完整地址段;独立 * 显式允许所有来源。空配置和非法配置一律拒绝。
*/
public final class AppIpWhitelist {
public static final int MAX_LENGTH = 2048;
private AppIpWhitelist() { }
/** 保存前校验并归一化。多条可使用逗号、分号、空白或换行分隔。 */
public static String normalize(String value) {
if (value == null || value.trim().isEmpty()) {
return "";
}
if (value.length() > MAX_LENGTH) {
throw new CustomException("IP 白名单不能超过2048个字符");
}
Set<String> rules = new LinkedHashSet<>();
for (String rule : value.trim().split("[,;\\s]+")) {
if (!validRule(rule)) {
throw new CustomException("IP 白名单格式错误:支持IP地址、IPv4整段通配符(如192.168.*.*)或独立*");
}
rules.add(rule);
}
return String.join("\n", rules);
}
public static void requireAllowed(String whitelist, String sourceIp) {
if (!isAllowed(whitelist, sourceIp)) {
throw new CustomException("来源 IP 不在应用白名单中", 403);
}
}
/** 运行时也完整校验配置,防止直接改库写入非法规则后意外放行。 */
public static boolean isAllowed(String whitelist, String sourceIp) {
String normalized;
try {
normalized = normalize(whitelist);
} catch (CustomException exception) {
return false;
}
byte[] source = address(sourceIp);
if (normalized.isEmpty() || source == null) {
return false;
}
for (String rule : normalized.split("\n")) {
if ("*".equals(rule)) {
return true;
}
if (rule.indexOf('*') >= 0) {
if (source.length != 4) {
continue;
}
String[] parts = rule.split("\\.");
boolean matches = true;
for (int index = 0; index < 4; index++) {
if (!"*".equals(parts[index]) && Integer.parseInt(parts[index]) != (source[index] & 255)) {
matches = false;
break;
}
}
if (matches) {
return true;
}
} else if (java.util.Arrays.equals(source, address(rule))) {
return true;
}
}
return false;
}
private static boolean validRule(String rule) {
if ("*".equals(rule)) {
return true;
}
if (rule.indexOf('*') < 0) {
return address(rule) != null;
}
String[] parts = rule.split("\\.", -1);
if (parts.length != 4) {
return false;
}
for (String part : parts) {
if (!"*".equals(part) && !octet(part)) {
return false;
}
}
return true;
}
private static boolean octet(String value) {
return value.matches("0|[1-9][0-9]{0,2}") && Integer.parseInt(value) <= 255;
}
/** IPv6 先限定字符集并要求冒号,避免 InetAddress 对任意字符串发起域名解析。 */
private static byte[] address(String value) {
if (value == null || value.length() > 45) {
return null;
}
if (value.indexOf(':') >= 0) {
if (!value.matches("[0-9a-fA-F:.]+")) {
return null;
}
try {
return InetAddress.getByName(value).getAddress();
} catch (Exception exception) {
return null;
}
}
String[] parts = value.split("\\.", -1);
if (parts.length != 4) {
return null;
}
byte[] result = new byte[4];
for (int index = 0; index < 4; index++) {
if (!octet(parts[index])) {
return null;
}
result[index] = (byte) Integer.parseInt(parts[index]);
}
return result;
}
}