配置Maven以通过基本身份验证使用CXF wsdl2java [英] Configure Maven to use CXF wsdl2java with Basic Authentication

查看:381
本文介绍了配置Maven以通过基本身份验证使用CXF wsdl2java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这样,当执行generate-sources阶段时,我的应用程序中的标准wsdl2java Maven插件给出了HTTP 401错误.

As such, the standard wsdl2java Maven plugin in my application gives an HTTP 401 error when the generate-sources phase is executed.

是否可以设置Maven/POM,以便我可以提供将生成存根的用户名/密码?

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

我遇到了一些答案,说这是不可能的,但是所有答案都超过1年.我还没有发现Maven是否对此发布了更新.一种选择是保存WSDL的本地副本(如建议的

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.

推荐答案

因为您提到了CXF,所以我想您的意思是cxf-codegen-plugin.有点hack,但是可以用.

Because you mentioned CXF then I suppose you meant cxf-codegen-plugin. It's a bit of a hack but it works.

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

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(..).

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

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

}

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

and file src\main\resources\META-INF\cxf\bus-extensions.txt with contents:

org.example.AuthenticatorReplacer::false

然后将新创建的项目添加为对cxf-codegen-plugin的依赖项:

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>

这种方式AuthenticatorReplacer由CXF扩展机制初始化,并用我们的默认身份验证器替换.

This way AuthenticatorReplacer is initialized by CXF extension mechanism and replaces default Authenticator with ours.

这篇关于配置Maven以通过基本身份验证使用CXF wsdl2java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