配置Maven以使用CXF wsdl2java和基本身份验证

use*_*897 8 cxf basic-authentication pom.xml maven

我有一个应用程序需要与SharePoint的一个Web服务集成.此Web服务无法自由访问,需要身份验证.

因此,我的应用程序中的标准wsdl2java Maven插件在执行generate-sources阶段时会出现HTTP 401错误.

有没有办法设置Maven/POM,以便我可以提供将生成存根的用户/密码?

我遇到了一些答案,说这是不可能的,但所有答案都超过1年.我还没有发现Maven是否已发布此更新.一种选择是保存WSDL的本地副本(如此处所示),但我希望避免使用本地副本.

Daw*_*tel 8

因为你提到了CXF,所以我想你的意思是cxf-codegen-plugin.这有点像黑客,但它的工作原理.

可以使用java.net.Authenticator提供HTTP身份验证凭据.需要定义自己的Authenticator类,它覆盖getPasswordAuthentication(..)方法.然后必须将其设置为默认身份验证器.据我所知,它不能以声明方式(例如使用环境属性)以编程方式使用Authenticator.setDefault(..).

为了调用Authenticator.setDefault(..),我会使用CXF扩展机制.使用类似的类创建单独的maven项目:

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());
            }
        });
    }

}
Run Code Online (Sandbox Code Playgroud)

和文件src\main\resources\META-INF\cxf\bus-extensions.txt包含内容:

org.example.AuthenticatorReplacer::false
Run Code Online (Sandbox Code Playgroud)

然后将新创建的项目添加为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>
Run Code Online (Sandbox Code Playgroud)

这样,AuthenticatorReplacer由CXF扩展机制初始化,并用我们的默认Authenticator替换.