在没有模块的情况下,如何访问JDK 11中的`sun.security.x509`?

13
我们有一个小方法,可以生成自签名的SSL证书,它显然依赖于sun.security.x509。目前,我们仍在使用JDK8构建它,尽管代码库的其余部分(只有一个小型单一库)是使用JDK11构建并在JVM11上运行。
不幸的是,在主JDK中没有替代方案,如(而CertificateFactory与生成证书几乎没有关系,与其javadoc所述相反...):
(tl,dr: 我们依赖sun.security.x509生成自签名SSL证书,需要用JDK8编译,因为在JDK11中没有替代方案。) 一种选择是使用BouncyCastle,但这会增加4MB的额外负担,尤其对于这样小的任务我们实际上并不需要,所以我正在考虑访问它的方法。
从我所看到的来看,该包和所需类仍然存在(请参见github上的sun.security.x509),但在构建时(使用maven)我遇到了错误:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project: Compilation failure: Compilation failure:
[ERROR] OldSelfSignedCertificateGenerator.java:[20,25] package sun.security.x509 does not exist
[ERROR] OldSelfSignedCertificateGenerator.java:[71,45] cannot find symbol
[ERROR]   symbol:   class X509CertInfo
[ERROR]   location: class OldSelfSignedCertificateGenerator

我进行了一些搜索并添加了:
<arg>--add-exports</arg><arg>java.base/sun.security.x509=ALL-UNNAMED</arg>

使用maven-compiler-plugin进行编译,它有些奏效 - 我只收到了WARNING,但与sun.security.x509包无关:

[WARNING] OldSelfSignedCertificateGenerator.java:[20,25] sun.security.x509.AlgorithmId is internal proprietary API and may be removed in a future release

但是!现在似乎我不情愿地进入了模块系统,并且它抱怨无法访问其他基本的Java类(以及我们的一个依赖项):

[ERROR] CertificateUtil.java:[35,17] package java.util.logging is not visible
  (package java.util.logging is declared in module java.logging, but module java.base does not read it)

我尝试以相同的方式导出java.logging模块,但没有太大的成功。似乎我还需要将这个库及其依赖项转换为模块系统,这并不是真正想要的。
这个问题与如何仅使用JDK支持的类生成自签名证书?有些相关。

简而言之,有没有一种方法可以在不使用模块系统的情况下使用JDK 11中的sun.security.x509包编译库?是否有一些简单的开关可以实现?

3个回答

14

事实证明,这可能与新JDK(9+)版本产生的构建在JDK8下不可执行有关:

<plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
        <source>9</source>
        <target>9</target>
        <release combine.self="override"></release>
        <compilerArgs>
            <arg>--add-exports</arg><arg>java.base/sun.security.x509=ALL-UNNAMED</arg>
        </compilerArgs>
    </configuration>
</plugin>

如何在Gradle中应用它? - dimirsen Z
1
没什么头绪,我倾向于完全避免使用Gradle... - Wojtek
1
你可能也想包含这些软件包 <arg>--add-exports</arg><arg>java.base/sun.security.provider=ALL-UNNAMED</arg> <arg>--add-exports</arg><arg>java.base/sun.security.pkcs=ALL-UNNAMED</arg> <arg>--add-exports</arg><arg>java.base/sun.security.util=ALL-UNNAMED</arg> - alaster
2
对于Gradle,它是这样的:compileJava { options.compilerArgs << '--add-exports=java.base/sun.security.x509=ALL-UNNAMED' } - kolobok
4
它在JDK17上停止工作了。 - Wojtek

3
为了在 Gradle 中包含 sun.security.[somePackage] 类,您可以添加以下内容:
tasks.withType(AbstractCompile) {
    options.compilerArgs += ["--add-exports", "java.base/sun.security.util=ALL-UNNAMED"]
    options.compilerArgs += ["--add-exports", "java.base/sun.security.pkcs=ALL-UNNAMED"]
}

0

我有一个包含sun.security.x509代码的junit5测试。尽管我已经配置了'maven-compiler-plugin'并添加了额外的编译参数,但测试仍然失败并出现了以下错误。 通过在'maven-surefire-plugin'中添加以下附加配置参数,我可以修复测试(在graalvm jdk 17上)。

<plugin>
     <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>3.0.0-M5</version>
        <configuration>
          <argLine>--add-opens java.base/sun.security.x509=ALL-UNNAMED</argLine>
        </configuration>
</plugin>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接