user1817897
user1817897

Reputation: 81

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.

Upvotes: 8

Views: 5645

Answers (4)

membersound
membersound

Reputation: 86657

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.

Upvotes: 1

Lao Pan
Lao Pan

Reputation: 1

Dawid's solution works for me too. It is a little tricky though. In Eclipse, the pom.xml keeps complaining that "wsdl2java failed: Could not load extension class AuthenticatorReplacer". You have to ignore this error message and use the command line:

mvn generate-sources

The Java classes will then be generated successfully.

Upvotes: -1

Robert Hutto
Robert Hutto

Reputation: 1440

I verified that Dawid's solution works. Alternatively, you can use SoapUI to pull down and cache the wsdl and then use SoapUi code generation support to use cxf to generate the code.

http://java.dzone.com/tips/generating-client-java-code

Upvotes: 0

Dawid Pytel
Dawid Pytel

Reputation: 2810

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.

Upvotes: 8

Related Questions