如何使用证书在spring boot中调用https webserivce

时间:2016-06-12 19:54:54

标签: spring-boot ssl-certificate jax-ws jks sslhandshakeexception

我在使用apache cxf face的spring boot中调用https webservice时遇到以下异常

  

SSLHandshakeException调用https://fanava.shaparak.ir:443/merchantwebservice/jax/merchantAuth:sun.security.validator.ValidatorException:PKIX路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException:无法找到所请求目标的有效证书路径

调用此服务需要哪些配置?

classClient:

@Configuration
public class WSClient {
   @Bean(name = "PaymentWebService")
   public PaymentWebService PaymentWebServiceCLient() throws MalformedURLException {
          JaxWsProxyFactoryBean factory;
          factory = new JaxWsProxyFactoryBean();
          factory.setServiceClass(PaymentWebService.class);
          factory.setAddress("http://localhost:8080/soap-api/merchantAuth_1.0");
          return (PaymentWebService) factory.create();
   }
}

2 个答案:

答案 0 :(得分:1)

1.getget webservice你想要的。

2.使用此证书创建密钥库。

3.ssl使用密钥库配置客户端:

@Configuration
public class WebServiceClient {

@Inject
private PaymentProperties paymentProperties;

@Autowired
private ResourceLoader resourceLoader;

@Bean(name = "PaymentWebService")
public PaymentWebService PaymentWebServiceCLient() throws MalformedURLException {

    JaxWsProxyFactoryBean factory;
    factory = new JaxWsProxyFactoryBean();
    factory.setServiceClass(PaymentWebService.class);
    // factory.setAddress("http://localhost:8080/ws/merchantAuth_1.0");
    factory.setAddress(paymentProperties.getWsPublicUrl());

    PaymentWebService service = (PaymentWebService) factory.create();
    try {
        final Client client = ClientProxy.getClient(service);
        setupSsl((HTTPConduit) ClientProxy.getClient(service).getConduit());
    } catch (Exception e) {
    }
    return service;
}

private void setupSsl(HTTPConduit httpConduit) throws Exception {

    final TLSClientParameters tlsCP = new TLSClientParameters();

    final String keyStoreLoc = paymentProperties.getSsl().getKeyStore();
    final String keyPassword = paymentProperties.getSsl().getKeyStorePassword();
    final String keystoreType = paymentProperties.getSsl().getKeyStoreType();

    final KeyStore keyStore = KeyStore.getInstance(keystoreType);
    Resource resource1 = resourceLoader.getResource(keyStoreLoc);
    keyStore.load(resource1.getInputStream(), keyPassword.toCharArray());
    final KeyManager[] myKeyManagers = getKeyManagers(keyStore, keyPassword);
    tlsCP.setKeyManagers(myKeyManagers);

    final String trustStoreLoc = paymentProperties.getSsl().getTrustStore();
    final String trustStorePassword = paymentProperties.getSsl().getTrustStorePassword();
    final String trustStoreType = paymentProperties.getSsl().getTrustStoreType();

    final KeyStore trustStore = KeyStore.getInstance(trustStoreType);
    Resource resource2 = resourceLoader.getResource(trustStoreLoc);
    trustStore.load(resource2.getInputStream(), trustStorePassword.toCharArray());
    final TrustManager[] myTrustStoreKeyManagers = getTrustManagers(trustStore);
    tlsCP.setTrustManagers(myTrustStoreKeyManagers);

    httpConduit.setTlsClientParameters(tlsCP);
}

private static TrustManager[] getTrustManagers(KeyStore trustStore)
        throws NoSuchAlgorithmException, KeyStoreException {
    String alg = KeyManagerFactory.getDefaultAlgorithm();
    TrustManagerFactory fac = TrustManagerFactory.getInstance(alg);
    fac.init(trustStore);
    return fac.getTrustManagers();
}

private static KeyManager[] getKeyManagers(KeyStore keyStore, String keyPassword)
        throws GeneralSecurityException, IOException {
    String alg = KeyManagerFactory.getDefaultAlgorithm();
    char[] keyPass = keyPassword != null ? keyPassword.toCharArray() : null;
    KeyManagerFactory fac = KeyManagerFactory.getInstance(alg);
    fac.init(keyStore, keyPass);
    return fac.getKeyManagers();
}
}

答案 1 :(得分:0)

网站的根证书不在JVM信任库中。因此,如果您将根证书导入<path_to>/jre/lib/security/cacerts,我认为您可以。