gpt4 book ai didi

java - TrustManager 只允许一个特定的不受信任的证书

转载 作者:行者123 更新时间:2023-11-29 08:53:00 25 4
gpt4 key购买 nike

我正在围绕我们内部使用的一些其余 Web 服务构建一个小型包装器 Java 类。我们使用安全的 HTTPS 连接。通常,SSL 只允许受信任的证书。我希望包装类的用户能够将 X509Certificate 对象作为包装构造函数的输入。该证书通常是不安全的,但这就是重点 - 我们希望在内部进行测试而不使用安全证书。

public ServicesWrapper(String serviceURL, X509Certificate additionalCertificate)
{
//Add the certificate to the trustmanager of the sslcontext.
}

我多次看到“信任所有证书”TrustManager hack (like here),但这不是我需要的。我真的很想指定给定的(不受信任的)证书现在应该被认为是受信任的。其他不受信任的证书不好。所以我实现了自己的 TrustManager,但我卡在了 checkTrusted 步骤:

public class NaiveTrustManager implements X509TrustManager
{
//The extra certificate that should be trusted.
private X509Certificate additionalCertificate;

//Constructor
public NaiveTrustManager(X509Certificate _additionalCertificate)
{
additionalCertificate = _additionalCertificate;
}

...other methods here...


public void checkServerTrusted(java.security.cert.X509Certificate[] certificateChain, String authType)
{
for(X509Certificate c : certificateChain)
{
**?????????????????**
pseudocode:
if (c is equal to additionalcertificate)
stop execution, we trust it!
if (nothing matches)
throw certificatEexception
}
}
}

根据证书链检查证书的正确语法是什么?我检查了一些属性,但证书似乎没有名称或 ID 或任何可匹配相等性或比较它们的内容。

最佳答案

不要为此编写代码。只需将自签名证书添加到您的测试环境信任库。

关于java - TrustManager 只允许一个特定的不受信任的证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21761007/

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