简体   繁体   English

将凭证从dotNet客户端传递到Java Web Service

[英]Pass Crendentials from dotNet client to Java Web Service

I have a dot net application that call a java web service. 我有一个调用Java Web服务的点网应用程序。 I am trying to implement authentication by passing credentials to the java service. 我正在尝试通过将凭据传递给Java服务来实现身份验证。 Here is the dot net code setting the credentials. 这是设置凭据的点网代码。 How can I get these credentials in my java application? 如何在我的Java应用程序中获得这些凭证? They aren't set in the headers... 它们没有设置在标题中...

 System.Net.NetworkCredential serviceCredentials = new NetworkCredential("user", "pass");
 serviceInstance.Credentials = serviceCredentials;

serviceInstance is an instance of SoapHttpClientProtocol. serviceInstance是SoapHttpClientProtocol的一个实例。

I've tried injecting the WebServiceContext like so 我试过像这样注入WebServiceContext

@Resource
WebServiceContext wsctx;

and pulling the crentials from the headers but they aren't there. 并从标头中提取凭证,但它们不存在。

You are not passing the credentials to your service the correct way. 您没有以正确的方式将凭据传递给服务。 In order to get the Authorize http request header do the following: 为了获得Authorize http请求标头,请执行以下操作:

// Create the network credentials and assign
// them to the service credentials
NetworkCredential netCredential = new NetworkCredential("user", "pass");
Uri uri = new Uri(serviceInstance.Url);
ICredentials credentials = netCredential.GetCredential(uri, "Basic");
serviceInstance.Credentials = credentials;

// Be sure to set PreAuthenticate to true or else
// authentication will not be sent.
serviceInstance.PreAuthenticate = true;

Note: Be sure to set PreAuthenticate to true or else authentication will not be sent. 注意:请确保将PreAuthenticate设置为true,否则将不发送身份验证。 see this article for more information. 有关更多信息,请参见本文

I had to dig-up some old code for this one :) 我不得不为此挖掘一些旧代码:)

Update: After inspecting the request/response headers using fiddler as suggested in the comments below a WWW-Authenticate header was missing at the Java Web Service side. 更新:如下面注释中所述,使用提琴手检查了请求/响应标头之后,Java Web Service端缺少WWW-Authenticate标头。

A more elegant way of implementing "JAX-WS Basic authentication" can be found in this article here using a SoapHeaderInterceptor (Apache CXF Interceptors) 可以在本文中使用SoapHeaderInterceptor (Apache CXF拦截器)在本文中找到实现“ JAX-WS基本认证”的更优雅的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM