Creativity and Innovation

Just another WordPress.com weblog


Leave a comment

Java Windows Authentication

To do Windows Authenticate in java you need jna.jar and platrofm.jar library

Below code is useful to do authentication

import com.sun.jna.platform.win32.Advapi32;
import com.sun.jna.platform.win32.Kernel32;
import com.sun.jna.platform.win32.WinBase;
import com.sun.jna.platform.win32.WinNT;

public class AuthWindowsUser {

public static void main(String[] args) {
boolean isWindowsUser=false;

isWindowsUser= authWindowsUser(“A563627″,””,”test@Pass123″);
if(isWindowsUser) {
System.out.println(“User is authenticated”);
} else {
System.out.println(“User is not authenticated”);
}
}

public static boolean authWindowsUser(String user, String domain, String password) {
WinNT.HANDLEByReference handle = new WinNT.HANDLEByReference();
boolean successful = Advapi32.INSTANCE.LogonUser(user, domain, password,WinBase.LOGON32_LOGON_NETWORK,WinBase.LOGON32_PROVIDER_DEFAULT, handle);
if (successful) {
Advapi32.INSTANCE.RevertToSelf();
Kernel32.INSTANCE.CloseHandle(handle.getValue());
}
return successful;
}

}

Advertisements