闂傚倸鍊搁崐鎼佸磹閹间礁纾圭€瑰嫭鍣磋ぐ鎺戠倞鐟滃繘寮抽敃鍌涚厱妞ゎ厽鍨垫禍婵嬫煕濞嗗繒绠抽柍褜鍓欑粻宥夊磿闁单鍥敍濠婂懐鐒奸柣搴秵閸嬩焦绂嶅⿰鍫熺厸闁告劑鍔岄埀顒傛暬楠炲繘鍩勯崘褏绠氬銈嗗姂閸婃洟寮搁幋婢㈠酣宕惰闊剟鏌熼鐣屾噰妞ゃ垺妫冨畷鐔煎Ω閵夈倕顥氶柣鐔哥矊缁绘帡宕氶幒鎾剁瘈婵﹩鍓涢娲⒑閹稿孩鐓ラ柛妯荤矒瀹曟垿骞樼紒妯绘珖闂侀€炲苯澧撮柟顕€绠栭弫鎾绘偐閼碱剦鍚嬫俊鐐€栧Λ浣规叏閵堝鏁婇柡鍥ュ灪閳锋帒霉閿濆懏鎲哥紒澶屽劋娣囧﹪顢曢姀鈥充淮闂佽鍠氶崑銈夊极閸愵喖纾兼慨妯块哺閻擄綁姊绘担鍝ョШ婵☆偄娼¢幃鐐烘晝閸屾稑浜楅梺闈涱槴閺呮粓鎮¢悢鍏肩厸闁搞儮鏅涙禒婊堟煃瑜滈崜娆忥耿闁秴鐒垫い鎺嶈兌閸熸煡鏌熼崙銈嗗     婵犵數濮烽弫鍛婃叏閻戣棄鏋侀柛娑橈攻閸欏繘鏌i姀鐘差棌闁轰礁锕弻鈥愁吋鎼粹€崇闂傚倸鍋嗛崹閬嶅Φ閸曨垰鍐€闁靛ě鍛帎缂傚倷娴囩紙浼村磹濠靛钃熼柨婵嗩槹閺呮煡鏌涘☉鍗炴灍闁圭兘浜堕幃妤冩喆閸曨剛顦ㄩ梺缁樻惈缁绘繂顕f繝姘亜閻炴稈鈧厖澹曞┑鐐村灦椤忎即骞嬮敃鈧壕鍧楁煛閸モ晛鏋旂紒鐘荤畺閺岀喓鈧稒岣块幊鍐煟韫囧﹥娅婇柡宀嬬秮楠炴ḿ鈧潧鎲¢崚娑㈡⒑閸濆嫭婀伴柣鈺婂灦閻涱噣宕堕鈧粈鍌炴煕濠靛嫬鍔滈柡鍛偢濮婄粯鎷呴搹鐟扮闂佺懓鎲¢幃鍌炪€侀弽銊ョ窞濠电姴瀛╃紞搴♀攽閻愬弶鈻曞ù婊勭矒楠炴垿鎳滈悙閫涚盎闂佽宕樺▔娑欑濠婂啠鏀介柍鈺佸暞閵囨繃鎱ㄦ繝鍛仩闁瑰弶鎸冲畷鐔碱敇閻旂ǹ鏅搁梺璇叉唉椤煤閻斾赴缂氱憸鏃堛€佸璺何ㄩ柨鏂垮⒔閻﹀牓姊洪幖鐐插姌闁告柨绉撮埢宥夊閵堝棌鎷洪柣鐘充航閸斿苯鈻嶉幇鐗堢厵闁告垯鍊栫€氾拷

45fan.com - 路饭网

搜索: 您的位置主页 > 网络频道 > 阅读资讯:当SSL碰到证书不合法应该怎么办?

当SSL碰到证书不合法应该怎么办?

2016-08-27 18:44:46 来源:www.45fan.com 【

当SSL碰到证书不合法应该怎么办?

当你用HttpsURLConnection来查看https网页内容而对方证书无效时候,回出现Exception,怎么办。

1.自己有一TrustManager 类

import com.sun.net.ssl.SSLContext;

import com.sun.net.ssl.TrustManager;

import com.sun.net.ssl.X509TrustManager;

import com.sun.net.ssl.TrustManagerFactory;

publicclass MyTrustManager implements X509TrustManager

{

private KeyStore keyStore;

private String keyStorePath;

private char[] keyStorePassword;

public MyTrustManager(){}

// MyTrustManager constructor. Save off keyStore object along with

// the path to the keystore (keyStorePath) and it's password

// (keyStorePassword).

public MyTrustManager(KeyStore keyStore,

String keyStorePath,

char[] keyStorePassword)

{

this.keyStore = keyStore;

this.keyStorePath = keyStorePath;

this.keyStorePassword = keyStorePassword;

}

// isClientTrusted checks to see if the chain is in the keyStore object.

// This is done with a call to isChainTrusted.

public boolean isClientTrusted(X509Certificate[] chain)

{

return isChainTrusted(chain);

}

// isServerTrusted checks to see if the chain is in the keyStore object.

// This is done with a call to isChainTrusted. If not it queries the

// user to see if the chain should be trusted and stored into the

// keyStore object. The keyStore is then saved in the file whose path

// keyStorePath

public boolean isServerTrusted(X509Certificate[] chain)

{

return true;

}

// getAcceptedIssuers retrieves all of the certificates in the keyStore

// and returns them in an X509Certificate array.

public X509Certificate[] getAcceptedIssuers()

{

X509Certificate[] X509Certs = null;

try

{

// See how many certificates are in the keystore.

int numberOfEntry = keyStore.size();

// If there are any certificates in the keystore.

if(numberOfEntry > 0)

{

// Create an array of X509Certificates

X509Certs = new X509Certificate[numberOfEntry];

// Get all of the certificate alias out of the keystore.

Enumeration aliases = keyStore.aliases();

// Retrieve all of the certificates out of the keystore

// via the alias name.

int i = 0;

while (aliases.hasMoreElements())

{

X509Certs[i] =

(X509Certificate)keyStore.

getCertificate((String)aliases.nextElement());

i++;

}

}

}

catch( Exception e )

{

System.out.println( "getAcceptedIssuers Exception: "

+ e.toString() );

X509Certs = null;

}

return X509Certs;

}

// isChainTrusted searches the keyStore for any certificate in the

// certificate chain.

private boolean isChainTrusted(X509Certificate[] chain)

{

return true;

}

}

2.注册你的 TrustManager类

X509TrustManager xtm = new MyTrustManager();

TrustManager mytm[] = {

xtm};

SSLContext ctx = SSLContext.getInstance("SSL");

ctx.init(null, mytm, null);

SSLSocketFactory factory = ctx.getSocketFactory();

//注册TrustManager类(factory)

HttpsURLConnection huc = (HttpsURLConnection)

(new URL(“http://www.aaa.com”).openConnection();

//huc.setHostnameVerifier(new com.smartghost.ssl.MyHostnameVerifier());

huc.setSSLSocketFactory(factory);

...... //错误不再

 

 

本文地址:http://www.45fan.com/a/question/68544.html
Tags: 过期 证书 ssl
编辑:路饭网
关于我们 | 联系我们 | 友情链接 | 网站地图 | Sitemap | App | 返回顶部