I'm trying to make a request to send the NFe to the Sefaz SP server, but I'm always getting the certificate error not found. I created a Bean that returns the WebServiceTemplate, and this is my code:
@Value("/pathTo/certificate.pfx")
private String keyStorePath;
@Value("/pathTo/certificate.jks")
private String trustStorePath;
@Value("password")
private String storePassword;
/**
* Template para envio via Sefaz-SP
* @return
* @throws Exception
*/
@Bean
@Qualifier("sefaz")
public WebServiceTemplate getSefazWebServiceTemplate() throws Exception{
SefazWebServiceClient sefazClient = new SefazWebServiceClient();
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setContextPath(NfeRecepcaoLoteResponse.class.getPackage().getName());
sefazClient.setMarshaller(marshaller);
sefazClient.setUnmarshaller(marshaller);
KeyStore ks = KeyStore.getInstance("JKS");
Resource keyStore = new FileSystemResource(new File(keyStorePath));
ks.load(keyStore.getInputStream(), storePassword.toCharArray());
LOGGER.info("Loaded keyStore: "+ keyStore.getURI().toString());
try {
keyStore.getInputStream().close();
} catch(IOException e) {
//Do nothing
}
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
keyManagerFactory.init(ks, storePassword.toCharArray());
KeyStore ts = KeyStore.getInstance("JKS");
Resource trustStore = new FileSystemResource(new File(trustStorePath));
ts.load(trustStore.getInputStream(), storePassword.toCharArray());
LOGGER.info("Loaded trustStore: "+trustStore.getURI().toString());
try {
trustStore.getInputStream().close();
} catch(IOException e) {
//Do nothing
}
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(ts);
HttpsUrlConnectionMessageSender msgSender = new HttpsUrlConnectionMessageSender();
msgSender.setKeyManagers(keyManagerFactory.getKeyManagers());
msgSender.setTrustManagers(trustManagerFactory.getTrustManagers());
sefazClient.setMessageSender(msgSender);
return sefazClient.getWebServiceTemplate();
}
However, whenever I make the request I get the following error:
org.springframework.ws.client.WebServiceIOException: I/O error: 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
Does anyone have any idea how I can put this certificate in the request?