java - Spring Boot 中的自签名证书和 SSL
问题描述
对于一个学校项目,我有一个客户端和一个 rest-api。用户使用登录表单登录客户端。当他们执行某个从客户端调用 API 服务器的操作时,客户端首先通过 URL 发送用户名和密码进行身份验证。然后,rest-api 会发回一个 JWT 令牌供客户端在后续调用中使用。
我现在正在尝试实现 HTTPS 来发送用户名和密码以进行身份验证。我已遵循本指南
但是,该指南使用 resttemplate 而不是 webclient(我在客户端使用它来调用 rest-api 端点)。
因此,我根据指南生成了一个 pk12 文件,并将其与以下属性一起添加到其余服务器:
#https config
server.ssl.key-store-type=PKCS12
# The path to the keystore containing the certificate
server.ssl.key-store=classpath:keystore/rideService.p12
# The password used to generate the certificate
server.ssl.key-store-password=geheim
# The alias mapped to the certificate
server.ssl.key-alias=rideService
security.require-ssl=true
根据邮递员的说法,这有效。
现在我正在尝试配置客户端,但我无法让它工作。我已将相同的 pk12 文件添加到资源中,并将以下代码添加到 webclient:
@Bean
public WebClient createSSLWebClient() throws InvalidAlgorithmParameterException, KeyStoreException, IOException, CertificateException, NoSuchAlgorithmException, UnrecoverableKeyException {
KeyStore keyStore = KeyStore.getInstance("PKCS12");
keyStore.load(new FileInputStream(ResourceUtils.getFile(trustStorePath)), trustStorePass.toCharArray());
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("SunX509");
keyManagerFactory.init(keyStore, trustStorePass.toCharArray());
SslContext sslContext = SslContextBuilder
.forClient()
.keyManager(keyManagerFactory)
.build();
HttpClient httpClient = HttpClient.create().secure(t -> t.sslContext(sslContext));
return WebClient.builder()
.clientConnector(new ReactorClientHttpConnector(httpClient))
.filter(logResponse()) // filterfunctie toevoegen aan de WebClient
.baseUrl(API_BASE_URL)
.build();
}
我总是收到“错误的请求”。我仍然开始了解这一切,所以我也尝试将其设置为信任管理器,但结果相同......谁能告诉我我做错了什么?
谢谢
解决方案
您是否真的为您的主机创建了 SSL 证书?也许这就是你缺少的部分。通过快速浏览,我根本没有看到在密钥库中设置 SSL 的教程。
推荐阅读
- scala - 从 Spark 中的单个数据帧中删除 Column 列表
- c# - How to host ASP.Net Core API 2.2 in IIS Server?
- azure - Cannot change workitem's parent through the azure devops api
- sorting - 排序在 golang 链码超级账本结构中不起作用
- r - Is there any way to use a slider in flexdashboard without shiny?
- c++ - Why fstream prints out last record of a file
- python - 在 Heroku 上加载应用程序依赖项时出现问题
- terraform - BucketRegionError: incorrect region, the bucket is not in 'eu-west-2' - terraform
- android - Parse JSON from a URL using Volley library in Android
- python - Equality operation does not work for Pandas dataframe value in one or more cells but work for others