Saturday, October 8, 2016

How to use waffle library for JNA (Java Native Access)

Hello everyone,


I would like to show you guys how to use waffle library for JNA (Java Native Access).

So, coding time...

[Source Code]

package thiago.leoncio;

import com.sun.jna.platform.win32.Sspi;
import com.sun.jna.platform.win32.Sspi.SecBufferDesc;

import waffle.windows.auth.IWindowsSecurityContext;
import waffle.windows.auth.impl.WindowsAuthProviderImpl;
import waffle.windows.auth.impl.WindowsSecurityContextImpl;

/**
 * @author THiago
 * @Date 10/08/2016
 * @Description: How to get details from current machine.
 * @libs for JNA(jna.jar;jna-min.jar;jna-platform.jar;guava-18.0.jar;waffle-jna-1.8.0.jar)
 */

public class Authenticator {

public static void main(String args[]) {

IWindowsSecurityContext clientContext = WindowsSecurityContextImpl.getCurrent( "Negotiate", "localhost" );

// create an auth provider and a security context for the client
// on the server
WindowsAuthProviderImpl provider = new WindowsAuthProviderImpl();
IWindowsSecurityContext serverContext = null;

do {  

    if (serverContext != null) {
        byte[] tokenForTheClientOnTheServer = serverContext.getToken();
     SecBufferDesc continueToken = new SecBufferDesc(Sspi.SECBUFFER_TOKEN, tokenForTheClientOnTheServer);
        clientContext.initialize(clientContext.getHandle(), continueToken, "localhost");
    }

    byte[] tokenForTheServerOnTheClient = clientContext.getToken();
    serverContext = provider.acceptSecurityToken("server-connection", tokenForTheServerOnTheClient, "Negotiate");

} while (clientContext.isContinue());

// client is, only by exchanging byte[] arrays
System.out.println(serverContext.getIdentity().getFqn());
}
}

[Output]
networkName\userID


as I explained in previous blog:
"
On top of JNA exists a library called Waffle which encapsulates all functionality you need to implement user authentication. Get from GitHub at https://github.com/dblock/waffle and also on Maven central. To download the jar files get from here: https://github.com/dblock/waffle/releases/download/Waffle-1.8.0/Waffle.1.8.zip
"

Please, see this JNA and Waffle article also:
http://phonecoding.com/2016/06/how-to-get-details-from-current-machine.html

Happy coding..
Thiago Leoncio.

No comments:

Post a Comment