gpt4 book ai didi

java - Spring Boot 中的自签名证书和 SSL

转载 作者:行者123 更新时间:2023-12-01 16:23:53 69 4
gpt4 key购买 nike

对于一个学校项目,我有一个客户端和一个rest-api。用户使用登录表单登录客户端。当他们执行从客户端调用 API 服务器的特定操作时,客户端首先通过 URL 发送用户名和密码进行身份验证。然后,rest-api 会发回 JWT token ,供客户端在后续调用中使用。

我现在正在尝试实现 HTTPS 来发送用于身份验证的用户名和密码。我已经关注了这个guide

但是,该指南使用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

根据 postman 的说法,这有效。

现在我正在尝试配置客户端,但无法让它工作。我已将相同的 pk12 文件添加到资源中,并将以下代码添加到 Web 客户端:

@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 证书?也许这就是你所缺少的部分。快速浏览了一下,我根本没有看到在 keystore 中设置 SSL 的教程。

关于java - Spring Boot 中的自签名证书和 SSL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62198424/

69 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com