如何在Java中检查文件是否为当前用户所有?

问题描述 投票:0回答:1

如果读取的文件不属于启动 JVM 的用户所有,我想显示警告。

在我的应用中,这个文件可以让程序写入任意文件。所以我想降低权限升级攻击的风险,如果管理员处理这个文件。

Files.getOwner(path)

retruns

UserPrincipal
代表文件所有者。但是我找不到一种独立于平台的方法来检查这个
UserPrincipal
是否与当前用户匹配。

我可以用

System.getProperty("user.name")

获取当前用户并尝试匹配名称。但是可以操纵这些属性,例如,如果文件所有者是一个组,这将不起作用。

java nio file-ownership
1个回答
0
投票

如果您已经有了文件所有者的委托人,问题仍然是如何可靠地让当前用户运行应用程序。

也许这个解决方案可以提供帮助?然而,它似乎利用了 Sprint 安全性:https://www.javaguides.net/2019/06/spring-security-get-current-logged-in-user-details.html

Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
UserDetails userDetails = (UserDetails) authentication.getPrincipal();

// getUsername() - Returns the username used to authenticate the user.
System.out.println("User name: " + userDetails.getUsername());

// getAuthorities() - Returns the authorities granted to the user.
System.out.println("User has authorities: " + userDetails.getAuthorities());
© www.soinside.com 2019 - 2024. All rights reserved.