0

I have a SOAP web service that I need to consume, that uses WS Security and some security policies.

When I first tried using the following code like this:

CMAdapterService cmAdapterService = new CMAdapterService(); CMAdapter port =
      cmAdapterService.getCMAdapterPort();

      // Use the BindingProvider's context to set the endpoint BindingProvider bp =
      (BindingProvider)port;



      Optional credentials
      bp.getRequestContext().put(BindingProvider.USERNAME_PROPERTY, "myUser");
      bp.getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, "myPass");

      calling the webservice method CmReturn cmReturn =
      port.getPersonalData("12345"); System.out.println(cmReturn);

      byte[] imageData = port.getPersonalData("123") .getPersonalData()
      .getData();

With the code above I run into a policy exception similar to the one here, except that my policy wasn't the ssl_policy it was ATAlwaysCapability policy.

I tried the code from the solution in the link above like this:

JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();

    factory.setServiceClass(CMAdapter.class);
    factory.setAddress("http://myHost:8080/bla/CMAdapter");

    CMAdapter cmAdapter = (CMAdapter) factory.create();

    Client client = ClientProxy.getClient(cmAdapter);
    HTTPConduit http = (HTTPConduit) client.getConduit();

    http.getAuthorization().setUserName("myUser");
    http.getAuthorization().setPassword("myPass");

    byte[] imageData = cmAdapter.getPersonalData("12345").getPersonalData().getData();

However, the code above works from the desktop Java application, but it doesn't work when I deploy it on Wildfly as a Java EE app (war).

To make it work with Wildfly I added the following 4 dependencies as provided to the maven's pom.xml:

<dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-ws-policy</artifactId>
        <version>3.3.1</version>
        <scope>provided</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-tools-common -->
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-tools-common</artifactId>
        <version>3.3.1</version>
        <scope>provided</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-frontend-jaxws -->
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-frontend-jaxws</artifactId>
        <version>3.3.1</version>
        <scope>provided</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-transports-http -->
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-transports-http</artifactId>
        <version>3.3.1</version>
        <scope>provided</scope>
    </dependency>

I also edited the webapp/WEB-INF/jboss-deployment-structure.xml like this:

    <?xml version="1.0" encoding="ISO-8859-1"?>
<jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.2">
    <deployment>
        <exclude-subsystems>
        </exclude-subsystems>
        <dependencies>
            <module name="org.jboss.ws.cxf.jbossws-cxf-client" services="import" />
            <module name="org.apache.cxf.impl">
                <imports>
                    <include path="META-INF"/>
                    <include path="META-INF/cxf"/>
                </imports>
            </module>
        </dependencies>
    </deployment>
</jboss-deployment-structure>

However when I try to call the web service from my enterprise Java app I get org.apache.cxf.transport.http.HTTPException: HTTP response '401: Unauthorized' when communicating with http://myHost:8080/bla/CMAdapter

user3362334
  • 1,980
  • 3
  • 26
  • 58

1 Answers1

0

I solved the problem by following this SO thread

I needed to add the following code:

HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();

//This is the magic line. Setting this to false solved the problem
httpClientPolicy.setAllowChunking(false);

http.setClient(httpClientPolicy);

Not sure exactly why it worked. It seems that wildfly was chunking the request, and that for some reasone username and password were partially sent.

user3362334
  • 1,980
  • 3
  • 26
  • 58