简体   繁体   中英

Pass Crendentials from dotNet client to Java Web Service

I have a dot net application that call a java web service. I am trying to implement authentication by passing credentials to the java service. Here is the dot net code setting the credentials. How can I get these credentials in my java application? They aren't set in the headers...

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

serviceInstance is an instance of SoapHttpClientProtocol.

I've tried injecting the WebServiceContext like so

@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:

// 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. 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.

A more elegant way of implementing "JAX-WS Basic authentication" can be found in this article here using a SoapHeaderInterceptor (Apache CXF Interceptors)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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