DZone Forums
Go Back   DZone Forums > Community > Languages & Frameworks > Java
Reload this Page authenticate with ntlm (or kerberos) using java UrlConnection
Notices
Reply
 
LinkBack Thread Tools Display Modes
  (#1 (permalink)) Old
Member
 
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Join Date: Jul 2009
Default authenticate with ntlm (or kerberos) using java UrlConnection - 07-23-2009, 03:23 AM

I need to consume a rest web service with java, passing the credentials of a domain user account.

right now I'm doing it with classic asp

Code:
set xmlHttp = server.createObject( "msxml2.serverxmlhttp" )
xmlHttp.open method, url, false, domain & "\" & user, password
xmlHttp.send body
out = xmlHttp.responseText
set xmlHttp = nothing
and with asp.net

Code:
HttpWebRequest request = (HttpWebRequest) WebRequest.Create( url );

request.Credentials = new NetworkCredential(user, password, domain);

request.Method = WebRequestMethods.Http.Get

HttpWebResponse response = (HttpWebResponse) request.GetResponse();

StreamReader outStream = new StreamReader( response.GetResponseStream(), Encoding.UTF8) ;

output = outStream.ReadToEnd();
how can I achieve this with java? Take into account that I'm not using the credentials of the currently logged user, I'm specifing the domain account (I have the password)

please tell me it's as easy as with classic asp and asp.net....
Reply With Quote
  (#2 (permalink)) Old
Forum Leader
 
glennji's Avatar
 
Posts: 58
Thanks: 0
Thanked 1 Time in 1 Post
Join Date: Feb 2008
Location: London
Send a message via MSN to glennji Send a message via Yahoo to glennji Send a message via Skype™ to glennji
Default 07-24-2009, 07:48 AM

Unfortunately, NTLM is a proprietary Microsoft protocol, so isn't included in the default Java APIs. However, you should be able to do the same thing with a 3rd-party library:

Open Source (and free):
Luigi Dragone Home Page - NTLM Authentication in Java

NTLMv2, commercial:
Jespa - Java Active Directory Integration

caveat: I've used neither. But hope that helps!


Quote:
Happy hacking!
Reply With Quote
  (#3 (permalink)) Old
Member
 
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Join Date: Jul 2009
Default 07-26-2009, 02:55 PM

thanks a lot for your answer, but I wonder if it's possible to do this, with the buil-in JRE classes, using kerberos instead of ntlm... I mean, kerberos is not propietary stuff like NTLM...
Reply With Quote
  (#4 (permalink)) Old
Member
 
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Join Date: Oct 2009
Default 10-12-2009, 02:51 AM

Quote:
Originally Posted by opensas View Post
thanks a lot for your answer, but I wonder if it's possible to do this, with the buil-in JRE classes, using kerberos instead of ntlm... I mean, kerberos is not propietary stuff like NTLM...
I'm pretty sure IIS uses SPNEGO - https://spnego.dev.java.net/ or JCIFS might give you some info - my assumption is that you need to generate an SPNEGO token and attach it to the request. RFC 4559 - SPNEGO-based Kerberos and NTLM HTTP Authentication in Microsoft Windows might be useful.

I'm pretty sure you can use the JGSS API that comes with the JRE 1.6 to get it working. Your Windows AD admins might need to generate a keytab file (+ a service principal - also known as the SPN using "ktpass") for your calling service though (maybe tied to a service account).

Have to do the following

1) Client obtains a Ticket Granting Ticket (TGT)
2) Client requests a Service Ticket (TGS_REQ)
3) Client obtains a Service Ticket (TGS_REP)

Client then sends the SPNEGO token as a part of the request. Couple this with Apache's HttpClient for java..

Have you tried just sending the BASIC authentication header with the request? I guess it depends how the IIS's authentication settings are configured.

Last edited by georgedaswani@hotmail.com; 10-12-2009 at 03:24 AM.
Reply With Quote
  (#5 (permalink)) Old
Member
 
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Join Date: Oct 2009
Default 10-19-2009, 05:01 AM

Here's a project, SpnegoHttpURLConnection, that has an example on how to do it.
Reply With Quote
Reply

Tags
authentication, java, kerberos, ntlm, urlconnection

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



Copyright 1997-2009, DZone, Inc.
vBulletin Skin developed by: vBStyles.com