四大微博OAuth认证
生活随笔
收集整理的這篇文章主要介紹了
四大微博OAuth认证
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
據說這兩天騰訊的服務器出了問題,認證的時候報這樣的錯:
oauth.signpost.exception.OAuthCommunicationException: Communication with the service provider failed: Not trusted server certificate Caused by: javax.net.ssl.SSLPeerUnverifiedException: No peer certificate
oauth.signpost.exception.OAuthCommunicationException: Communication with the service provider failed: Nopeer certificate?
這是因為Https認證被截獲導致,Client認為安全失效,很久之前就出現了這個問題了,那時候在WebView上加上下面的代碼就可以解決了
沒想到又出現這個問題,于是一翻研究,在stackoverflow.com上找到答案,寫了一個自定義類繼承SSLSocketFactory
public class SSLSocketFactoryEx extends SSLSocketFactory { SSLContext sslContext = SSLContext.getInstance("TLS"); public SSLSocketFactoryEx(KeyStore truststore) throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException { super(truststore); TrustManager tm = new X509TrustManager() { public java.security.cert.X509Certificate[] getAcceptedIssuers() { return null; } @Override public void checkClientTrusted( java.security.cert.X509Certificate[] chain, String authType) throws java.security.cert.CertificateException { } @Override public void checkServerTrusted( java.security.cert.X509Certificate[] chain, String authType) throws java.security.cert.CertificateException {} }; sslContext.init(null, new TrustManager[] { tm }, null); } @Override public Socket createSocket(Socket socket, String host, int port,boolean autoClose) throws IOException, UnknownHostException { return sslContext.getSocketFactory().createSocket(socket, host, port,autoClose); } @Override public Socket createSocket() throws IOException { return sslContext.getSocketFactory().createSocket(); } } 調用方法,只要用認證返回的HttpCilent即可。代碼: Java 代碼復制內容到剪貼板public HttpClient getNewHttpClient() { try { KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); trustStore.load(null, null); SSLSocketFactory sf = new SSLSocketFactoryEx(trustStore); sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); HttpParams params = new BasicHttpParams(); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset(params, HTTP.UTF_8); SchemeRegistry registry = new SchemeRegistry(); registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); registry.register(new Scheme("https", sf, 443)); ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry); return new DefaultHttpClient(ccm, params); } catch (Exception e) { return new DefaultHttpClient(); } }這樣就解決了問題,有網友說把騰訊認證的地址https去掉改成http,那是不可取的做法。
我已經把代碼集成到signpost中,如果有需要的同學可自行下載,有不明白或者不好的地方給我評論留言。
源代碼下載:http://06peng.com/read.php/60.htm
?
?
?
總結
以上是生活随笔為你收集整理的四大微博OAuth认证的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: windows XP cmd命令集
- 下一篇: Silverlight在调用wcf时传输