成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專(zhuān)欄INFORMATION COLUMN

HttpsURLConnection使用,并實(shí)現(xiàn)雙向認(rèn)證

layman / 2888人閱讀

摘要:添加信任所有服務(wù)端證書(shū)也可在方法中控制信任所有證書(shū)使用發(fā)送請(qǐng)求默認(rèn)端口測(cè)試客戶端證書(shū)路徑證書(shū)密碼發(fā)送請(qǐng)求導(dǎo)入客戶端證書(shū)添加信任證書(shū)為信任所有證書(shū)創(chuàng)建上下文初始化參數(shù)為,則不上傳客戶端證書(shū)通常情況都是如此驗(yàn)證系統(tǒng)默認(rèn)證書(shū)導(dǎo)出服務(wù)端證書(shū),

添加信任所有服務(wù)端證書(shū)也可在方法中控制

package something;

import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.ssl.X509TrustManager;
/**
 * 信任所有證書(shū)
 * @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發(fā)送POST請(qǐng)求(默認(rèn)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 {
    //測(cè)試url
    private final static String URL_TEST="https://www.baidu.com";
    //客戶端證書(shū)路徑
    private final static String PATH="c://xxxx";
    //證書(shū)密碼
    private final static String psw="123456";
    /**
     * 發(fā)送POST請(qǐng)求
     * @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{
        //導(dǎo)入客戶端證書(shū)
        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());
        //添加信任證書(shū)
        TrustManager[] tm={new AllTrustManager()};//AllTrustManager()為信任所有證書(shū)
        
        SSLContext ctx=SSLContext.getInstance("SSL");//創(chuàng)建ssl上下文
        //初始化 ;參數(shù)1為null,則不上傳客戶端證書(shū)(通常情況都是如此);
        ctx.init(kmf.getKeyManagers(), tm, new SecureRandom());
        //ctx.init(kmf.getKeyManagers(), null, new SecureRandom());//驗(yàn)證系統(tǒng)默認(rèn)證書(shū)
        //ctx.init(kmf.getKeyManagers(), TrustManager[] tm, new SecureRandom());//導(dǎo)出服務(wù)端證書(shū),然后按照keymanager一樣實(shí)現(xiàn)trustmanager
        SSLSocketFactory sf=ctx.getSocketFactory();
        
        URL _url=new URL(URL_TEST);
        HttpsURLConnection conn=(HttpsURLConnection) _url.openConnection();
        conn.setRequestMethod("POST");//設(shè)定請(qǐng)求方法
        conn.setConnectTimeout(20000);
        conn.setReadTimeout(20000);
        conn.setDoInput(true);//打開(kāi)輸入流
        conn.setDoOutput(true);//打開(kāi)輸出流寫(xiě)入寫(xiě)出參數(shù)必需
        conn.setSSLSocketFactory(sf);//添加ssl參數(shù)
        //輸出參數(shù)
        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();
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/68470.html

相關(guān)文章

  • [轉(zhuǎn)]https配合移動(dòng)端開(kāi)發(fā)

    摘要:而且文章經(jīng)過(guò)統(tǒng)計(jì)發(fā)現(xiàn)的應(yīng)用程序沒(méi)有正確地在通信過(guò)程中進(jìn)行證書(shū)驗(yàn)證。確認(rèn)服務(wù)器端證書(shū)的和代碼中的證書(shū)主體一致。根據(jù)開(kāi)發(fā)安全的應(yīng)用提出的觀點(diǎn),可以避免最終用戶證書(shū)有效期可能比較短的問(wèn)題。 轉(zhuǎn)載請(qǐng)注明出處 http://www.paraller.com 原文排版地址 點(diǎn)擊獲取更好閱讀體驗(yàn) 轉(zhuǎn)載:http://xhrwang.me/2015/06/06/https-and-android....

    Render 評(píng)論0 收藏0
  • 關(guān)于java訪問(wèn)https資源時(shí),忽略證書(shū)信任問(wèn)題

    摘要:程序在訪問(wèn)資源時(shí),出現(xiàn)報(bào)錯(cuò)這本質(zhì)上,是在訪問(wèn)資源時(shí)的證書(shū)信任問(wèn)題。因此,如果用訪問(wèn)資源,發(fā)現(xiàn)證書(shū)不可信任,則會(huì)報(bào)文章開(kāi)頭說(shuō)到的錯(cuò)誤。 java程序在訪問(wèn)https資源時(shí),出現(xiàn)報(bào)錯(cuò)sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunC...

    songjz 評(píng)論0 收藏0
  • springboot RestTemplate,訪問(wèn)https實(shí)現(xiàn)SSL請(qǐng)求

    一、RestTemplate 簡(jiǎn)介Spring RestTemplate是Spring提供的用于訪問(wèn)Rest服務(wù)的客戶端,RestTemplate 提供了多種便捷訪問(wèn)遠(yuǎn)程Http服務(wù)的方法,能夠大大提高客戶端的編寫(xiě)效率. RestTemplate包含以下幾個(gè)部分: HttpMessageConverter 對(duì)象轉(zhuǎn)換器:將請(qǐng)求對(duì)象轉(zhuǎn)換為具體的數(shù)據(jù)格式輸出,例 入:Jaxb2RootElemen...

    bitkylin 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<