英文:
Access `sun.security.x509` in JDK 11 without modules?
问题
(摘要:)
我们有一个小的方法用于生成自签名的SSL证书,它明显依赖于 sun.security.x509
。目前,由于这个原因,我们仍然在使用JDK8构建它,尽管代码库的其余部分(仅是一个小型单一库)是使用JDK11构建和在JVM11上运行的。
不幸的是,在主要的JDK中没有替代方法,如下(而且 CertificateFactory
与生成证书几乎没有关系,与其javadoc所述相反...):
- https://bugs.java.com/bugdatabase/view_bug.do?bug_id=JDK-8165481
- https://bugs.java.com/bugdatabase/view_bug.do?bug_id=8058778
一个选项是使用BouncyCastle,但那是额外的4MB,我们实际上并不需要,特别是对于这么小的任务,因此我正在考虑在不涉及模块系统的情况下访问它。
据我所见,包和所需的类仍然存在(参见sun.security.x509
在github上的链接),但是在构建它时(使用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
我进行了一些搜索,并在 maven-compiler-plugin
中添加了以下内容:
<arg>--add-exports</arg><arg>java.base/sun.security.x509=ALL-UNNAMED</arg>
这在某种程度上起作用 - 我只收到与 sun.security.x509
包无关的 WARNING
:
[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
模块,但没有太大的成功。而且似乎我还需要将这个库及其依赖项都转换为模块系统,这实际上并不是想要的。
问题与https://stackoverflow.com/questions/39143858/how-to-generate-a-self-signed-certificate-using-only-jdk-supported-classes 有些关联。
tl,dr;
在JDK 11下,有没有一种方式可以在不使用模块系统的情况下编译使用 sun.security.x509
包的库?是否有一些简单的开关可以实现这一点?
英文:
(tl,dr at the end)
We have a small method that generates self-signed SSL certificate and it obviously depends on sun.security.x509
. Currently we are still building it using JDK8 because of that, even though the rest of the codebase (it's only small, single library) is build using JDK11 and run with JVM11.
Unfortunately there aren't replacement in the main JDK, as per (and CertificateFactory
has little to nothing with generating certificates, contrary to what it's javadoc states…):
- https://bugs.java.com/bugdatabase/view_bug.do?bug_id=JDK-8165481
- https://bugs.java.com/bugdatabase/view_bug.do?bug_id=8058778
One option would be to use BouncyCastle, but that's additional 4MB that we really don't need, especially for such small task so I was pondering ways to access it while
From what I saw, the package and required classes are still package and relevant classes are still there (see sun.security.x509
on github but when building it (using maven) I get error:
[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
I was searching a bit and adding:
<arg>--add-exports</arg><arg>java.base/sun.security.x509=ALL-UNNAMED</arg>
to maven-compiler-plugin
and it somewhat worked - I only get WARNING
not regarding sun.security.x509
package:
[WARNING] OldSelfSignedCertificateGenerator.java:[20,25] sun.security.x509.AlgorithmId is internal proprietary API and may be removed in a future release
BUT! Now it seems I entered (unwillingly!) module system and it complains about access to other, basic Java classes (and one more our dependency):
[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)
I tried adding java.logging
module in the same manner to exports but without much success. It also seems that I would have to convert both this library and it's dependency to module system, which is not really desired.
The question is somewhat related to https://stackoverflow.com/questions/39143858/how-to-generate-a-self-signed-certificate-using-only-jdk-supported-classes
tl,dr;
is there a way to compile library using sun.security.x509
package under JDK 11 without module system? Some simple switch?
答案1
得分: 13
<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>
英文:
It turns out that presumably it has to do with the fact that builds produced by newer JDK (9+) Versions won't be executable under 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>
答案2
得分: 2
要在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"]
}
英文:
To include sun.security.[somePackage]
classes in gradle you may add:
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"]
}
</details>
# 答案3
**得分**: 0
```xml
<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>
英文:
I had a junit5 test with sun.security.x509 code inside. Test failed with such error although I had a 'maven-compiler-plugin' configured with additional compile parameters above.
I could fix test (on graalvm jdk 17) by adding additional config parameter to 'maven-surefire-plugin' like below.
<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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论