java.security.AccessControlException:access denied(“java.lang.RuntimePermission”“loadLibrary.jpcap”)

时间:2014-02-16 06:41:57

标签: java javafx-2 jpcap accesscontrolexception

我的学校项目的应用程序有jpcap类,我在尝试运行jar文件时遇到这些异常:

java.lang.ExceptionInInitializerError
...
Caused by: java.security.AccessControlException: access denied ("java.lang.RuntimePermission" "loadLibrary.jpcap")
at java.security.AccessControlContext.checkPermission(Unknown Source)
at java.security.AccessController.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkPermission(Unknown Source)
at sun.plugin2.applet.FXAppletSecurityManager.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkLink(Unknown Source)
at java.lang.Runtime.loadLibrary0(Unknown Source)
at java.lang.System.loadLibrary(Unknown Source)
at jpcap.JpcapCaptor.<clinit>(JpcapCaptor.java:251)
... 12 more

有没有办法允许我的代码而不添加:

grant { permission java.security.AllPermission; };

到jre location \ lib \ security \ java.policy?

1 个答案:

答案 0 :(得分:0)

对于遇到同样问题的所有人:

  1. 首先,您必须在applet中添加MANIFEST.MF文件。检查:Simpliest way to add an attribute to a jar Manifest in Maven以获取签名和将清单文件添加到applet的方法。 确保此配置不会在applet参数配置中覆盖

  2. Java 8+需要签名的applet,因此您必须签署您的applet。如果您在开发环境中,则必须对applet进行自签名并将此证书添加到Java控制面板。请参阅a way to add Self-Signed Certificates to the List of Trusted Certificates in the Java Runtime

  3. 即使在此之后,仍有一些代码仅与PrivilegedActions一起运行。请参阅此答案:https://stackoverflow.com/a/1730904/2692914

  4. 无论如何,我就是这样做的,我已经使用了这个Minimal Java Applet built with Maven as a base project

    <强> MANIFEST.MF

    Manifest-Version: 1.0
    Application-Name: One Applet
    Codebase: *
    Permissions: all-permissions
    Application-Library-Allowable-Codebase: *
    Caller-Allowable-Codebase: *
    Trusted-Only: false
    Trusted-Library: false
    

    <强>的pom.xml

    <build>
    <plugins>
    ...
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <configuration>
            <descriptorRefs>
                <descriptorRef>jar-with-dependencies</descriptorRef>
            </descriptorRefs>
            <archive>
                <index>true</index>
                <manifestFile>${project.basedir}/MANIFEST.MF</manifestFile>
                <manifest>
                    <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                </manifest>
            </archive>
        </configuration>
        <executions>
            <execution>
                <id>make-assembly</id>
                <phase>package</phase>
                <goals>
                    <goal>attached</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jarsigner-plugin</artifactId>
        <version>1.2</version>
        <configuration>
            <keystore>src/main/resources/minimal-keystore.jks</keystore>
            <alias>minimal</alias>
            <storepass>abcd1234</storepass>
            <keypass>abcd1234</keypass>
        </configuration>
        <executions>
            <execution>
                <id>sign</id>
                <goals>
                    <goal>sign</goal>
                </goals>
            </execution>
            <execution>
                <id>verify</id>
                <goals>
                    <goal>verify</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
    ...
    </plugins>
    </build>
    

    <强> minimal.cer

    keytool -export -keystore minimal-keystore.jks -alias minimal -file minimal.cer
    

    Adding cert to the Java Control Panel

    <强> SomeClass.java

    try {
        AccessController.doPrivileged(new PrivilegedAction() {
            public Object run() {
                try {
                    for (String dll : dlls) {
                        String dllPath = basePath + dll;
                        System.out.println("Cargando: " + dllPath);
                        System.load(dllPath);
                    }
                    return null;
                } catch (Exception e) {
                    e.printStackTrace();
                    throw e;
                }
            }
        });
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    }
    

    嗯,那就是它!