Verify Windows user credentials using native Java APIs

I need to save the username and credentials of Windows for the subsequent launch of some process that requires these credentials.

When I collect this data as user inputs, I want to verify the credentials are correct. Is there a built-in api in Java that can help me verify Windows system credentials?

I went through the LoginContext class, but it seems that it can only be used for single sign-on purposes. Another suggestion I received was to try to start a process that requires these credentials and see if it works or not. But that does not look right.

Please let me know if someone has done this before or someone knows how to do it.

Thanks Piyush

+8
java windows validation credentials
source share
1 answer

By credentials, do you mean the actual user password? You can then use LDAP to try connecting to Windows Active Directory. See Related Question: Verifying Windows Password Using LDAP

A more complicated way to do this is to use your own Windows calls, perhaps through the JNA platform: http://jna.java.net/javadoc/platform/com/sun/jna/platform/win32/package-summary.html

There is a project called "waffles" that wraps this in a more useful library, see, for example, the logonUser function at https://github.com/dblock/waffle/blob/master/Source/JNA/waffle-jna/src/ waffle / windows / auth / impl / WindowsAuthProviderImpl.java . This speaks directly to win32 advapi32.dll.

It will also allow you to perform Windows authentication for local users without a domain.

EDIT : full working code from OP

 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.HANDLEByReference; HANDLEByReference phUser = new HANDLEByReference() if(! Advapi32.INSTANCE.LogonUser("administrator", InetAddress.getLocalHost().getHostName(), "password", WinBase.LOGON32_LOGON_NETWORK, WinBase.LOGON32_PROVIDER_DEFAULT, phUser)) { throw new LastErrorException(Kernel32.INSTANCE.GetLastError()); } 
+7
source share

All Articles