【问题标题】:Creating SSL client with Axis2/Java使用 Axis2/Java 创建 SSL 客户端
【发布时间】:2011-09-21 21:28:54
【问题描述】:

我正在尝试连接到使用 SSL 但没有成功的 WebService。我使用 Axis2,我发现了一些有用的文章:http://people.apache.org/~dumindu/docs/HowToConfigureSSL.html,但它是针对 C 的。在这篇文章中,他们使用 axis2.xml 或 C 编码设置了 SERVER_CERT、KEY_FILE 和 SSL_PASSPHRASE 的路径。我试图更改配置文件,但这对我不起作用。如果有人知道如何在 Java 代码中设置此参数,请告诉我。

【问题讨论】:

    标签: java ssl axis2


    【解决方案1】:

    您可能对this answer 感兴趣以回答类似问题。特别是,Axis 2 似乎正在使用 Apache HttpClient 3.x,根据this document

    如果你想执行 SSL 客户端 身份验证(2 路 SSL),您可以 使用 Protocol.registerProtocol HttpClient 的特性。你可以 覆盖“https”协议,或使用 SSL 的不同协议 客户端身份验证通信 如果您不想与常规混淆 HTTPS。在以下位置查找更多信息 http://jakarta.apache.org/commons/httpclient/sslguide.html

    (您可以从现有密钥库构建 SSLContext,并使用 this socket factory 配置 HttpClient 3.1。)

    【讨论】:

      【解决方案2】:

      我为不同的端点初始化了EasySSLProtocolSocketFactory 和协议实例,并使用这样的唯一键注册协议:

      /**
       * This method does the following:
       * 1. Creates a new and unique protocol for each SSL URL that is secured by client certificate
       * 2. Bind keyStore related information to this protocol
       * 3. Registers it with HTTP Protocol object 
       * 4. Stores the local reference for this custom protocol for use during furture collect calls
       * 
       *  @throws Exception
       */
      public void registerProtocolCertificate() throws Exception {
          EasySSLProtocolSocketFactory easySSLPSFactory = new EasySSLProtocolSocketFactory();
          easySSLPSFactory.setKeyMaterial(createKeyMaterial());
          myProtocolPrefix = (HTTPS_PROTOCOL + uniqueCounter.incrementAndGet());
          Protocol httpsProtocol = new Protocol(myProtocolPrefix,(ProtocolSocketFactory) easySSLPSFactory, port);
          Protocol.registerProtocol(myProtocolPrefix, httpsProtocol);
          log.trace("Protocol [ "+myProtocolPrefix+" ] registered for the first time");
      }
      
      /**
       * Load keystore for CLIENT-CERT protected endpoints
       */
      private KeyMaterial createKeyMaterial() throws GeneralSecurityException, Exception  {
          KeyMaterial km = null;
          char[] password = keyStorePassphrase.toCharArray();
          File f = new File(keyStoreLocation);
          if (f.exists()) {
              try {
                  km = new KeyMaterial(keyStoreLocation, password);
                  log.trace("Keystore location is: " + keyStoreLocation + "");
              } catch (GeneralSecurityException gse) {
                  if (logErrors){
                      log.error("Exception occured while loading keystore from the following location: "+keyStoreLocation, gse);
                      throw gse;
                  }
              }
          } else {
              log.error("Unable to load Keystore from the following location: " + keyStoreLocation );
              throw new CollectorInitException("Unable to load Keystore from the following location: " + keyStoreLocation);
          }
          return km;
      }   
      

      当我必须调用 Web 服务时,我会这样做(基本上将 URL 中的“https”替换为 https1、https2 或其他内容,具体取决于您为该特定端点初始化的协议):

      httpClient.getHostConfiguration().setHost(host, port,Protocol.getProtocol(myProtocolPrefix));
      initializeHttpMethod(this.url.toString().replace(HTTPS_PROTOCOL, myProtocolPrefix));
      

      它就像一个魅力!

      【讨论】:

      • 你能指出进口吗? EasySSLProtocolSocketFactory?关键材料?具有依赖关系的完整示例将非常好。谢谢。
      • httpClientinitializeHttpMethodaxis2 的东西还是?
      猜你喜欢
      • 1970-01-01
      • 2011-05-23
      • 1970-01-01
      • 1970-01-01
      • 2011-11-19
      • 2010-09-20
      • 2014-01-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多