https双向认证java

**欢迎关注公众号**
**微信扫一扫**

https 双向认证demo


import javax.net.ssl.*;
import java.io.*;
import java.net.URL;
import java.security.KeyStore;

/**
 * 双向SSL 认证
 */
public class HttpsClient {

    private final static String charset = "UTF-8";

    /**
     * 客户端证书设置
     */
    protected String clientCertPwd;// 客户端证书密码
    protected String clientCertPath;
    protected String clientKeyType = "JKS";
    /**
     * 服务端证书设置
     */
    protected String trustCertPath;
    protected String truestCertPwd;
    protected String truestKeyType = "JKS";

    private SSLContext sslContext;

/**
* post方法
*/
    public Object post(Object request, String requestAddr) {
        String requestData = (String) request;
        StringBuffer sb = null;
        HttpsURLConnection urlCon = null;

        OutputStream os = null;
        InputStream fis = null;
        BufferedInputStream bis = null;

        InputStream is = null;
        BufferedReader br = null;
        try {
            if (sslContext == null) {
                sslContext = SSLContext.getInstance("SSL");
                KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
                TrustManagerFactory tmf = TrustManagerFactory
                        .getInstance("SunX509");

                KeyStore keyStore = KeyStore.getInstance(clientKeyType);
                keyStore.load(new FileInputStream(clientCertPath),
                        clientCertPwd.toCharArray());
                kmf.init(keyStore, clientCertPwd.toCharArray());

                KeyStore trustKeyStore = KeyStore.getInstance(truestKeyType);
                trustKeyStore.load(new FileInputStream(trustCertPath),
                        truestCertPwd.toCharArray());
                tmf.init(trustKeyStore);
                sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
                HostnameVerifier hostnameVerifier = new HostnameVerifier() {
                    @Override
                    public boolean verify(String arg0, SSLSession arg1) {
                        return true;
                    }
                };
                HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);
            }

            URL url = new URL(requestAddr);
            urlCon = (HttpsURLConnection) url.openConnection();
            urlCon.setDoOutput(true);
            urlCon.setDoInput(true);
            urlCon.setRequestMethod("POST");
            urlCon.setRequestProperty("Content-type", "text/xml;charset=" + charset);
            urlCon.setSSLSocketFactory(sslContext.getSocketFactory());

            os = urlCon.getOutputStream();
            fis = new ByteArrayInputStream(requestData.getBytes(charset));
            bis = new BufferedInputStream(fis);
            byte[] bytes = new byte[1024];
            int len = -1;
            while ((len = bis.read(bytes)) != -1) {
                os.write(bytes, 0, len);
            }
            closeOutputStream(os, fis, bis);

            is = urlCon.getInputStream();
            br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            sb = new StringBuffer();
            String line;
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
            return sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                closeOutputStream(os, fis, bis);
                closeInputStream(is, br);
                closeUrlCon(urlCon);
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return null;
    }

    /**
     * @param urlCon
     */
    private static void closeUrlCon(HttpsURLConnection urlCon) {
        if (urlCon != null) {
            urlCon.disconnect();
        }
    }

    /**
     * @param is
     * @param br
     * @throws IOException
     */
    private static void closeInputStream(InputStream is, BufferedReader br)
            throws IOException {
        if (br != null) {
            br.close();
        }
        if (is != null) {
            is.close();
        }
    }

    /**
     * @param os
     * @param fis
     * @param bis
     * @throws IOException
     */
    private static void closeOutputStream(OutputStream os, InputStream fis,
                                          BufferedInputStream bis) throws IOException {
        if (os != null) {
            os.flush();
        }
        if (bis != null) {
            bis.close();
        }
        if (fis != null) {
            fis.close();
        }
        if (os != null) {
            os.close();
        }
    }

    public String getTruestCertPwd() {
        return truestCertPwd;
    }

    public void setTruestCertPwd(String truestCertPwd) {
        this.truestCertPwd = truestCertPwd;
    }

    public String getClientCertPwd() {
        return clientCertPwd;
    }

    public void setClientCertPwd(String clientCertPwd) {
        this.clientCertPwd = clientCertPwd;
    }

    public String getClientCertPath() {
        return clientCertPath;
    }

    public void setClientCertPath(String clientCertPath) {
        this.clientCertPath = clientCertPath;
    }

    public String getTrustCertPath() {
        return trustCertPath;
    }

    public void setTrustCertPath(String trustCertPath) {
        this.trustCertPath = trustCertPath;
    }

    public String getTruestKeyType() {
        return truestKeyType;
    }

    public void setTruestKeyType(String truestKeyType) {
        this.truestKeyType = truestKeyType;
    }

    public String getClientKeyType() {
        return clientKeyType;
    }

    public void setClientKeyType(String clientKeyType) {
        this.clientKeyType = clientKeyType;
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程猿薇茑

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值