Spring Boot настраивает внешние веб-сервисы HTTPS с .p12 от localhost - PullRequest
0 голосов
/ 02 декабря 2019

Как мы можем вызвать внешний веб-сервис https с локального хоста при весенней загрузке.

  • У меня есть сертификат .p12, предоставленный внешними сторонними сервисами.
  • Я преобразовал этот файл .p12 в формат .crt и импортировал в "% JAVA_HOME%" \ jre \ lib \ security \ cacerts truststore

Конфигурация Spring Boot

application-local.yml
--
server:
 port: 2020
 ssl:
  key-store: classpath:keystore/expert-dev-server.p12
  key-store-password: changeit
  key-store-type: PKCS12
  key-alias: expert-dev-server

Resttemplate

Resource resource = applicationContext.getResource("classpath:keystore/expert-dev-server.p12");

    String spKeyStoreType = "PKCS12";

    KeyStore keystore = KeyStore.getInstance(spKeyStoreType);
    keystore.load(resource.getInputStream(), "changeit".toCharArray());

    SSLContext sslContext = SSLContexts.custom().loadKeyMaterial(keystore, "changeit".toCharArray()).build();

    HttpClient client = HttpClients.custom().setSSLContext(sslContext).build();

    HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
    requestFactory.setHttpClient(client);

    RestTemplate restTemplate = new RestTemplate(requestFactory);
    restTemplate.setRequestFactory(new BufferingClientHttpRequestFactory(restTemplate.getRequestFactory()));

    List<ClientHttpRequestInterceptor> interceptors = new ArrayList<>();
    interceptors.add(new ExternalLoggingRequestInterceptor());
    restTemplate.setInterceptors(interceptors);

    this.template = restTemplate;

Когда я звоню в службу, появляется следующее исключение.

https-jsse-nio-2020-exec-9, обработка исключения: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: сбой построения пути PKIX: sun.security.provider.certpath.SunCertPathBuilderException: невозможно найти действительный путь сертификации для запрошенной цели

%% Invalidated:  [Session-5, TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384]
https-jsse-nio-2020-exec-9, SEND TLSv1.2 ALERT:  fatal, description = certificate_unknown
https-jsse-nio-2020-exec-9, WRITE: TLSv1.2 Alert, length = 2
https-jsse-nio-2020-exec-9, called closeSocket()
https-jsse-nio-2020-exec-9, handling exception: javax.net.ssl.SSLHandshakeException: 
sun.security.validator.ValidatorException: PKIX path building failed: 
sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path 
to requested target
2019-12-02 11:21:55.831 ERROR 16172 --- [nio-2020-exec-9] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : 
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request 
processing failed; nested exception is org.springframework.web.client.ResourceAccessException: I/O 
error on GET request for "https://<hostname>/services/parties": 
sun.security.validator.ValidatorException: PKIX path building failed: 
sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path 
to requested target; nested exception is javax.net.ssl.SSLHandshakeException:  
sun.security.validator.ValidatorException: PKIX path building failed: s 
sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path 
to requested target] with root cause

sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path 
to requested target
at sun.security.provider.certpath.SunCertPathBuilder.build(SunCertPathBuilder.java:141) ~ 
[na:1.8.0_172]
at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:126) ~ 
[na:1.8.0_172]
at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:280) ~[na:1.8.0_172]
at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:392) ~[na:1.8.0_172]
at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:302) ~[na:1.8.0_172]
at sun.security.validator.Validator.validate(Validator.java:260) ~[na:1.8.0_172]
at sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:324) ~[na:1.8.0_172]
at sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:229) ~[na:1.8.0_172]
at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:124) ~ 
[na:1.8.0_172]
at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1596) ~[na:1.8.0_172]
at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:216) ~[na:1.8.0_172]
at sun.security.ssl.Handshaker.processLoop(Handshaker.java:1052) ~[na:1.8.0_172]
at sun.security.ssl.Handshaker.process_record(Handshaker.java:987) ~[na:1.8.0_172]

Мы можем вызывать один и тот же сервис из среды разработки с конфигурацией application-dev.yml

Что не так в этой конфигурации, какие-либо дополнительные необходимые .?

Пожалуйста, приветp, заранее спасибо.

...