Dav*_*e C 7 java pem apache-httpclient-4.x
I'd like to programmatically access a site that requires Client certificates, which I have in PEM files. In this application I don't want to add them to my keystore, use keytool, or openssl if I can avoid doing so. I need to deal with them directly in code.
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet("https://my.secure.site.com/url");
// TODO: Specify ca.pem and client.pem here?
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
if (entity != null) {
entity.consumeContent();
}
httpclient.getConnectionManager().shutdown();
Run Code Online (Sandbox Code Playgroud)
How would I 'send' the certificate with the request?
最简单的可能是使用.p12格式(虽然其他工作也很好 - 只需要注意base64块之外的额外行)并添加如下内容:
// systems I trust
System.setProperty("javax.net.ssl.trustStore", "foo");
System.setProperty("javax.net.ssl.trustStorePassword", "changeit");
// my credentials
System.setProperty("javax.net.ssl.keyStoreType", "PKCS12");
System.setProperty("javax.net.ssl.keyStore", "cert.p12");
System.setProperty("javax.net.ssl.keyStorePassword", "changeit");
Run Code Online (Sandbox Code Playgroud)
或者 - 使用类似的东西
KeyStore ks = KeyStore.getInstance( "pkcs12" );
ks.load( new FileInputStream( ....), "mypassword".toCharArray() );
KeyStore jks = KeyStore.getInstance( "JKS" );
ks.load(...
Run Code Online (Sandbox Code Playgroud)
反而在上面创建.而不是依赖系统属性 - 使用像:
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
kmf.init(aboveKeyStore, "changeme".toCharArray());
sslContext = SSLContext.getInstance("SSLv3");
sslContext.init(kmf.getKeyManagers(), null, null);
Run Code Online (Sandbox Code Playgroud)
这使它与密钥库分开.
DW.
| 归档时间: |
|
| 查看次数: |
7167 次 |
| 最近记录: |