java - 我需要授予 java 应用程序 super 用户访问权限以查看 Mac 上 protected 文件

标签 java macos root

如上所述。 我浏览了网络,还给 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被扔掉。

screen shot of Mac OS X authorization prompt

与使用 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/

相关文章:

macos - AudioFileReadPackets错误-50

macos - 如何让 strace 以非 root 权限运行跟踪命令?

java - 在 Tomcat 中运行 WAR 在 WildFly 中失败

java - groovy 脚本中使用 java 的 Hello World 示例给出了空指针

java - Spring MVC JSP表单错误

c# 检查给定的 Sharepoint 路径是否是根 sharepoint 路径?

mongodb - 将 MongoDB 从 root 更改为 Sudo 用户

java - MongoDB:getLastError() 返回什么

c++ - 如何从现有的 git cmake 项目创建 Xcode 项目?

ios - 在类中定义的闭包中到达 "self"