Я пытаюсь сделать вызов HTTPS API, используя RestTemplate. У меня есть keystore.p12 (Сертификат) на моем пути к классам. Я реализовал фабрику соединений SSL для RestTemplate, я получаю эту ошибку. Вот код, который я попробовал.
RestClientConfig. java
@Configuration
public class RestClientConfig {
@Bean
RestTemplate getRestTemplateClientAuthentication(RestTemplateBuilder restTemplateBuilder) throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException, CertificateException, IOException {
final String allPassword = "keystoretest!";
String filePath = "src/main/resources/keystore.p12";
SSLContext sslContext = SSLContextBuilder
.create()
.loadTrustMaterial(new File(filePath), allPassword.toCharArray())
.build();
HttpClient client = HttpClients.custom()
.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
.setSSLContext(sslContext)
.build();
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
requestFactory.setHttpClient(client);
return new RestTemplate(requestFactory);
}
}
RedisServiceImpl. java
@Service
public class RedisServiceImpl implements IRedisService {
private Logger LOGGER = LoggerFactory.getLogger(RedisServiceImpl.class);
@Value("${cache.service.url}")
private String cacheServiceUrl;
@Autowired
private RestTemplate restTemplate;
@Override
public void saveToRedis(List<Order> orderList) throws UnrecoverableKeyException, NoSuchAlgorithmException, URISyntaxException {
HttpEntity httpEntity = null;
for ( Order order: orderList) {
LOGGER.info("{},{}",order.getNbr(), order.getType());
String url = cacheServiceUrl+ "/" + order.getNbr();
URI uri = new URI(url);
httpEntity = AppConstants.getHeaders(order.getType());
ResponseEntity redisResponse = restTemplate.exchange(uri, HttpMethod.POST, httpEntity, Object.class);
if (redisResponse.getStatusCode().is2xxSuccessful()) {
LOGGER.info("Success");
}
}
}
}
org.springframework.web.client.ResourceAccessException: I/O error on POST request for "https://dev.supertest.com/set/6/19287601863": 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: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:744) ~[spring-web-5.1.10.RELEASE.jar:5.1.10.RELEASE]
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:710) ~[spring-web-5.1.10.RELEASE.jar:5.1.10.RELEASE]
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:598) ~[spring-web-5.1.10.RELEASE.jar:5.1.10.RELEASE]