Spring RestTemplate I / O错误:没有对等证书

时间:2013-08-27 21:50:44

标签: android spring ssl resttemplate self-signed

我总是得到同样的错误,试图获得https资源:

org.springframework.web.client.ResourceAccessException: I/O error: No peer certificate; nested exception is javax.net.ssl.SSLPeerUnverifiedException: No peer certificate

我有一个自签名虚拟主机,我的应用运行,该应用在http上工作正常,但我需要https

这是我在android app中的代码:

mRestTemplate = new RestTemplate();
mRestTemplate.getMessageConverters().add(new GsonHttpMessageConverter());
mRestTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory());

final ResponseObject responseObject = mRestTemplate.postForObject(APP_URL, requestObject, ResponseObject.class);

更新1

  • 我尝试了@nilesh提出的解决方案但没有工作。

  • 我尝试了this solution同样的错误

    HttpParams params = new BasicHttpParams();
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(params, HTTP.DEFAULT_CONTENT_CHARSET);
    HttpProtocolParams.setUseExpectContinue(params, true);
    
    SchemeRegistry schReg = new SchemeRegistry();
    schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    schReg.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
    ClientConnectionManager conMgr = new ThreadSafeClientConnManager(params, schReg);
    
    client = DefaultHttpClient(conMgr, params);
    
    final HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
        factory.setHttpClient(client);
    
    mRestTemplate = new RestTemplate();
    mRestTemplate.setRequestFactory(factory);
    
  • 我尝试了this solution但没有成功,同样的错误

    1. 获取所有必需的证书(root和任何中间CA)
    2. 使用keytool和BouncyCastle提供程序创建密钥库并导入证书
    3. 在您的Android应用中加载密钥库并将其用于安全连接 不要使用标准java.net.ssl.HttpsURLConnection进行安全连接。使用Apache HttpClient(Version 4 atm)库,它已经内置在android中。它建立在java连接库的基础之上,在我看来,更快,更好的模块化和更容易理解。

1 个答案:

答案 0 :(得分:7)

在使用RestTemplate发出任何Http请求之前运行以下方法。这对我有用。

public void trustSelfSignedSSL() {
        try {
            SSLContext ctx = SSLContext.getInstance("TLS");
            X509TrustManager tm = new X509TrustManager() {

                public void checkClientTrusted(X509Certificate[] xcs,
                        String string) throws CertificateException {
                }

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

                public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                    return null;
                }

                @Override
                public void checkClientTrusted(
                        java.security.cert.X509Certificate[] arg0, String arg1)
                        throws java.security.cert.CertificateException {
                }

                @Override
                public void checkServerTrusted(
                        java.security.cert.X509Certificate[] arg0, String arg1)
                        throws java.security.cert.CertificateException {

                }
            };
            ctx.init(null, new TrustManager[] { tm }, null);
            SSLContext.setDefault(ctx);
        } catch (Exception ex) {
            throw new RuntimeException("Exception occurred ",ex)
        }
    }