0
I am trying to make a Request for sending Nfe to the Sefaz SP server, but I am always receiving 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 requisition?
You imported the certificates to your machine?
– felipesa
Ex: keytool -v -import -trustcacerts -alias exemploCertificao -file C: exemploCertificado.cer -Keystore C: seukeystore -storepass senha123
– felipesa
I used a java solution that accessed the server looking for the certificates and generated the cacert file ... yet it did not work in this structure ... so I changed the whole structure of my service and abandoned Webservicetemplate, going on to use the Axis2 to make communications (and it worked perfectly the generated certificates) ... unfortunately it had a bit of a tight deadline for delivery ... But from what I researched, i should change and implement a new Messagesender to accept these keys and the peculiarities that xml has ...
– Efraim Ferreira