JaxWS:具有基本身份验证的Web服务 [英] JaxWS: Webservice with Basic Authentication

查看:147
本文介绍了JaxWS:具有基本身份验证的Web服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为SOAP Web服务构建一个客户端。我使用IntelliJ IDEA自动生成大部分客户端代码,告诉它从WSDL构建JaxWS Web服务客户端。

I'm building a client for a SOAP webservice. I auto-generated most of the client code with IntelliJ IDEA, by telling it to build a JaxWS webservice client from a WSDL.

webservice在不同的URL上运行(测试,集成,生产),所以我需要能够在我的客户端配置服务URL。我的代码如下所示:

The webservice runs on different URLs (test, integration, production), so I need to be able to configure the service URL in my client. My code looks like this:

String urlString = props.getProperty(URL);
service = new RequestMultiTransportService(new URL(urlString),
              new QName("http://some.uri.com/",
                        "RequestMultiTransportService"));
Boolean useBasicAuth = Boolean.parseBoolean(props.getProperty(BASICAUTH));
RequestMultiTransport rmt = service.getRequestMultiTransportPort();
if (useBasicAuth) {
    String user = props.getProperty(AUTHUSER);
    String pw   = props.getProperty(AUTHPW);
    Map requestContext = ((BindingProvider)rmt).getRequestContext();
    requestContext.put(BindingProvider.USERNAME_PROPERTY, user);
    requestContext.put(BindingProvider.PASSWORD_PROPERTY, pw);
}
ProvisioningResponse response = rmt.send("some", "params", "...");

如您所见,该服务可能需要基本身份验证。这就是问题:虽然我可以为实际请求配置基本身份验证,但我无法将其配置为加载WSDL文件(在 RequestMultiTransportService 的构造函数中发生)。 RequestMultiTransportService 由IDEA自动生成,其构造函数只调用其超级构造函数,即 javax.xml.ws.Service

As you can see, the service might need basic authentication. And here's the problem: While I can configure basic authentication for the actual request, I cannot configure it for loading the WSDL file (which happens in the constructor of RequestMultiTransportService). RequestMultiTransportService is autogenerated by IDEA, and its constructor just calls its super constructor, being the one of javax.xml.ws.Service.

因此,无论Web服务需要基本身份验证,我的代码都会失败,因为它不提供用于获取位于<$ c $的WSDL文件的用户/密码C> urlString 。我想到的一种可能的解决方法是在本地存储WSDL文件,并使用 file:// URL指向它。但这不符合我的要求,因为WSDL文件中定义的服务位置各不相同,我似乎无法更改服务对象中的服务URL已从WSDL文件加载。

So whereever the webservice requires basic authentication, my code fails, because it does not provide a user / password for fetching the WSDL file located at urlString. A possible workaround I thought of is to store the WSDL file locally and point to it with a file:// URL. But this does not fulfill my requirements, because the service location defined in the WSDL file varies, and I don't seem to be able to change the service URL in the service object which has been loaded from the WSDL file.

有没有人可以获得基本身份验证的WSDL文件?

Has anyone a solution for getting the WSDL file with basic authentication?

推荐答案

为WebServiceClient创建一个新的构造函数,并将用户名,密码和端点位置(有所不同)作为参数传递给它。在此之后

Create a new constructor to your WebServiceClient and pass the username, the password and the endpoint location (that varies) as parameter to it. After this put

requestContext.put(BindingProvider.USERNAME_PROPERTY, user);
requestContext.put(BindingProvider.PASSWORD_PROPERTY, pw);
requestContext.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, endpointUrl);

进入requestContext并确保你不会访问defaultConstructor。

into the requestContext and make sure you will not access the defaultConstructor.

这篇关于JaxWS:具有基本身份验证的Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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