2017-08-30 251 views
8

在我的项目SSL错误,我有以下项目结构:JAX-WS Axis2的代理在使用的ProxySelector

我有一个生产WAR文件,可以部署在Tomcat应用服务器中的模块。该模块具有依赖于Axis2的库:

<dependency> 
     <groupId>org.apache.axis2</groupId> 
     <artifactId>axis2</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.apache.axis2</groupId> 
     <artifactId>axis2-transport-http</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.apache.axis2</groupId> 
     <artifactId>axis2-webapp</artifactId> 
     <type>war</type> 
    </dependency> 

而这个类包含在WEB-INF下的conf文件夹的axis2.xml文件。

现在这个模块依赖于一个单元模块,它有一个jar包的类型。

在我的网络模块

现在,在我的存根代码,我下面的代码:

GazelleObjectValidator.getInstance()validateObject();

XcpdValidationService是jar模块(依赖项)中的一个类,此方法通过SSL调用外部Web服务并使用代理。

此Web服务客户端是通过JAX WS RI

产生,但是这个类不从父模块使用axis2.xml配置和使用它自己的轴配置,是默认的,我的代理是没有配置...

@WebEndpoint(name = "GazelleObjectValidatorPort") 
public GazelleObjectValidator getGazelleObjectValidatorPort() { 
    return super.getPort(new QName("http://ws.validator.sch.gazelle.ihe.net/", "GazelleObjectValidatorPort"), GazelleObjectValidator.class); 
} 

的方法本身是这样的:

@WebMethod 
@WebResult(name = "validationResult", targetNamespace = "") 
@RequestWrapper(localName = "validateObject", targetNamespace = "http://ws.validator.sch.gazelle.ihe.net/", className = "net.ihe.gazelle.schematron.ValidateObject") 
@ResponseWrapper(localName = "validateObjectResponse", targetNamespace = "http://ws.validator.sch.gazelle.ihe.net/", className = "net.ihe.gazelle.schematron.ValidateObjectResponse") 
public String validateObject(
    @WebParam(name = "base64ObjectToValidate", targetNamespace = "") 
    String base64ObjectToValidate, 
    @WebParam(name = "xmlReferencedStandard", targetNamespace = "") 
    String xmlReferencedStandard, 
    @WebParam(name = "xmlMetadata", targetNamespace = "") 
    String xmlMetadata) 
    throws SOAPException_Exception 
; 

我GazelleObjectValidatorService由以下插件生成:

