如何从 Java 提升 UAC 权限?
我需要使用 Preferences API 的 systemRoot 功能,但如果打开 UAC,则由于 Windows 上缺乏权限而失败。我正在尝试查找弹出 UAC 提示并提升权限以允许 systemRoot 更新成功的技术细节。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据这个SO问题的接受答案,您无法更改正在运行的进程的UAC权限。
根据这个SO问题的答案,启动具有提升权限的进程的可能方法是:
According the accepted answer to this SO question, you cannot change the UAC permissions of a running process.
According to the answers to this SO question, possible ways to launch a process with elevated permissions are:
您可以使用 run-as-root 库: https://github.com/dyorgio/run- as-root
PS:我是作者。
You can use run-as-root library: https://github.com/dyorgio/run-as-root
P.S.: I'm the author.
除了清单之外,使用 JNI 通过 verb = runas 调用 ShellExecute 也可以执行此操作 - 但使用清单指定内容是一种更可靠的方法。获取嵌入到 exe 中的清单可能有点棘手,并且早期版本的 Visual C++ 中的清单处理存在很多问题,但现在大多数问题都已解决。
也就是说,我鼓励您认真思考为什么需要访问系统根目录 - 是为了存储所有用户的设置吗?如果是这样,您可能需要考虑使用单独的应用程序来管理这些设置(带有自己的清单)。您不能只是弹出 UAC 提升对话框 - 您实际上必须启动一个新进程(如果您查看任务管理器以及似乎以这种方式工作的应用程序,您会看到该应用程序的第二个实例实际上已启动- 查看任务管理器中的 UAC 虚拟化列以查看差异)。
另一种可能性是调整注册表区域中的安全设置,您绝对必须从非提升进程中配置该区域 - 但这违背了 UAC 的设计,并且几乎总是会造成比其价值更多的麻烦。也许更好的是喝 M$ kool-aid 并为 UAC 正确设计你的应用程序。 (相信我,我感受到你对此的痛苦 - 经历过很多次)。
由于我自己也经历过这种痛苦,我发现以下 MSDN 文章对于理解 Microsoft UAC 的设计意图非常有帮助:
http://msdn.microsoft.com/en-us/library/aa511445.aspx
希望这有帮助...
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.
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).
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).
As I was experiencing this pain myself, I found the following MSDN article quite helpful to understand the Microsoft design intent with UAC:
http://msdn.microsoft.com/en-us/library/aa511445.aspx
Hope this helps...