0 ssl ssl-certificate spring-boot
我对这个 SSL 配置很陌生。我正在努力在 Springboot 应用程序上添加 2 路 ssl 配置。我在网上浏览了很多关于这个实现的代码片段。
我的目标服务器端点 - https://endpoint.com/path/to/service是安全的,我可以对其进行 REST 调用。
如果我设置我的 application.yml:
server:
port: 9001
ssl:
enabled: true
client-auth: need
key-store: classpath:KEYSTORE.pfx
key-store-password: password
key-alias: aliasname
key-store-type: PKCS12
trust-store: classpath:TRUSTSTORE.p12
trust-store-password: password
trust-store-type: PKCS12
Run Code Online (Sandbox Code Playgroud)
我的应用程序在https ://localhost:9001上运行。
TRUSTSTORE.p12 包含服务器的公共证书和 CA 证书。
通过此设置,我可以使用 RestTemplate 配置从服务器获取响应。当我提供另一个没有我的服务器公共证书的 TrustStore 时,它无法建立连接 - 预期的行为。
但是当我禁用 SSL 时:
server:
port: 9001
ssl:
enabled: false
Run Code Online (Sandbox Code Playgroud)
我的应用程序在 HTTP//:localhost:9001 上运行。
我仍然能够使用 SSL 上下文中可用的信任库和密钥库进行相互身份验证吗?我仍然可以使用上面的 yml 配置和下面的代码来验证服务器的公共证书吗?
// KeyStore and TrustStore
keyStore = KeyStore.getInstance("pkcs12");
ClassPathResource classPathResource = new ClassPathResource("KEYSTORE.pfx");
InputStream inputStream = classPathResource.getInputStream();
keyStore.load(inputStream, "password".toCharArray());
TS = KeyStore.getInstance("pkcs12");
ClassPathResource classPathResource1 = new ClassPathResource("TRUSTSTORE.p12");
InputStream inputStream1 = classPathResource1.getInputStream();
TS.load(inputStream1, "password".toCharArray());
// configuring the SSLContext
SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(
new SSLContextBuilder().loadTrustMaterial(TS, new TrustSelfSignedStrategy())
.loadKeyMaterial(keyStore, "password".toCharArray()).build());
Run Code Online (Sandbox Code Playgroud)
注意:当 ssl 设置为 FALSE 时,其他应用程序将通过 http://:localhost:9001 调用我的端点。我会在https://endpoint.com/path/to/service上对服务器进行 REST 调用。
如果没有 SSL,则无法进行相互 SSL 身份验证。虽然人们可能会构建某种使用普通 HTTP 而不是 HTTPS 的相互身份验证方法,但对此没有标准。在浏览器中实现的通常基于证书的相互身份验证仅适用于 HTTPS,因为它实际上是 SSL 层的一部分。
| 归档时间: |
|
| 查看次数: |
159 次 |
| 最近记录: |