Categories
Coding

NT Authentication using JCIFS and SHAJ

How to authenticate a Windows user using JCIFS and SHAJ

Here is another set of examples on how to do native Windows-based user authentication from Java, one using JCIFS and the other using SHAJ. JCIFS has the advantage of not having to install a DLL into the system search path, whereas SHAJ has the advantage of not having to specify a domain controller explicitly in the code. (Incidentally , there is a utility called netdom.exe in the Windows 2000 Resource Kit Tools download that you can use to find your currently active domain controller – just execute netdom /query for the details).

Here is the code:


package uk.co.researchkitchen.auth.nt;

import java.net.UnknownHostException;

import jcifs.UniAddress;
import jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbAuthException;
import jcifs.smb.SmbException;
import jcifs.smb.SmbSession;

import com.cenqua.shaj.Shaj;
import com.cenqua.shaj.log.Log;
import com.cenqua.shaj.log.PrintStreamLog;

public class TestAuth {

public void doAuthJCifs() {
UniAddress mydomaincontroller = null;
try {
mydomaincontroller = UniAddress.getByName( "192.168.0.10" );
} catch (UnknownHostException e) {

e.printStackTrace();
}
NtlmPasswordAuthentication mycreds = new NtlmPasswordAuthentication( "domain", "username", "password" );
try {
SmbSession.logon(mydomaincontroller, mycreds );
} catch( SmbAuthException sae ) {
sae.printStackTrace();
} catch( SmbException se ) {
se.printStackTrace();
}

}

public void doAuthShaj() {
Log.Factory.setInstance(new PrintStreamLog(System.out, true));

if(!Shaj.init()) {
System.err.println("Failed to initialize");
return;
}

System.out.println(Shaj.checkPassword("domain", "username", "password"));

}
}

Leave a Reply