在CXF中,有没有一种方法可以以编程方式对客户端的配置进行编程?

时间:2014-03-21 21:29:18

标签: java spring web-services cxf

我有一个包含Web服务和Web服务客户端的项目。我想配置Web服务客户端而不影响Web服务。我怎么能这样做?

我想配置此客户端,以便它可以设置这些值:https://cwiki.apache.org/confluence/display/CXF20DOC/TLS+Configuration

1 个答案:

答案 0 :(得分:3)

是的,spring config完全是可选的,最后它主要被翻译成" java类和cofigurations。"通常CXF docs提供spring和programmatic配置,在你的情况下,有一个特殊的段落可以给你一个起点:

  

请参阅this blog条目,了解HTTPConduit TLS属性   可以从代码中设置

由于通常不鼓励仅使用网址答案,因此我会去垃圾邮件"这个答案将整个客户端代码复制粘贴给任何可能需要它的人:

  public class Client {
       private static void configureSSLOnTheClient(Object c) {
          org.apache.cxf.endpoint.Client client = ClientProxy.getClient(c);
          HTTPConduit httpConduit = (HTTPConduit) client.getConduit();

          try {
              TLSClientParameters tlsParams = new TLSClientParameters();
              tlsParams.setDisableCNCheck(true);

              KeyStore keyStore = KeyStore.getInstance("JKS");
              String trustpass = "password";

              File truststore = new File("certs\\truststore.jks");
              keyStore.load(new FileInputStream(truststore), trustpass.toCharArray());
              TrustManagerFactory trustFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
              trustFactory.init(keyStore);
              TrustManager[] tm = trustFactory.getTrustManagers();
              tlsParams.setTrustManagers(tm);

              truststore = new File("certs\\wibble.jks");
              keyStore.load(new FileInputStream(truststore), trustpass.toCharArray());
              KeyManagerFactory keyFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
              keyFactory.init(keyStore, trustpass.toCharArray());
              KeyManager[] km = keyFactory.getKeyManagers();
              tlsParams.setKeyManagers(km);

              FiltersType filter = new FiltersType();
              filter.getInclude().add(".*_EXPORT_.*");
              filter.getInclude().add(".*_EXPORT1024_.*");
              filter.getInclude().add(".*_WITH_DES_.*");
              filter.getInclude().add(".*_WITH_NULL_.*");
              filter.getExclude().add(".*_DH_anon_.*");
              tlsParams.setCipherSuitesFilter(filter);

              httpConduit.setTlsClientParameters(tlsParams);
          } catch (KeyStoreException kse) {
              System.out.println("Security configuration failed with the following: " + kse.getCause());
          } catch (NoSuchAlgorithmException nsa) {
              System.out.println("Security configuration failed with the following: " + nsa.getCause());
          } catch (FileNotFoundException fnfe) {
              System.out.println("Security configuration failed with the following: " + fnfe.getCause());
          } catch (UnrecoverableKeyException uke) {
              System.out.println("Security configuration failed with the following: " + uke.getCause());
          } catch (CertificateException ce) {
              System.out.println("Security configuration failed with the following: " + ce.getCause());
          } catch (GeneralSecurityException gse) {
              System.out.println("Security configuration failed with the following: " + gse.getCause());
          } catch (IOException ioe) {
              System.out.println("Security configuration failed with the following: " + ioe.getCause());
          }
      }

      public static void main(String args[]) {
          System.out.println("The client's security configuration will be done programatically.");
          System.out.println();
          String address = "https://localhost:9001/SoapContext/SoapPort";
          JaxWsProxyFactoryBean proxyFactory = new JaxWsProxyFactoryBean();
          proxyFactory.setServiceClass(Greeter.class);
          proxyFactory.setAddress(address);

          Greeter client = (Greeter) proxyFactory.create();
          configureSSLOnTheClient(client);

          System.out.println("Invoking greetMe...");
          try {
              String resp = client.greetMe(System.getProperty("user.name"));
              System.out.println("Server responded with: " + resp);
              System.out.println();

          } catch (Exception e) {
              System.out.println("Invocation failed with the following: " + e.getCause());
              System.out.println();
          }

      }
  }