I am trying to fetch some data from an API secured with SSL. I have configured my OAUth2RestTemplate
with the necessary configuration but I am getting the following exception
Caused by: org.springframework.web.client.ResourceAccessException: I/O error on POST request for "https://.../oauth/token": 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
This is my RestTemplate config:
@EnableOAuth2Client
@Configuration
public class RestTemplateConfig {
private final MyConfig config;
public RestTemplateConfig(MyConfig config) {
this.config = config;
}
@Bean
protected OAuth2ProtectedResourceDetails resource() {
ResourceOwnerPasswordResourceDetails resource = new ResourceOwnerPasswordResourceDetails();
List scopes = new ArrayList<String>();
scopes.add("read");
resource.setAccessTokenUri(nikolaConfig.getBaseUrl() + "/oauth/token");
resource.setClientId("...");
resource.setClientSecret("...");
resource.setGrantType("...");
resource.setScope(scopes);
resource.setUsername(config.getLogin());
resource.setPassword(config.getPassword());
return resource;
}
@Bean
public OAuth2RestOperations restTemplate() {
AccessTokenRequest atr = new DefaultAccessTokenRequest();
return new OAuth2RestTemplate(resource(), new DefaultOAuth2ClientContext(atr));
}
}
And my call:
String test = restTemplate.getForObject(URI.create(config.getBaseUrl() + "/configuration/all"), String.class);
Could someone explain how to set the resttemplate up so it works with Https?
EDIT: I tried adding keystore.p12
containing the site's cert to the application but that changed nothing:
server.ssl.key-store=classpath:keystore.p12
server.ssl.key-store-password=xxx
server.ssl.key-password=xxx
server.ssl.trust-store=classpath:keystore.p12
server.ssl.trust-store-password=xxx