【问题标题】:Java Shipping JAR with SecurityManager Policy File带有 SecurityManager 策略文件的 Java Shipping JAR
【发布时间】:2017-12-22 21:50:11
【问题描述】:

我正在尝试发布一个执行一些 RMI 调用的 Java 应用程序。

我需要将它作为 JAR 文件发送(这是一个要求,没有办法)。

现在,为了允许某些事情(如套接字和 RMI 连接),我需要一个 SecurityManager 和一个 Policy 文件。

我想将此策略文件发送到 jar 中,并从 JAR 中设置策略路径。

现在,我的代码如下所示:

public static void main(String[] args)
{
        System.setProperty("java.security.policy","jar:file:/Policies/Server.policy");
        Policy.getPolicy().refresh();

        ... /* All other code */

}

当我将路径更改为我的 PC 上的路径并在没有 JAR 的情况下运行代码(一个 IntelliJ '应用程序')时,我在启动我的应用程序时没有问题,当我尝试运行我的 JAR 时,我得到以下异常:

Exception in thread "main" java.security.AccessControlException: access denied ("java.net.SocketPermission" "localhost:2000" "listen,resolve")

我感觉我设置的路径不对,谁能告诉我这条路径应该是什么?

【问题讨论】:

  • 当您从 JAR 运行文件时,您是否能够读取和打印文件的内容?

标签: java security jar policy


【解决方案1】:

我能够通过将代码更改为以下内容来解决此问题:

public static void main(String[] args)
{
    boolean quit = false;

    String serverPolicyPath = "/Policies/Server.policy";
    URL serverPolicyURL = Main.class.getResource(serverPolicyPath);

    if (serverPolicyURL == null)
    {
        System.err.println("getResource returned NULL");
        return;
    }

    System.setProperty("java.security.policy",serverPolicyURL.toString());
    Policy.getPolicy().refresh();

    ...

我没有尝试手动找出路径,而是让 Java 为我修复它。

【讨论】:

    猜你喜欢
    • 2014-09-27
    • 2012-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-18
    • 1970-01-01
    相关资源
    最近更新 更多