Java HttpsURLConnection 调用其余 Web 服务并以编程方式应用证书
Java HttpsURLConnection call rest web service and apply certificate programmatically
我一直在尝试绑定休息服务以进行付款。他们给了我 p12 格式的证书,并指导我使用 OpenSSL 库将其转换为 pem 格式。现在我有这两个文件了。
key.pem(-----BEGIN ENCRYPTED PRIVATE KEY-----)
cert.pem(-----BEGIN CERTIFICATE-----)
我的目标是使用 HttpsURLConnection 调用此休息服务。据我所知,我需要做以下事情:
KeyStore, SSLContext and then apply into httpsCon.setSSLSocketFactory(context.getSocketFactory());
我一直在寻找不同的解决方案,但找不到可行的解决方案。有人可以提供工作示例吗?
这是为我工作的代码。希望对某人有所帮助
public class Main {
@Autowired
ResourceLoader resourceLoader;
private static void applyCertificateInformation(HttpsURLConnection con, String password) throws IOException, NoSuchAlgorithmException, CertificateException, KeyStoreException, UnrecoverableKeyException, KeyManagementException {
KeyStore clientStore = KeyStore.getInstance("PKCS12");
clientStore.load(resourceLoader.getResource("my-cert.p12").getInputStream(), password.toCharArray());
KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
kmf.init(clientStore, password.toCharArray());
KeyManager[] kms = kmf.getKeyManagers();
TrustManager[] tms = new TrustManager[]{
new X509TrustManager() {
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
@Override
public void checkClientTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
}
@Override
public void checkServerTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
}
}
};
SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
sslContext.init(kms, tms, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
con.setSSLSocketFactory(sslContext.getSocketFactory());
}
}
我一直在尝试绑定休息服务以进行付款。他们给了我 p12 格式的证书,并指导我使用 OpenSSL 库将其转换为 pem 格式。现在我有这两个文件了。
key.pem(-----BEGIN ENCRYPTED PRIVATE KEY-----)
cert.pem(-----BEGIN CERTIFICATE-----)
我的目标是使用 HttpsURLConnection 调用此休息服务。据我所知,我需要做以下事情:
KeyStore, SSLContext and then apply into httpsCon.setSSLSocketFactory(context.getSocketFactory());
我一直在寻找不同的解决方案,但找不到可行的解决方案。有人可以提供工作示例吗?
这是为我工作的代码。希望对某人有所帮助
public class Main {
@Autowired
ResourceLoader resourceLoader;
private static void applyCertificateInformation(HttpsURLConnection con, String password) throws IOException, NoSuchAlgorithmException, CertificateException, KeyStoreException, UnrecoverableKeyException, KeyManagementException {
KeyStore clientStore = KeyStore.getInstance("PKCS12");
clientStore.load(resourceLoader.getResource("my-cert.p12").getInputStream(), password.toCharArray());
KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
kmf.init(clientStore, password.toCharArray());
KeyManager[] kms = kmf.getKeyManagers();
TrustManager[] tms = new TrustManager[]{
new X509TrustManager() {
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
@Override
public void checkClientTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
}
@Override
public void checkServerTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
}
}
};
SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
sslContext.init(kms, tms, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
con.setSSLSocketFactory(sslContext.getSocketFactory());
}
}