java - (Java) 未发送所需的 SSL 证书(在 curl 调用中使用时工作正常)
问题描述
因此,我正在尝试通过在 Java 程序中使用客户端证书来测试与我公司 Web 服务器的连接(使用 2 路 SSL)。我尝试在 curl 调用中使用相同的证书(分离的证书和密钥)并设法获得所需的响应。但是当我尝试在我的 Java 程序中使用它(组合成 pkcs12 格式)时,它会给出 400 响应,说没有发送所需的 SSL 证书。为什么会这样?
public static void main(String[] args) {
System.out.println("Taufiq's mutual SSL-authentication test");
org.apache.log4j.BasicConfigurator.configure();
Logger.getRootLogger().setLevel(Level.INFO);
try {
final String CERT_ALIAS = "something", CERT_PASSWORD = "something";
KeyStore identityKeyStore = KeyStore.getInstance("pkcs12");
FileInputStream identityKeyStoreFile = new FileInputStream(new File("src/Cert.p12"));
identityKeyStore.load(identityKeyStoreFile, CERT_PASSWORD.toCharArray());
KeyStore trustKeyStore = KeyStore.getInstance("jks");
FileInputStream trustKeyStoreFile = new FileInputStream(new File("src/truststore.jks"));
trustKeyStore.load(trustKeyStoreFile, CERT_PASSWORD.toCharArray());
SSLContext sslContext = SSLContexts.custom()
// load identity keystore
.loadKeyMaterial(identityKeyStore, CERT_PASSWORD.toCharArray(), new PrivateKeyStrategy() {
@Override
public String chooseAlias(Map<String, PrivateKeyDetails> aliases, Socket socket) {
return CERT_ALIAS;
}
})
// load trust keystore
.loadTrustMaterial(trustKeyStore, null)
.build();
SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContext,
new String[]{"TLSv1.2", "TLSv1.1"},
null,
SSLConnectionSocketFactory.getDefaultHostnameVerifier());
CloseableHttpClient client = HttpClients.custom()
.setSSLSocketFactory(sslConnectionSocketFactory)
.build();
// Call a SSL-endpoint
callEndPoint (client);
} catch (Exception ex) {
System.out.println("Boom, we failed: " + ex);
ex.printStackTrace();
}
}
private static void callEndPoint(CloseableHttpClient aHTTPClient) {
try {
String ServerUrl = "My Company URL";
System.out.println("Calling URL: " + ServerUrl);
HttpPost post = new HttpPost(ServerUrl);
post.setHeader("Content-type", "application/json");
System.out.println("**POST** request Url: " + post.getURI());
HttpResponse response = aHTTPClient.execute(post);
int responseCode = response.getStatusLine().getStatusCode();
System.out.println("Response Code: " + responseCode);
System.out.println("Content:-\n");
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line;
while ((line = rd.readLine()) != null) {
System.out.println(line);
}
} catch (Exception ex) {
System.out.println("Boom, we failed: " + ex);
ex.printStackTrace();
}
}
示例 Curl 调用: curl -v --key key.pem --pass ****** --cert cert.pem MyCompanyURL
解决方案
事实证明,当我使用核心 jdk jsse 类时,程序运行良好。我使用了上述程序,因为我是从互联网上复制其中一个示例。
推荐阅读
- mysql - 如何回答使用多个表而不连接的问题?SQL
- android - Xamarin Android 应用程序在使用 r8 收缩编译时崩溃
- pandas - 浮动到数据框
- python - 使用 scrapy 限制检索到的项目数
- php - php连续显示两条记录然后重复,从中间开始
- c# - 比较两个数字的相等性时出现意外错误
- c++ - 如何从类成员函数返回指针,例如 size_t * class :: function(); 并使用类析构函数 ~size_t*class::function();
- java - NetBeans 安装程序在“正在配置安装程序...”后以白屏关闭
- wso2 - WSO2 Identity Server 5.7.0 - 无法在主域下创建新角色
- python - Keras 二元分类器教程示例仅提供 50% 的验证准确度