Configure Maven to use CXF wsdl2java with basic authentication

I have an application that needs to be integrated with one of the SharePoint web services. This web service cannot be accessed freely and requires authentication.

Thus, the standard wsdl2java Maven plugin in my application gives an HTTP 401 error when performing the source generation phase.

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

I came across some answers saying this is not possible, but all answers are older than 1 year. I did not find if Maven released an update about this. One option is to keep a local copy of WSDL (as suggested here ), but I would like to avoid local copies.

+8
maven basic-authentication cxf
source share
4 answers

Since you mentioned CXF, I suppose you meant cxf-codegen-plugin. This is a bit of a hack, but it works.

HTTP authentication credentials can be provided using java.net.Authenticator. You just need to define its own authenticator class, which overrides the getPasswordAuthentication (..) method. Then it should be installed as the default authenticator. As far as I know, this cannot be done declaratively (for example, using environment properties) only programmatically using Authenticator.setDefault (..).

To call Authenticator.setDefault (..), I would use the CXF extension mechanism. Create a separate maven project with a 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 the src \ main \ resources \ META-INF \ cxf \ bus-extensions.txt file with the contents:

 org.example.AuthenticatorReplacer::false 

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

 <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> 

Thus, the AuthenticatorReplacer is initialized by the CXF extension mechanism and replaces the default authenticator with ours.

+8
source share

A clean alternative to @Dawid Pytel's solution is to run this class during the wsdl class generation lifecycle:

 <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 should be the class main(String[] args) and run the code inside.

+2
source share

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

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

0
source share

Dawid solution also works for me. This is a bit complicated. In Eclipse, pom.xml continues to complain that "wsdl2java failed: failed to load the AuthenticatorReplacer extension class". You should ignore this error message and use the command line:

mvn generate-sources

Then Java classes will be successfully generated.

-one
source share

All Articles