使用显式的应用程序授权和部署隔离,替代已禁用的 SecurityManager 检查。
代码对比
✕ JDK 23 and earlier
SecurityManager manager = System.getSecurityManager();
if (manager != null) {
manager.checkRead(path.toString());
}
return Files.readString(path);
✓ JDK 24+
// Untrusted users must not be able to modify this tree
Path root = allowedRoot.toRealPath();
Path resolved = root.resolve(requested)
.normalize()
.toRealPath();
if (!resolved.startsWith(root)) {
throw new SecurityException(
"Path is outside the allowed root");
}
return Files.readString(resolved);
发现此代码有问题? 告诉我们。
为什么现代方式更好
显式策略
授权在应用程序逻辑中清晰可见且可测试。
真正隔离
进程、容器和操作系统边界可保护整个应用程序。
必须迁移
移除在 JDK 24 及更高版本中已无法实施策略的检查。
旧方式
SecurityManager 检查
现代方式
显式授权
自 JDK
24
难度
高级
JDK 支持
从 SecurityManager 检查迁移到显式授权
可用
JDK 24 及更高版本必须迁移,因为 JEP 486 永久禁用了 Security Manager。
工作原理
JEP 486 在 JDK 24 中永久禁用了 Security Manager,因此通过 System.getSecurityManager() 执行的检查只能在 JDK 23 及更早版本中实施策略。应用程序必须在领域逻辑中显式授予访问权限;当不受信任的用户无法并发修改该目录树时,解析真实的文件系统路径可防止现有符号链接逃逸出允许的根目录。对于攻击者可写的目录树,应使用 SecureDirectoryStream 等抗竞态、相对于句柄的访问方式。使用进程、容器或操作系统边界实现隔离。JDK 24 暂时保留了这个已弃用的 API,但它无法启用,也没有其他进程内沙箱取代它。
相关文档
证明