我需要授予Java应用程序超级用户访问权限,以便在Mac上查看受保护的文件 [英] I need to give a java application super user access to view protected files on a mac
问题描述
如上所述. 我搜寻了网络,也获得了Mac的支持,并惹恼了Mac(OSX Lion)天才(出于绝望). 我不知道该怎么做,我真的不想坐在终端上给它命令. 有没有人遇到过这个问题或有解决方案?
As above. I have scoured the web, i also rang mac support and annoyed a mac (OSX Lion) genius (out of desperation). I have no idea how to do this, I really don't want to have to sit on top of a terminal and give it commands. Has any one encountered this or got a solution?
推荐答案
尝试查看Greg Guerin的 AuthKit 库.它是Mac专用的库,其中包装了 Mac OS X授权服务.
Try looking at Greg Guerin's AuthKit library. It is a Mac-specific library that wraps 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
.
The auth.authorize()
call will cause the standard "Please enter your password to allow program X to make changes" dialog. The user can cancel if desired, causing glguerin.authkit.UnauthorizedCancellation
to be thrown.
与使用sudo
或setuid
相比,此解决方案具有巨大的优势:它仅以root用户身份运行必要的任务.
This solution has a huge advantage over using sudo
or setuid
: it only runs the necessary tasks as root.
最后一个陷阱:AuthKit的默认JNI加载器使用Cocoa/Java桥,该桥已从Snow Leopard的Mac OS X中删除.因此,在最新版本的Mac OS X上,上面的代码将失败,并显示UnsatisfiedLinkError
.要解决此问题,请使用以下命令:
One last gotcha: the default JNI loader for AuthKit uses the Cocoa/Java bridge, which was removed from Mac OS X as of Snow Leopard. So on recent versions of Mac OS X, the code above will fail with UnsatisfiedLinkError
. To work around this, use the following:
// 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文档以了解更多信息细节.
Finally, be sure to read the AuthKit documentation for more detail.
这篇关于我需要授予Java应用程序超级用户访问权限,以便在Mac上查看受保护的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!