在哪里放置java小程序策略文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2612895/
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-08-13 09:53:18  来源:igfitidea点击:

Where to place java applet policy file?

javaappletpolicy

提问by makdere

I am working on an artificial intelligence project which is a logic game and aims two user connecting to the server on the network who acts as an Admin and then start to play one by one.

我正在研究一个人工智能项目,这是一个逻辑游戏,目标是两个用户连接到网络上的服务器,作为管理员,然后开始一个一个地玩。

In order to create connections, i have a server code which is just listening on localhost:8000 and assigning team values to the clients as they arrive. After connecting, clients make their move under Admin's control.

为了创建连接,我有一个服务器代码,它只是侦听 localhost:8000 并在客户端到达时将团队值分配给他们。连接后,客户端在管理员的控制下移动。

The question is that when i try to put my code to work in the browser it fails with the following error:

问题是,当我尝试将我的代码放在浏览器中时,它失败并出现以下错误:

java.security.AccessControlException: access denied (java.net.SocketPermission 127.0.0.1:8000 connect,resolve)

Even though i have created my own policy, first granting only Socket access permission to the codebase of my project folder (file:///home/xxx/projects/-), after it didn't work i granted allpermissions from allcodebase. I tried placing my policy file both in the home directory and in the same directory where my applet code resides.

即使我已经创建了自己的策略,首先只授予对我的项目文件夹 ( file:///home/xxx/projects/-)代码库的 Socket 访问权限,但在它不起作用后,我授予所有代码库的所有权限。我尝试将我的策略文件放在主目录和我的小程序代码所在的同一目录中。

Appreciate any tips, thanks.

感谢任何提示,谢谢。

采纳答案by objects

by default I think it looks for a file named .java.policy in your home directory

默认情况下,我认为它会在您的主目录中查找名为 .java.policy 的文件

you can check the file /lib/security/java.security to see where it looks. check the keys named policy.url.n

您可以检查文件 /lib/security/java.security 以查看它的位置。检查名为 policy.url.n 的键

回答by colinjwebb

Place the policy file where the JRE is.

将策略文件放在 JRE 所在的位置。

For instance, my policy file is located at C:\Program Files\Java\jre1.6.0_01\lib\security

例如,我的策略文件位于 C:\Program Files\Java\jre1.6.0_01\lib\security

回答by Tom Hawtin - tackline

I strongly suggest respecting the same-origin policy. Security implications are not necessarily obvious at the best of times. Also, an applet with standard permissions has better usability and should be easier to maintain.

我强烈建议尊重同源政策。在最好的情况下,安全影响不一定很明显。此外,具有标准权限的小程序具有更好的可用性并且应该更易于维护。

回答by eckes

If you grant a permission to a specific JAR (codebase), this only works if the whole execution stack from the start of the thread to the method invocation is in that codebase/protection domain.

如果您授予特定 JAR(代码库)的权限,则这仅在从线程开始到方法调用的整个执行堆栈都在该代码库/保护域中时才有效。

If your method is alibrary, and you are sure that you access the operation safely, you can use a Priveledged Block, which essentially makes sure the part of the stack calling your block is not considered in the access calculation.

如果您的方法是库方法,并且您确定可以安全地访问该操作,则可以使用 Priveledged 块,这实质上确保在访问计算中不考虑调用您的块的堆栈部分。

Privileged Block, as described here:

特权块,如下所述:

http://docs.oracle.com/javase/6/docs/technotes/guides/security/doprivileged.html

http://docs.oracle.com/javase/6/docs/technotes/guides/security/doprivileged.html

Greetings Bernd

问候伯恩德

回答by sdflkj

You can set the location of the security policy file by using this command-line option

您可以使用此命令行选项设置安全策略文件的位置

-Djava.security.policy=policyfilepath

with the javacommand.

java命令。

Or you can even set this propery using the System.setProperty()procedure.

或者您甚至可以使用该System.setProperty()过程设置此属性。