java http 双向认证_HttpsURLConnection使用,并实现双向认证

添加信任所有服务端证书也可在方法中控制

package something;

import java.security.cert.CertificateException;

import java.security.cert.X509Certificate;

import javax.net.ssl.X509TrustManager;

/**

* 信任所有证书

* @author hp

*

*/

public class AllTrustManager implements X509TrustManager {

@Override

public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {

// TODO Auto-generated method stub

}

@Override

public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {

// TODO Auto-generated method stub

}

@Override

public X509Certificate[] getAcceptedIssuers() {

// TODO Auto-generated method stub

return null;

}

}

使用HttpsURLConnection发送POST请求(默认443端口)

package something;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.PrintWriter;

import java.net.URL;

import java.security.KeyManagementException;

import java.security.KeyStore;

import java.security.KeyStoreException;

import java.security.NoSuchAlgorithmException;

import java.security.SecureRandom;

import java.security.UnrecoverableKeyException;

import java.security.cert.CertificateException;

import javax.net.ssl.HttpsURLConnection;

import javax.net.ssl.KeyManagerFactory;

import javax.net.ssl.SSLContext;

import javax.net.ssl.SSLSocketFactory;

import javax.net.ssl.TrustManager;

public class HttpsRequest {

//测试url

private final static String URL_TEST="https://www.baidu.com";

//客户端证书路径

private final static String PATH="c://xxxx";

//证书密码

private final static String psw="123456";

/**

* 发送POST请求

* @param agrs

* @return

* @throws IOException

* @throws KeyManagementException

* @throws KeyStoreException

* @throws NoSuchAlgorithmException

* @throws CertificateException

* @throws UnrecoverableKeyException

*/

public String doPost(String agrs) throws IOException, KeyManagementException, KeyStoreException, NoSuchAlgorithmException, CertificateException, UnrecoverableKeyException{

//导入客户端证书

KeyStore ks=KeyStore.getInstance("pkcs12");

FileInputStream instream = new FileInputStream(new File(PATH));

ks.load(instream, psw.toCharArray());

KeyManagerFactory kmf=KeyManagerFactory.getInstance("SunX509");

kmf.init(ks, psw.toCharArray());

//添加信任证书

TrustManager[] tm={new AllTrustManager()};//AllTrustManager()为信任所有证书

SSLContext ctx=SSLContext.getInstance("SSL");//创建ssl上下文

//初始化 ;参数1为null,则不上传客户端证书(通常情况都是如此);

ctx.init(kmf.getKeyManagers(), tm, new SecureRandom());

//ctx.init(kmf.getKeyManagers(), null, new SecureRandom());//验证系统默认证书

//ctx.init(kmf.getKeyManagers(), TrustManager[] tm, new SecureRandom());//导出服务端证书,然后按照keymanager一样实现trustmanager

SSLSocketFactory sf=ctx.getSocketFactory();

URL _url=new URL(URL_TEST);

HttpsURLConnection conn=(HttpsURLConnection) _url.openConnection();

conn.setRequestMethod("POST");//设定请求方法

conn.setConnectTimeout(20000);

conn.setReadTimeout(20000);

conn.setDoInput(true);//打开输入流

conn.setDoOutput(true);//打开输出流写入写出参数必需

conn.setSSLSocketFactory(sf);//添加ssl参数

//输出参数

PrintWriter pw=new PrintWriter(conn.getOutputStream());

pw.write(agrs);

pw.flush();

//获取输入流

BufferedReader br=new BufferedReader(new InputStreamReader(conn.getInputStream()));

StringBuffer result=new StringBuffer();

String s=br.readLine();

while(s!=null){

result.append(s);

s=br.readLine();

}

pw.close();

br.close();

return result.toString();

}

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值