Trying to do NTLM authorization from Java Code.
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   amit_gupta
Posted On:   Monday, April 9, 2001 03:45 AM

I am writing a client application which talks to a web server which has NT authentication. I have coded my client so that it can use NTLM authentication scheme ( http://www.innovation.ch/java/ntlm.html). But when I send Type 1 message request I do not receive back type 2 message. What could be the possible reason..... I am also writing a snip from my code Socket s = new Socket(host,80); InputStream is = s.getInputStream(); OutputStream os = s.getOutputStream(); PrintWriter pw = new PrintWriter(os); String send1 = "GET / HTTP/1.1 "; BufferedReader breader = new BufferedReader( new InputStreamReader(is)); String read1; read1 = breader.readLine();    More>>

I am writing a client application which talks to a web server which has NT authentication. I have coded my client so that it can use NTLM authentication scheme ( http://www.innovation.ch/java/ntlm.html). But when I send Type 1 message request I do not receive back type 2 message. What could be the possible reason.....
I am also writing a snip from my code

Socket s = new Socket(host,80);
InputStream is = s.getInputStream();
OutputStream os = s.getOutputStream();
PrintWriter pw = new PrintWriter(os);

String send1 = "GET / HTTP/1.1

";

BufferedReader breader = new BufferedReader( new InputStreamReader(is));

String read1;
read1 = breader.readLine();
while ( read1 != null)
{
System.out.println(read1);
read1 = breader.readLine();
}

System.out.println("One ends");

Socket s1 = new Socket(host,80);
InputStream is1 = s1.getInputStream();
OutputStream os1 = s1.getOutputStream();
PrintWriter pw1 = new PrintWriter(os1);



NTLM_Type1_Message nm1 = new NTLM_Type1_Message();
nm1.setNTLM_Type1_Message(hostname, domain); //my class for type1 message
String sah = nm1.encode64();
send1 = "GET / HTTP/1.1
";
send1 += "Content-Length: 0
";
send1 += "Authorization: " + sah + "
";

send1 += "
";

pw1.print(send1);
pw1.flush();
********************************/

After sending this request I do not get Type 2 message in response WWW-Authenticate field instead I get only "NTLM ".

Can somebody put light on it?

   <<Less

Re: Trying to do NTLM authorization from Java Code.

Posted By:   Sean_Sullivan  
Posted On:   Wednesday, February 27, 2002 08:37 AM

I am using Sun JDK 1.4.0 on Windows 2000.

I would like to be able authenticate NT users via the JAAS API and NTLM.

Is this possible?

I want users to be able to enter their username and password so that I can authenticate them via NTLM.

My (naively written) sample app is included below...


///////////////////////////////////////////////////////////////////////////





package experimental.jaastest;



import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

import javax.security.auth.*;

import javax.security.auth.callback.UnsupportedCallbackException;

import javax.security.auth.callback.Callback;

import javax.security.auth.login.*;

import java.util.*;

import com.sun.security.auth.NTUserPrincipal;



/**

*

*

* @author: Sean C. Sullivan

*

*/

public class JaasTest extends javax.swing.JFrame

{

public static class LoginCallbackHandler implements javax.security.auth.callback.CallbackHandler

{

public void handle(Callback[] callbacks)

throws java.io.IOException, UnsupportedCallbackException

{

System.out.println("callbacks.length " + callbacks.length);

for (int i = 0; i < callbacks.length; i++)

{

System.out.println(callbacks[i]);

}

}

}



public static class NTLMConfiguration extends javax.security.auth.login.Configuration

{

private static final String strLoginModuleName = "com.sun.security.auth.module.NTLoginModule";

private static final AppConfigurationEntry[] entries;



static

{

Map mOptions = new HashMap();

mOptions.put("debug", "true");

entries = new AppConfigurationEntry[1];

entries[0] = new AppConfigurationEntry(

strLoginModuleName,

AppConfigurationEntry.LoginModuleControlFlag.REQUIRED,

mOptions);

}



public NTLMConfiguration()

{

}



public AppConfigurationEntry[] getAppConfigurationEntry(String appName)

{

// we ignore the appName parameter value



return entries;

}



public void refresh()

{

// empty method

}

}



public static class NTLMAuthenticationPanel extends javax.swing.JPanel

{

public NTLMAuthenticationPanel()

{

this.setLayout(new java.awt.BorderLayout());

JButton btnLogin = new JButton("Go");

btnLogin.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent ae)

{

LoginContext lc = null;



Configuration cfg = Configuration.getConfiguration();

AppConfigurationEntry[] acEntries = cfg.getAppConfigurationEntry(null);

for (int i = 0; i < acEntries.length; i++)

{

System.out.println("Login module name: "

+ acEntries[i].getLoginModuleName());

}



try

{

Set principalSet = new java.util.HashSet();

Set pubCredentialSet = new java.util.HashSet();

Set privateCredentialSet = new java.util.HashSet();



java.security.Principal p = new NTUserPrincipal("foobaruser");



Subject subj = new Subject(

false /* read-only */,

principalSet,

pubCredentialSet,

privateCredentialSet);



lc = new LoginContext(

"appName",

subj,

new LoginCallbackHandler());

lc.login();

}

catch (LoginException ex)

{

System.out.println(ex);

}

catch (SecurityException ex)

{

System.out.println(ex);

}

}

});

this.add(btnLogin);

}





}

public JaasTest()

{

this.getContentPane().add(new NTLMAuthenticationPanel());

}



/**

* main application method

* @param args an array of command-line arguments

*/

public static void main(java.lang.String[] args) {

Configuration.setConfiguration(new NTLMConfiguration());

javax.swing.JFrame f = new JaasTest();

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.setSize(600, 500);

f.setTitle("JAAS Test");

f.setVisible(true);

}

}



///////////////////////////////////////////////////////////////////////////



/*



Related resources:



http://www.jguru.com/forums/view.jsp?EID=398647



http://www.jguru.com/forums/view.jsp?EID=706540



http://www.jguru.com/forums/view.jsp?EID=439210



http://java.sun.com/j2se/1.4/docs/guide/security/jaas/spec/com/sun/security/auth/NTUserPrincipal.html



http://java.sun.com/j2se/1.4/docs/api/javax/security/auth/package-summary.html



http://java.sun.com/j2se/1.4/docs/api/javax/security/auth/login/package-summary.html



http://java.sun.com/j2se/1.4/docs/guide/security/jaas/JAASRefGuide.html





*/

Re: Trying to do NTLM authorization from Java Code.

Posted By:   James_Hicks  
Posted On:   Tuesday, July 24, 2001 09:46 AM

What is your progress on this? I am starting a project that authenticates against an NT Domain using NTLM. Could you show me what you have done so far?

James
About | Sitemap | Contact