windows 如何从 Java 提升我的 UAC 权限?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4662574/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 15:57:42  来源:igfitidea点击:

How do I elevate my UAC permissions from Java?

javawindowsuac

提问by Brian Knoblauch

I need to use the systemRoot feature of the Preferences API, but it fails due to lack of permissions on Windows if UAC is on. I'm trying to find the technical details of popping the UAC prompt and elevating my permissions to allow the systemRoot updates to succeed.

我需要使用 Preferences API 的 systemRoot 功能,但如果 UAC 处于打开状态,它会由于缺乏 Windows 权限而失败。我试图找到弹出 UAC 提示和提升我的权限以允许 systemRoot 更新成功的技术细节。

采纳答案by Stephen C

According the accepted answer to this SO question, you cannot change the UAC permissions of a running process.

根据此 SO question的已接受答案,您无法更改正在运行的进程的 UAC 权限。

According to the answers to this SO question, possible ways to launch a process with elevated permissions are:

根据this SO question的答案,以提升的权限启动进程的可能方法是:

  • create a wrapper to launch the JVM (with the appropriate arguments!) with a windows manifest that requests raised privileges, or
  • use the application linked to the 2nd answer to run the JVM with raised privileges.
  • 创建一个包装器来启动 JVM(使用适当的参数!),并使用请求提升权限的 Windows 清单,或
  • 使用链接到第二个答案的应用程序以提高的权限运行 JVM。

回答by Kevin Day

In addition to the manifest Using JNI to call ShellExecute with verb = runas will also do this - but specifying things with a manifest is a more robust way of doing it. Getting a manifest embedded in an exe can be a bit tricky, and there were a lot of problems with manifest handling in earlier versions of Visual C++, but most of them are worked out now.

除了使用 JNI 使用动词 = runas 调用 ShellExecute 的清单外,也可以执行此操作 - 但使用清单指定内容是一种更可靠的方法。将清单嵌入到 exe 中可能有点棘手,而且在 Visual C++ 的早期版本中,清单处理存在很多问题,但现在大部分都已解决。

That said, I'd encourage you to think hard about why you need to access the system root - is it to store settings for all users? If so, you may want to consider having a separate application for managing those settings (with it's own manifest). You can't just pop open a UAC elevation dialog - you actually have to launch a new process (if you look at task manager with apps that appear to work this way, you'll see that a second instance of the app actually gets launched - look at the UAC Virtualization column in task manager to see the differences).

也就是说,我鼓励您认真思考为什么需要访问系统根目录 - 是否要为所有用户存储设置?如果是这样,您可能需要考虑使用单独的应用程序来管理这些设置(使用它自己的清单)。你不能只是弹出一个 UAC 提升对话框 - 你实际上必须启动一个新进程(如果你查看任务管理器的应用程序似乎以这种方式工作,你会看到应用程序的第二个实例实际上被启动了- 查看任务管理器中的 UAC 虚拟化列以查看差异)。

Another possibility is to adjust the security settings in the area of the registry that you absolutely must configure from your non-elevated process - but this goes against the design of UAC, and it'll almost always cause more trouble than it's worth. Probably better to drink the M$ kool-aid and design your app properly for UAC. (Believe me, I feel your pain on this - been through it a number of times).

另一种可能性是调整注册表区域中的安全设置,您绝对必须从非提升过程中对其进行配置 - 但这与 UAC 的设计背道而驰,而且它几乎总是会造成比其价值更多的麻烦。喝 M$ kool-aid 并为 UAC 正确设计您的应用程序可能更好。(相信我,我感受到你对此的痛苦 - 经历过很多次)。

As I was experiencing this pain myself, I found the following MSDN article quite helpful to understand the Microsoft design intent with UAC:

当我自己经历这种痛苦时,我发现以下 MSDN 文章对理解 Microsoft 使用 UAC 的设计意图非常有帮助:

http://msdn.microsoft.com/en-us/library/aa511445.aspx

http://msdn.microsoft.com/en-us/library/aa511445.aspx

Hope this helps...

希望这可以帮助...

回答by Dyorgio

You can use run-as-root library: https://github.com/dyorgio/run-as-root

您可以使用 run-as-root 库:https: //github.com/dyorgio/run-as-root

// Specify JVM options (optional)
RootExecutor rootExecutor = new RootExecutor("-Xmx64m");

// Execute privileged action
rootExecutor.run(() -> System.out.println("Call your admin code here."));

P.S.: I'm the author.

PS:我是作者。