Authenticate with ntlm (or kerberos) with java UrlConnection

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

right now i'm doing it with classic asp


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



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? Please note that I do not use the credentials of the current registered user, I specify the domain account (I have a password)

tell me it is as simple as with classic asp and asp.net ....

+5
source share
3 answers

JRE , Java Windows.

, , IMO Apache Commons HttpClient 3.x - , - , NTLM. , HttpClient - .

HttpClient - 4.0, , , NTLM. .

, :

HttpClient httpClient = new HttpClient();
httpClient.getState().setCredentials(AuthScope.ANY, new NTCredentials(user, password, hostPortionOfURL, domain));
GetMethod request = new GetMethod(url);
BufferedReader reader = new InputStreamReader(request.getResponseBodyAsStream());

.

0

java.net.URLStreamHandler java.net.URL - com.intersult.net.http.NtlmHandler:

NtlmHandler handler = new NtlmHandler();
handler.setUsername("domain\\username");
handler.setPassword("password");
URL url = new URL(null, urlString, handler);
URLConnection connection = url.openConnection();

java.net.Proxy url.openConnection(proxy).

Maven-Dependency:

    <dependency>
        <groupId>com.intersult</groupId>
        <artifactId>http</artifactId>
        <version>1.1</version>
    </dependency>
0

Take a look at the SpnegoHttpURLConnection class in the SPNEGO HTTP Servlet Filter project. There are also a few examples in this project.

There is a client library in this project that pretty much does what you do in your example.

Take a look at this example with javadoc ...

 public static void main(final String[] args) throws Exception {
     final String creds = "dfelix:myp@s5";

     final String token = Base64.encode(creds.getBytes());

     URL url = new URL("http://medusa:8080/index.jsp");

     HttpURLConnection conn = (HttpURLConnection) url.openConnection();

     conn.setRequestProperty(Constants.AUTHZ_HEADER
             , Constants.BASIC_HEADER + " " + token);

     conn.connect();

     System.out.println("Response Code:" + conn.getResponseCode());
 }
-2
source

All Articles