java中实现https双向认证
来源:互联网 发布:java时间戳转换成时间 编辑:程序博客网 时间:2024/05/19 13:27
1、在IE中访问WSDL的URL,弹出“安全警报”窗口,查看证书->详细信息标签页->复制到文件->下一步->下一步->指定文件名,将证书下载保存为.cer文件,例如:test.cer
2、用下载到的证书文件生成信任库文件:
在 keytool -import -file test.cer -storepass 111111 -keystore c:\client.truststore –noprompt
这里生成的client.truststore证书是根据cacert.cer(根证书)证书来生成的。
全部依赖库:
commons-logging-1.1.1.jar
httpclient-4.1.3.jar
httpcore-4.1.4.jar
httpmime-4.1.3.jar(上传文件使用)
import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.InputStream;import java.io.InputStreamReader;import java.security.KeyStore;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.client.HttpClient;import org.apache.http.client.methods.HttpGet;import org.apache.http.conn.scheme.Scheme;import org.apache.http.conn.ssl.SSLSocketFactory;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.util.EntityUtils;public class HttpsClient { private static final String KEY_STORE_TYPE_JKS = "jks"; private static final String KEY_STORE_TYPE_P12 = "PKCS12"; private static final String SCHEME_HTTPS = "https"; private static final int HTTPS_PORT = 443; private static final String HTTPS_URL = "https://192.168.6.120/index.html"; private static final String KEY_STORE_CLIENT_PATH = "F:/client.p12"; private static final String KEY_STORE_TRUST_PATH = "F:/client.truststore"; private static final String KEY_STORE_PASSWORD = "xiaozl"; private static final String KEY_STORE_TRUST_PASSWORD = "111111"; public static void main(String[] args) throws Exception { ssl(); } private static void ssl() throws Exception { HttpClient httpClient = new DefaultHttpClient(); try { KeyStore keyStore = KeyStore.getInstance(KEY_STORE_TYPE_P12); KeyStore trustStore = KeyStore.getInstance(KEY_STORE_TYPE_JKS); InputStream ksIn = new FileInputStream(KEY_STORE_CLIENT_PATH); InputStream tsIn = new FileInputStream(new File(KEY_STORE_TRUST_PATH)); try { keyStore.load(ksIn, KEY_STORE_PASSWORD.toCharArray()); trustStore.load(tsIn, KEY_STORE_TRUST_PASSWORD.toCharArray()); } finally { try { ksIn.close(); } catch (Exception ignore) {} try { tsIn.close(); } catch (Exception ignore) {} } SSLSocketFactory socketFactory = new SSLSocketFactory(keyStore, KEY_STORE_PASSWORD, trustStore); Scheme sch = new Scheme(SCHEME_HTTPS, HTTPS_PORT, socketFactory); httpClient.getConnectionManager().getSchemeRegistry().register(sch); HttpGet httpget = new HttpGet(HTTPS_URL); System.out.println("executing request" + httpget.getRequestLine()); HttpResponse response = httpClient.execute(httpget); HttpEntity entity = response.getEntity(); System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); if (entity != null) { System.out.println("Response content length: " + entity.getContentLength()); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(entity.getContent())); String text; while ((text = bufferedReader.readLine()) != null) { System.out.println(text); } bufferedReader.close(); } EntityUtils.consume(entity); } finally { httpClient.getConnectionManager().shutdown(); } }}
文章来源:
http://www.blogjava.net/icewee/archive/2012/06/05/379983.html
- java中实现https双向认证
- java https双向认证实现
- java实现https双向认证
- C#中实现https的双向认证
- HTTPS 中双向认证
- CXF实现HTTPS双向认证
- Java nginx https 双向认证
- java 通过Httpclient模拟https Post 请求,实现双向认证
- php用curl实现https双向认证
- nginx与ios实现https双向认证
- ASIHTTPRequest实现https双向认证请求
- ASIHTTPRequest实现https双向认证请求
- ASIHTTPRequest实现https双向认证请求
- ASIHTTPRequest实现https双向认证请求
- ASIHTTPRequest实现https双向认证请求
- ASIHTTPRequest实现https双向认证请求
- 基于HTTPS的双向认证实现
- Tomcat实现https双向认证功能
- Android Looper和Handler
- 朋友是一条通往过去的路
- What's IAM
- 策略模式及与简单工厂模式的结合
- C语言改写与for循环等价的循环语句
- java中实现https双向认证
- VIM高效率操作 (转载)
- jquery选择器
- 使用Java测试网络连通性的几种方法
- 做个标记(ios6以后禁止屏幕旋转)
- 如何使用SVM基于Suffix Array的String Kernel核函数
- jquery的get和Post方法默认字符编码
- iOS通过http post上传图片
- oracle的简单操作和要注意的地方