【问题标题】:Java errors with certrificates, rest template证书的 Java 错误,resttemplate
【发布时间】:2023-04-07 20:46:01
【问题描述】:


我的证书有问题。即,我正在使用 2 台服务器。第一个是我试图在另一个上调用的服务。错误是:

org.springframework.web.client.ResourceAccessException: I/O error on POST request for "https://xxx":no more data allowed for version 1 certificate; nested exception is javax.net.ssl.SSLProtocolException: no more data allowed for version 1 certificate

Caused by: javax.net.ssl.SSLProtocolException: no more data allowed for version 1 certificate

10:35:08,479 INFO  [stdout] (http-0.0.0.0-0.0.0.0-8443-1)   at sun.security.ssl.HandshakeMessage$CertificateMsg.<init>(Unknown Source)

10:35:08,480 INFO  [stdout] (http-0.0.0.0-0.0.0.0-8443-1)   at sun.security.ssl.ClientHandshaker.processMessage(Unknown Source)

10:35:08,480 INFO  [stdout] (http-0.0.0.0-0.0.0.0-8443-1)   at sun.security.ssl.Handshaker.processLoop(Unknown Source)

10:35:08,481 INFO  [stdout] (http-0.0.0.0-0.0.0.0-8443-1)   at sun.security.ssl.Handshaker.process_record(Unknown Source)

10:35:08,481 INFO  [stdout] (http-0.0.0.0-0.0.0.0-8443-1)   at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)

10:35:08,482 INFO  [stdout] (http-0.0.0.0-0.0.0.0-8443-1)   at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)

Caused by: java.security.cert.CertificateParsingException: no more data allowed for version 1 certificate

10:35:08,489 INFO  [stdout] (http-0.0.0.0-0.0.0.0-8443-1)   at sun.security.x509.X509CertInfo.parse(Unknown Source)

10:35:08,489 INFO  [stdout] (http-0.0.0.0-0.0.0.0-8443-1)   at sun.security.x509.X509CertInfo.<init>(Unknown Source)

10:35:08,489 INFO  [stdout] (http-0.0.0.0-0.0.0.0-8443-1)   at sun.security.x509.X509CertImpl.parse(Unknown Source)

10:35:08,489 INFO  [stdout] (http-0.0.0.0-0.0.0.0-8443-1)   at sun.security.x509.X509CertImpl.<init>(Unknown Source)

10:35:08,489 INFO  [stdout] (http-0.0.0.0-0.0.0.0-8443-1)   at sun.security.provider.X509Factory.engineGenerateCertificate(Unknown Source)

10:35:08,489 INFO  [stdout] (http-0.0.0.0-0.0.0.0-8443-1)   at java.security.cert.CertificateFactory.generateCertificate(Unknown Source)

10:35:08,489 INFO  [stdout] (http-0.0.0.0-0.0.0.0-8443-1) 

我的代码:

@Override
public void changeState(Device device) {            
    RestTemplate restTemplate = new RestTemplate();  

    HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.setContentType(MediaType.APPLICATION_JSON);

    HttpEntity<Device> entity = new HttpEntity<Device>(device, httpHeaders);
    restTemplate.exchange(url, HttpMethod.POST, entity, Void.class);        
}

我已经尝试过这个解决方案:Disabling SSL Certificate Validation in Spring RestTemplate,但它对我不起作用。如何忽略证书?

【问题讨论】:

  • 谁能帮忙?

标签: java spring ssl-certificate x509certificate resttemplate


【解决方案1】:

尝试将此代码添加到方法的开头。如果是这样,请将代码移动到应用程序的开头:

java.lang.System.setProperty("javax.net.ssl.trustStore", "NONE");

【讨论】:

    【解决方案2】:

    服务器需要附加证书,但我没有看到任何 ConnectionFactory 已创建。

    您需要加载证书材料并使用以下内容设置 SSL:

    HttpClient sslClient = HttpClients.custom()
                                .setSSLContext(sslContext)
                                              .build();
    
            ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(sslClient);
            restTemplate().setRequestFactory(requestFactory);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-03
      • 2018-02-27
      • 2015-12-31
      • 2016-01-14
      • 1970-01-01
      • 2017-07-12
      • 1970-01-01
      相关资源
      最近更新 更多