java.security.AccessControlException:使用 Java Web Start 访问被拒绝

发布于 2024-08-22 00:14:17 字数 520 浏览 10 评论 0原文

我在使用 JWS (Java Web Start) 访问文件时遇到一些问题。该程序添加了新的标签和图像。该程序在我的本地计算机上运行良好,但当我使用 JWS 在远程服务器上运行该程序时,会出现错误页面。以下是错误示例:

Exception in thread "AWT-EventQueue-0" java.security.AccessControlException: access denied (java.io.FilePermission add2.png read)
 at java.security.AccessControlContext.checkPermission(Unknown Source)
 at java.security.AccessController.checkPermission(Unknown Source)
 at java.lang.SecurityManager.checkPermission(Unknown Source)

即使在确保图像具有读取访问权限后也会发生这种情况。

有想法吗?

I am having some issues with accessing files using JWS (Java Web Start). The program adds a new label and image. The program runs fine on my local computer but gives me pages of errors when I run the program on my remote server using JWS. Here's a sample of the error:

Exception in thread "AWT-EventQueue-0" java.security.AccessControlException: access denied (java.io.FilePermission add2.png read)
 at java.security.AccessControlContext.checkPermission(Unknown Source)
 at java.security.AccessController.checkPermission(Unknown Source)
 at java.lang.SecurityManager.checkPermission(Unknown Source)

This occurs even after making sure the images have read access.

Ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

硬不硬你别怂 2024-08-29 00:14:17

与小程序一样,JNLP(Webstart)在客户端计算机上运行,​​而不是在服务器计算机上运行。客户端从网页下载程序并在本地计算机上运行。 java.io 中的任何引用都将指向本地磁盘文件系统(代码运行的地方),而不是远程磁盘文件系统(代码下载的地方),正如您所看到的那样预计。

您有 2 个选择:

  1. 将映像打包到 JAR 中并使用 ClassLoader#getResourceAsStream() 来从中获取 InputStream

    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    BufferedImage image = ImageIO.read(classLoader.getResourceAsStream("add2.png"));
    
  2. 将图像放在网络服务器中的公共位置,以便您可以通过 URL 访问它(仅比类路径中慢一点)。

    BufferedImage image = ImageIO.read(new URL("http://example.com/add2.png"));
    

也就是说,无论如何,在 java.io 中使用相对路径而不是绝对路径都是一个坏主意。永远不要这样做。它将依赖于当前的工作目录,您无法控制该目录。

Like applets, JNLP (webstart) runs at client machine, not at server machine. The client downloads the program from a webpage and runs it at local machine. Any references in java.io stuff will point to the local disk file system (there where the code runs), not the remote disk file system (there where the code is been downloaded from) as you seem to expect.

You have 2 options:

  1. Pack the image in the JAR and use ClassLoader#getResourceAsStream() instead to obtain an InputStream from it.

    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    BufferedImage image = ImageIO.read(classLoader.getResourceAsStream("add2.png"));
    
  2. Put the image in public location in the webserver, so that you can access it by URL (is only a tad slower than having in classpath).

    BufferedImage image = ImageIO.read(new URL("http://example.com/add2.png"));
    

That said, regardless of all, using relative paths instead of absolute paths in java.io stuff is a bad idea. Never do this. It will be dependent on the current working directory, which you have no control over.

虫児飞 2024-08-29 00:14:17

add2.png 在哪里?如果它位于您的本地文件系统上(而不是具有 .jnlp 文件的服务器),则不允许这样做,以保护用户的隐私。此外,也不允许从其他 Web 服务器获取资源。

请记住,JWS 和小程序代码通常是不受信任的(除非它经过数字签名并被用户接受)。因此,适用于它们的默认权限必须相当严格。


编辑添加:从堆栈跟踪看来,您的程序仍在尝试读取本地文件,而不是使用远程服务器的 URL。确保您的代码没有对 java.io.File 进行任何引用;这将帮助您查明任何有问题的代码区域。

Where is add2.png? If it's on your local filesystem (as opposed to the server that has the .jnlp file), then this is not allowed, to protect the user's privacy. Also, getting the resource from another web server is not allowed either.

Remember that JWS and applet code is generally untrusted (unless it's been digitally signed, and accepted by the user). So, the default permissions applying to them have to be fairly restrictive.


Edited to add: It seems from the stacktrace that your program is still trying to read the local file, rather than using the URL to the remote server. Make sure your code does not make any references to java.io.File; this will help you pinpoint any problematic areas of code.

故人爱我别走 2024-08-29 00:14:17

面临类似的问题。

通过从控制面板 -> 删除临时 Internet 文件来解决爪哇

Faced similar issue.

Resolved by removing Temporary Internet Files from Control Panel -> Java

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文