关闭

联合登录,请求令牌时对方返回空以及SSLPeerUnverifiedException的解决办法

标签: java联合登录
1401人阅读 评论(0) 收藏 举报
分类:

最近做了一个联合登录,在本地测试通过,但是一上线就报错。后来通过日志信息观察到,去请求令牌时,对方返回了空。现在把修改前后的请求方法写在下面:

之前,请求不到令牌的写法:

private static JSONObject getJsonFromUrl(String urlStr) {
		// org.apache.http.client.HttpClient
		HttpClient httpClient = new DefaultHttpClient();
		// org.apache.http.client.methods.HttpPost
		HttpPost httpPost = new HttpPost(urlStr);
		JSONObject json = null;
		try {			
			HttpResponse response = httpClient.execute(httpPost);
			HttpEntity entity = response.getEntity();
			// 使用InputStream从数据流中读取数据
			InputStream is = entity.getContent();
			byte[] bytes = new byte[256];
			StringBuffer sb = new StringBuffer();
			while (is.read(bytes) > 0) {				
				sb.append(new String(bytes, HTTP.UTF_8));
				bytes = new byte[256];
			}
			json = JSONObject.fromObject(sb.toString());
		} catch (Exception e) {
			LOG.error("http client execute error:" + e.getMessage(), e);
		}
		return json;
	}	
可以请求到令牌的写法:

private static JSONObject getJsonFromUrl(String urlStr) {
		// org.apache.http.client.HttpClient
		HttpClient httpClient = new DefaultHttpClient();
		// org.apache.http.client.methods.HttpPost
		HttpPost httpPost = new HttpPost(urlStr);
		String urlResult = "";
		try {
			// org.apache.http.util.EntityUtils	;Static helpers for dealing with HttpEntitys.	;Since:4.0
			// apache提供的方法 EntityUtils.toString : Get the entity content as a String, using the provided default character set if none is found in the entity. If defaultCharset is null, the default "ISO-8859-1" is used.
			urlResult = EntityUtils.toString(httpClient.execute(httpPost).getEntity(), "UTF-8");
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println("urlStr from yihaodian is : " + urlResult);

		JSONObject json = JSONObject.fromObject(urlResult);
		System.out.println("urlStr to JSONObject , JSONObject is : " + json);

		return json;
	}

具体原因,还在找

然后,又出现了如下异常:

javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated
	at com.sun.net.ssl.internal.ssl.SSLSessionImpl.getPeerCertificates(SSLSessionImpl.java:352)
	at org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:128)
	at org.apache.http.conn.ssl.SSLSocketFactory.connectSocket(SSLSocketFactory.java:397)
	at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:148)
	at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:149)
	at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:121)
	at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:573)
	at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:425)
	at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:820)
	at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:754)
	at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:732)
好在有网,查询后,得到了这样的解决方案:
/**
	* 获取可信任https链接,以避免不受信任证书出现peer not authenticated异常
	*
	* @param base
	* @return
	*/
	public static HttpClient wrapClient(HttpClient base) {
		try {
			SSLContext ctx = SSLContext.getInstance("TLS");
			X509TrustManager tm = new X509TrustManager() {
				public void checkClientTrusted(X509Certificate[] xcs, String string) {
				}

				public void checkServerTrusted(X509Certificate[] xcs, String string) {
				}

				public X509Certificate[] getAcceptedIssuers() {
					return null;
				}
			};
			ctx.init(null, new TrustManager[] { tm }, null);
			SSLSocketFactory ssf = new SSLSocketFactory(ctx);
			ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
			ClientConnectionManager ccm = base.getConnectionManager();
			SchemeRegistry sr = ccm.getSchemeRegistry();
			sr.register(new Scheme("https", ssf, 443));
			System.out.println("==[" + DateUtil.DateTime() + "]成功忽略证书有效性");
			return new DefaultHttpClient(ccm, base.getParams());
		} catch (Exception ex) {
			System.out.println("==[" + DateUtil.DateTime() + "]忽略证书有效性出现异常:" + ex.getMessage());
			ex.printStackTrace();
			return null;
		}
	}
然后,HttpClient对象,需要处理一下:

HttpClient httpClient = new DefaultHttpClient();
// 获取可信任https链接,以避免不受信任证书出现peer not authenticated异常
httpClient = wrapClient(httpClient);

说明一下需要导入的包,因为可能有的类重名,会造成困惑。可能并不是所有的包都有用,Eclipse划黄线的,删掉就好:

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import java.security.cert.X509Certificate;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;

致敬:http://www.iteye.com/topic/1136303

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:298208次
    • 积分:4226
    • 等级:
    • 排名:第7225名
    • 原创:111篇
    • 转载:52篇
    • 译文:3篇
    • 评论:68条
    文章分类
    最新评论