JaxWS:具有基本身份验证的 Web 服务

发布于 2025-01-06 01:48:03 字数 1440 浏览 1 评论 0原文

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

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

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 之一。

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

有没有人有通过基本身份验证获取 WSDL 文件的解决方案?

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.

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", "...");

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.

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不可一世的女人 2025-01-13 01:48:03

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

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

requestContext 并确保您不会访问 defaultConstructor。

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文