四大微博OAuth认证

四大微博OAuth认证  

作品 ,  评论(32) ,  引用(0) ,  阅读(2581)    大 |  中 |  小   

据说这两天腾讯的服务器出了问题,认证的时候报这样的错:

Java 代码 复制内容到剪贴板
        
  1. oauth.signpost.exception.OAuthCommunicationException: Communication with the service provider failed: Not trusted server certificate      
  2. Caused by: javax.net.ssl.SSLPeerUnverifiedException: No peer certificate      
  3. oauth.signpost.exception.OAuthCommunicationException: Communication with the service provider failed: Nopeer certificate  

这是因为Https认证被截获导致,Client认为安全失效,很久之前就出现了这个问题了,那时候在WebView上加上下面的代码就可以解决了

Java 代码 复制内容到剪贴板
        
  1. public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {        
  2.      handler.proceed();      
  3. }    

没想到又出现这个问题,于是一翻研究,在stackoverflow.com上找到答案,写了一个自定义类继承SSLSocketFactory,

Java 代码 复制内容到剪贴板
        
  1. public class SSLSocketFactoryEx extends SSLSocketFactory {      
  2.       
  3.     SSLContext sslContext = SSLContext.getInstance("TLS");      
  4.       
  5.     public SSLSocketFactoryEx(KeyStore truststore)      
  6.             throws NoSuchAlgorithmException, KeyManagementException,      
  7.             KeyStoreException, UnrecoverableKeyException {      
  8.         super(truststore);      
  9.       
  10.         TrustManager tm = new X509TrustManager() {      
  11.       
  12.             public java.security.cert.X509Certificate[] getAcceptedIssuers() {      
  13.                 return null;      
  14.             }      
  15.       
  16.             @Override      
  17.             public void checkClientTrusted(      
  18.                     java.security.cert.X509Certificate[] chain, String authType)      
  19.                     throws java.security.cert.CertificateException {      
  20.       
  21.             }      
  22.       
  23.             @Override      
  24.             public void checkServerTrusted(      
  25.                     java.security.cert.X509Certificate[] chain, String authType)      
  26.                     throws java.security.cert.CertificateException {      
  27.       
  28.             }      
  29.         };      
  30.       
  31.         sslContext.init(nullnew TrustManager[] { tm }, null);      
  32.     }      
  33.       
  34.     @Override      
  35.     public Socket createSocket(Socket socket, String host, int port,      
  36.             boolean autoClose) throws IOException, UnknownHostException {      
  37.         return sslContext.getSocketFactory().createSocket(socket, host, port,      
  38.                 autoClose);      
  39.     }      
  40.       
  41.     @Override      
  42.     public Socket createSocket() throws IOException {      
  43.         return sslContext.getSocketFactory().createSocket();      
  44.     }      
  45. }  

调用方法,只要用认证返回的HttpCilent即可。代码:

Java 代码 复制内容到剪贴板
        
  1. public HttpClient getNewHttpClient() {      
  2.         try {      
  3.             KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());      
  4.             trustStore.load(nullnull);      
  5.       
  6.             SSLSocketFactory sf = new SSLSocketFactoryEx(trustStore);      
  7.             sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);      
  8.       
  9.             HttpParams params = new BasicHttpParams();      
  10.             HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);      
  11.             HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);      
  12.       
  13.             SchemeRegistry registry = new SchemeRegistry();      
  14.             registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));      
  15.             registry.register(new Scheme("https", sf, 443));      
  16.       
  17.             ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);      
  18.       
  19.             return new DefaultHttpClient(ccm, params);      
  20.         } catch (Exception e) {      
  21.             return new DefaultHttpClient();      
  22.         }      
  23.     }  

这样就解决了问题,有网友说把腾讯认证的地址https去掉改成http,那是不可取的做法。

我已经把代码集成到signpost中,如果有需要的同学可自行下载,有不明白或者不好的地方给我评论留言。

源码下载:

下载文件
点击这里下载文件

2011-12-11日更新了oauth-signpost-1211.jar包,把原来打jar包时也一起打的commons-codec.jar去掉了。

你可能感兴趣的:(java,exception,socket,String,service,null)