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

rkm*_*max 0 ssl spring android self-signed resttemplate

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

org.springframework.web.client.ResourceAccessException: I/O error: No peer certificate; nested exception is javax.net.ssl.SSLPeerUnverifiedException: No peer certificate
Run Code Online (Sandbox Code Playgroud)

我有一个自签名虚拟主机,我的应用程序运行,应用程序正常,http但我需要https.

这是我的Android应用程序中的代码:

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

final ResponseObject responseObject = mRestTemplate.postForObject(APP_URL, requestObject, ResponseObject.class);
Run Code Online (Sandbox Code Playgroud)

更新1

  • 我尝试了@nilesh提出的解决方案但没有奏效.

  • 我尝试了这个解决方案同样的错误

    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);
    
    Run Code Online (Sandbox Code Playgroud)
  • 我尝试了这个解决方案没有成功和相同的错误

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

nil*_*esh 8

在使用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)
        }
    }
Run Code Online (Sandbox Code Playgroud)