日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

四大微博OAuth认证

發布時間:2025/1/21 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 四大微博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上加上下面的代碼就可以解決了

public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { handler.proceed(); }

沒想到又出現這個問題,于是一翻研究,在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认证的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。