如上所述。 我浏览了网络,还给 mac 支持人员打电话,并惹恼了一位 mac (OSX Lion) 天才(出于绝望)。 我不知道如何做到这一点,我真的不想坐在终端上并给它命令。 有人遇到过这个问题或者有解决办法吗?
最佳答案
尝试查看 Greg Guerin 的 AuthKit图书馆。它是一个特定于 Mac 的库,包含 Mac OS X Authorization Services .
这是一个例子:
import glguerin.authkit.*;
Privilege priv = new Privilege("system.privilege.admin");
Authorization auth = new MacOSXAuthorization();
try
{
// This will cause an authentication prompt to be
// shown to the user, requesting the "system.privilege.admin"
// privilege.
auth.authorize(priv, true);
// If we reach this point, we can execute privileged programs.
// Load the secured file.
Process proc = auth.execPrivileged(new String[] { "/bin/cat", "/root/securefile" });
InputStream inputStream = proc.getInputStream();
// Use standard I/O mechanisms to read the input.
}
catch (UnauthorizedCancellation e)
{
// User chose not to authorize the application.
// Handle appropriately.
}
auth.authorize()
调用将导致标准的“请输入您的密码以允许程序 X 进行更改”对话框。如果需要,用户可以取消,从而导致 glguerin.authkit.UnauthorizedCancellation
被扔掉。
与使用 sudo
相比,该解决方案具有巨大优势或setuid
:它仅以 root 身份运行必要的任务。
最后一个问题:AuthKit 的默认 JNI 加载程序使用 Cocoa/Java 桥接器,该桥接器从 Snow Leopard 开始从 Mac OS X 中删除。因此,在最新版本的 Mac OS X 上,上面的代码将失败并显示 UnsatisfiedLinkError
。要解决此问题,请使用以下命令:
// Put this class somewhere:
public class AuthKitLibLoader extends LibLoader
{
@Override
protected File makeFallbackDir()
{
return new File(".");
}
}
// Then, before calling AuthKit (using the above example), do this:
// Hook in our "Snow Leopard-safe" extension to AuthKit (see below).
System.setProperty("glguerin.util.LibLoader.imp", AuthKitLibLoader.class.getName());
最后,请务必阅读 AuthKit documentation了解更多详情。
关于java - 我需要授予 java 应用程序 super 用户访问权限以查看 Mac 上 protected 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7408219/