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

原创 2015年07月09日 11:53:24

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

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

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

NO.91 SSLPeerUnverifiedException 问题之解决(附HttpClientUtils升级版)

SSLPeerUnverifiedException问题解决。 附HttpClinet工具 1. 基于Apache HttpClient4 2. 添加获取图片的方法 3. 支持https 4. 支持会...
  • amosryan
  • amosryan
  • 2014年02月28日 15:12
  • 10339

OkHttp调用HTTPS遇到的问题之:SSLPeerUnverifiedException

关于OkHttp OkHttp学习 Https学习 最近在用OkHttp调https接口的时候遇到一个问题 javax.net.ssl.SSLPeerUnverifiedException: pe...

Android okHttp:Https之SSL - Hostname was not verified

踩坑篇项目服务端传输协议换成了https,客户端打接口一直报:Hostname 'url' was not verified...

关于ThinkPHP下表单令牌错误的相关解决办法

今天在用ThinkPHP做程序的时候,以前用create创建数据的时候,出现了错误提示“表单令牌错误”,然后各种百度各种谷歌,得到的网上解答给出了以下的建议 1、清缓存: 用了,我把所有的Cache...

jQuery在异步请求数据返回后,调用$("selector").html(data.content);之后因为一些特殊字符或者',"不能显示内容的问题解决办法

这个曾经困扰我了大半天。我出的状况是: 首先说明,我返回的data.content内容是一个页面的html代码。 1.数据能通过jquery中的ajax异步正常返回,alert(data.co...
  • gayayzy
  • gayayzy
  • 2012年07月31日 15:46
  • 2636

MSN无法登录的15种解决办法

  • 2013年05月27日 17:24
  • 38KB
  • 下载

oracle10gem登录报错解决办法

  • 2014年12月28日 11:52
  • 157B
  • 下载

bootstrap 标签页与fullcalendar 日历插件联合使用时出现隐藏页不显示日历的解决办法

昨天做项目的时候,前端需要用到日历,使用了日历插件fullcalendar ,功能很强大。同时前台使用的bootstrap 的标签页形式显示多个日历,但是当整合到一起后,发现只有默认显示的第一页的日历...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:联合登录,请求令牌时对方返回空以及SSLPeerUnverifiedException的解决办法
举报原因:
原因补充:

(最多只允许输入30个字)