Configure Maven to use CXF wsdl2java with Basic Authentication

故事扮演 提交于 2019-12-01 17:17:36

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.

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.

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

Lao Pan

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!