我需要给一个java应用程序超级用户访问权限来查看mac上的受保护文件

And*_*ndy 2 java macos root

如上.我已经搜索过网络,我也支持mac支持并且惹恼了mac(OSX Lion)的天才(出于绝望).我不知道如何做到这一点,我真的不想坐在终端上并给它命令.有没有人遇到这个或得到解决方案?

Mat*_*nit 10

试着看看Greg Guerin的AuthKit图书馆.它是一个Mac专用库,包含Mac OS X授权服务.

这是一个例子:

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.
}
Run Code Online (Sandbox Code Playgroud)

auth.authorize()调用将导致标准"请输入您的密码以允许程序X进行更改"对话框.如果需要,用户可以取消,导致glguerin.authkit.UnauthorizedCancellation被抛出.

Mac OS X授权提示的屏幕截图

这个解决方案比使用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());
Run Code Online (Sandbox Code Playgroud)

最后,请务必阅读AuthKit文档以获取更多详细信息.