<plugin> 
    <groupId>org.apache.axis2</groupId> 
    <artifactId>axis2-aar-maven-plugin</artifactId> 
    <version>${axis2.version}</version> 
    <extensions>true</extensions> 
    <executions> 
     <execution> 
      <id>package-aar</id> 
      <phase>prepare-package</phase> 
      <goals> 
       <goal>aar</goal> 
      </goals> 
     </execution> 
    </executions> 
    <configuration> 
     <fileSets> 
      <fileSet> 
      <directory>${project.basedir}/src/main/resources/wsdl</directory> 
       <outputDirectory>META-INF</outputDirectory> 
       <includes> 
        <include>**/*.xsd</include> 
       </includes> 
      </fileSet> 
     </fileSets> 
     <servicesXmlFile>${project.build.outputDirectory}/axis2/services.xml</servicesXmlFile> 
     <wsdlFile>${project.build.outputDirectory}/wsdl/ClientConnectorService.wsdl</wsdlFile> 
    </configuration> 
</plugin> 

我试图重写我的axis2.xml配置transportSender用我自己的定义MyCommonsHttpTransportSender:

<transportSender name="http" 
       class="eu.epsos.pt.cc.MyCommonsHTTPTransportSender"> 
    <parameter name="PROTOCOL">HTTP/1.1</parameter> 
    <parameter name="Transfer-Encoding">chunked</parameter> 

<transportSender name="https" 
       class="eu.epsos.pt.cc.MyCommonsHTTPTransportSender"> 
    <parameter name="PROTOCOL">HTTP/1.1</parameter> 
    <parameter name="Transfer-Encoding">chunked</parameter> 
</transportSender> 

知道该代理。

但不幸的是,由于Web服务客户端在作为战争依赖的jar内部,它似乎并不使用我的axis2.xml配置,而是使用它自己的轴配置,它不知道代理。

这导致以下错误,你清楚地看到,它使用默认CommonsHTTPTransportSender并因此引发错误:

Caused by: java.net.ConnectException: Connection refused (Connection refused) 
    at java.net.PlainSocketImpl.socketConnect(Native Method) 
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) 
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) 
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) 
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) 
    at java.net.Socket.connect(Socket.java:589) 
    at sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:668) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:498) 
    at org.apache.commons.httpclient.protocol.ReflectionSocketFactory.createSocket(ReflectionSocketFactory.java:140) 
    at org.apache.commons.httpclient.protocol.SSLProtocolSocketFactory.createSocket(SSLProtocolSocketFactory.java:130) 
    at org.apache.commons.httpclient.HttpConnection.open(HttpConnection.java:707) 
    at org.apache.commons.httpclient.MultiThreadedHttpConnectionManager$HttpConnectionAdapter.open(MultiThreadedHttpConnectionManager.java:1361) 
    at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:387) 
    at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:171) 
    at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:397) 
    at org.apache.axis2.transport.http.AbstractHTTPSender.executeMethod(AbstractHTTPSender.java:621) 
    at org.apache.axis2.transport.http.HTTPSender.sendViaPost(HTTPSender.java:193) 
    at org.apache.axis2.transport.http.HTTPSender.send(HTTPSender.java:75) 
    at org.apache.axis2.transport.http.CommonsHTTPTransportSender.writeMessageWithCommons(CommonsHTTPTransportSender.java:404) 
    at org.apache.axis2.transport.http.CommonsHTTPTransportSender.invoke(CommonsHTTPTransportSender.java:231) 
    at org.apache.axis2.engine.AxisEngine.send(AxisEngine.java:443) 
    at org.apache.axis2.description.OutInAxisOperationClient.send(OutInAxisOperation.java:406) 
    at org.apache.axis2.description.OutInAxisOperationClient.executeImpl(OutInAxisOperation.java:229) 
    at org.apache.axis2.client.OperationClient.execute(OperationClient.java:165) 
    at org.apache.axis2.jaxws.core.controller.impl.AxisInvocationController.execute(AxisInvocationController.java:578) 
    at org.apache.axis2.jaxws.core.controller.impl.AxisInvocationController.doInvoke(AxisInvocationController.java:127) 
    at org.apache.axis2.jaxws.core.controller.impl.InvocationControllerImpl.invoke(InvocationControllerImpl.java:93) 
    at org.apache.axis2.jaxws.client.proxy.JAXWSProxyHandler.invokeSEIMethod(JAXWSProxyHandler.java:373) 
    ... 40 common frames omitted  

有没有办法让孩子罐子WS客户端使用相同的父模块的Axis2配置(即一个可部署的战争和具有的axis2依赖性?)

UPDATE:

我WAR文件具有Axis2配置,从这个战争的源代码,以产生服务调用wsimport,它位于父WAR中的依赖项的JAR中。该服务调用外部WebService,这发生在Axis上(尽管不使用axis2.xml配置文件,因为该文件位于JAR的WEB-INF文件夹中。 没有任何可能性在没有Axis的JAR中使用外部WebService调用并仅使用JAXWS?这将解决我的问题...

+1

您是否正在使用代理选择器,或者您只需要为存根配置代理? – kolossus

+0

我只想知道如何为我的存根配置代理。如果使用我的代理选择器是可行的,那就太好了。如果有另一种方式,它也是我的一个解决方案。现在他通过套接字并忽略我的代理选择器,因为Axis在 –

+1

内使用HTTP客户端我想我找到了原因:Axis不是JAX-WS的真正实现。 [根据我以前的答案](https://stackoverflow.com/q/30636982/),我们应该使用这两种方法之一来设置安全套接字工厂(而不是'JAXWSProperties.SSL_SOCKET_FACTORY' )在我的答案。你也应该删除你的答案,并添加任何进一步的更新问题 – kolossus

回答

2

Axis2提供了一种方便的方法to configure the HTTP Transport。所以,从你的示例代码如下:

HttpTransportProperties.ProxyProperties proxyProperties = new HttpTransportProperties.new ProxyProperties(); 
proxyProperties.setProxyHostName("hostName"); 
proxyProperties.setProxyPort("hostPort"); 
proxyProperties.setUsername("User"); 
proxyProperties.setPassword("pw"); 
//set the properties 
objectValidatorService.getServiceClient().getOptions().setProperty(HttpConstants.PROXY, proxyProperties); 

以上,因为你使用的股票JAX-WS实现,而不是the Axis2-specific client不会为你工作。 根据您的堆栈跟踪,看起来您正在连接到TLS安全端点。 There's a solution for that

我已经做了大量的研究,并且使用库存JAX-WS无法访问底层HTTPUrlConnection。我们有什么,是一种方式set a custom SSLContextFactory。因此,我们首先创建一个自定义的工厂,that will connect to the proxy first

public class CustomSocketFactory extends SSLProtocolSocketFactory { 

    private static final CustomSocketFactory factory = new CustomSocketFactory(); 

    static CustomSocketFactory getSocketFactory(){ 
     return factory; 
    }  

    public CustomSocketFactory() { 
     super(); 
    } 

    @Override 
    public Socket createSocket(String host, int port, InetAddress clientHost, int clientPort) { 
     Socket socket = null; 
     try { 
      int proxyPort = 1000; 
      InetSocketAddress proxyAddr = new InetSocketAddress("proxyAddr", proxyPort); 
      Socket proxyConn = new Socket(new Proxy(Proxy.Type.SOCKS, proxyAddr)); 
      proxyConn.connect(new InetSocketAddress("endHost", 443)); 
      socket = (SSLSocket) super.createSocket(proxyConn, "proxyEndpoint", proxyPort, true); 

     } catch (IOException ex) { 

     Logger.getLogger(CustomSocketFactory.class.getName()).log(Level.SEVERE, null, ex); 
    } 
     return socket; 
    } 
} 

我们现在要注册这个自定义套接字工厂与Apache运行的HTTPClient(轴不使用java的股票HttpURLConnection的,因为是由您的堆栈跟踪证明):

Protocol.registerProtocol("https",new Protocol("https", new CustomSocketFactory(), 443)); 

这只适用于TLS连接。(但是,自定义套接字工厂也适用于非https端点)。您还需要将超时设置为0 so we can guarantee that your overriden createSocket gets invoked

+0

感谢您的回答。但不幸的是,我的GazelleObjectValidatorService没有getServiceClient()方法。我的GazelleObjectValidatorService是由JAX-WS RI生成的类2.2.8 –

+0

public class GazelleObjectValidatorService extends Service(javax.xml.ws.Service) –

+0

我添加了用于生成GazelleObjectValidator的插件的配置 –