Configure Maven to use CXF wsdl2java with Basic Authentication

坚强是说给别人听的谎言 提交于 2019-12-01 15:25:12

问题


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.


回答1:


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.




回答2:


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.




回答3:


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




回答4:


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.



来源:https://stackoverflow.com/questions/13342876/configure-maven-to-use-cxf-wsdl2java-with-basic-authentication

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