我在本地主机上使用ssl运行Springboot应用程序。我有一些集成测试来验证ssl工作:
application.properties
server.ssl.key-store-type=PKCS12
server.ssl.key-store=classpath:ssl/localhost.p12
server.ssl.key-store-password=localhost_ssl
server.ssl.key-alias=localhost_ssl
server.ssl.enabled=true
集成测试
@ParameterizedTest
@MethodSource("getSslTestParameters")
void onlyAllowSecureCallsToEndpoint(boolean includesSecure, ResultMatcher expectedStatus) throws Exception {
MockHttpServletRequestBuilder postCall = post("/api/tutors/register")
.content(objectMapper.writeValueAsString(TutorRegistrationDTO.builder().build()))
.contentType(APPLICATION_JSON)
.with(csrf());
if (includesSecure) {
postCall.secure(true);
}
mvc.perform(postCall).andExpect(expectedStatus);
}
private static Stream<Arguments> getSslTestParameters() {
return Stream.of(
Arguments.of(true, status().isOk()),
Arguments.of(false, status().isFound()));
}
当我使用IntelliJ和Maven运行时,这个测试在Java 8中运行良好。现在我升级到Java 17,当我用IntelliJ运行它时,它仍然可以工作,但当我运行Maven构建时,它就停止工作了。我得到以下错误:
IOException: Failed to load keystore type [PKCS12] with path [jar:file:/C:/mypath/target/app-1.0.0-SNAPSHOT.jar!/ssl/localhost.p12] due to [Illegal char <:> at index 3: jar:file:C:mypathtargetapp-1.0.0-SNAPSHOT.jar!ssllocalhost.p12]
我可以确认localhost.p12
在jar中可用,但它在解析.p12文件时似乎有问题。我认为文件本身没有错,因为测试仍然可以与IntelliJ一起工作。
我已经通过设置postCall.secure(false)
验证了IntelliJ没有给我一个假阳性,并且验证了测试失败。
在Java 8和Java 17之间ssl的工作方式有什么变化吗?我在文档中找不到任何相关的内容。
编辑:我应该补充说,应用程序仍然运行没有问题。问题完全出在测试上。
它不再正确地解释从Maven到jar的路径。它抱怨路径jar:...
,分号。
我通过将/ssl/localhost.p12
复制到集成测试Maven模块的测试资源中来解决这个问题。
我没有解释为什么它可以解释Java 8中的jar路径,而不是Java 17中的路径。