Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configure Maven to use CXF wsdl2java with Basic Authentication

I have an application that needs to integrate with one of SharePoint's web services. This web service cannot be accessed freely and needs authentication.

As such, the standard wsdl2java Maven plugin in my application gives an HTTP 401 error when the generate-sources phase is executed.

Is there a way to setup Maven/POM so that I can provide a user/password that will generate the stubs?

I have come across some answers saying this is not possible but all answers are older than 1 year. I haven't found if Maven have issued an update on this. One option is to save a local copy of the WSDL (as suggested here) but I would like to avoid having local copies.

like image 732
user1817897 Avatar asked Nov 12 '12 11:11

user1817897


2 Answers

Because you mentioned CXF then I suppose you meant cxf-codegen-plugin. It's a bit of a hack but it works.

HTTP authentication credentials can be provided using java.net.Authenticator. One need to just define his own Authenticator class which overrides getPasswordAuthentication(..) method. Then it has to be set as default Authenticator. As far as I know it can't be done declaratively (for instance using environment properties) only programatically using Authenticator.setDefault(..).

In order to call Authenticator.setDefault(..) I would use CXF extension mechanism. Create separate maven project with similar class:

public class AuthenticatorReplacer {

    public AuthenticatorReplacer(Bus bus) {
        java.net.Authenticator.setDefault(new java.net.Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("test", "test123"
                        .toCharArray());
            }
        });
    }

}

and file src\main\resources\META-INF\cxf\bus-extensions.txt with contents:

org.example.AuthenticatorReplacer::false

Then add newly created project as a dependency to cxf-codegen-plugin:

<plugin>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-codegen-plugin</artifactId>
    <version>${project.version}</version>
    <dependencies>
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>cxf-authenticator-replacer</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
    ...
</plugin>

This way AuthenticatorReplacer is initialized by CXF extension mechanism and replaces default Authenticator with ours.

like image 104
Dawid Pytel Avatar answered Sep 19 '22 23:09

Dawid Pytel


An clean alternative to @Dawid Pytel's solution would be to run this class during lifecycle of wsdl class auto generation:

<plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>exec-maven-plugin</artifactId>
      <version>1.4.0</version>
      <executions>
        <execution>
          <phase>generate-sources</phase>
          <goals>
            <goal>java</goal>
          </goals>
        </execution>
      </executions>
      <configuration>
        <mainClass>path.to.AuthenticatorReplacer</mainClass>
      </configuration>
    </plugin>

Important: your AuthenticatorReplacer has to be a main(String[] args) class and running the code inside.

like image 24
membersound Avatar answered Sep 20 '22 23:09

membersound