{ Think. Ask Why. Do it. }

Windows Environment SSO and Java

In windows based enterprise environment, most applications are integrated using single sign-on(SSO) functionality. What is single sign-on,
According to wikipedia:

Single sign-on (SSO) is a property of access control of multiple related, but independent software systems. With this property a user logs in once and gains access to all systems without being prompted to log in again at each of them.

Simply, single sign-on allows enterprise users to access different range of applications using their same domain credentials.

Recently I was facing a scenario where I need to access some resources programmatically using Java. Its a HTTP based resource, its protected based on windows authentication and I was using Oracle Java 6. According to following guide, built in http handler handles the http authentication.

I ran into problem, where I was running the developer environment using my domain account (senthadev\dev) and I have to use a different domain account(senthavde\qatest) to access the resource. It wasn't possible since java always used my developer domain account to authenticate and my account does not have access. How to solve this?

While searching for an solution, I ran into some suggestions such as to override the default library, bytecode patching etc..

Then I found an interesting project called Waffle

Its a 100% java library which uses Windows SSPI to provide SSO funcationality. I used it to impersonate the java threads as a senthadev\qatest user and then access the http resource.

Here is the maven code to include the waffle library:


And the sample java code:

package com.senthadev.winlogon;


import com.sun.jna.platform.win32.Advapi32Util;
import com.sun.jna.platform.win32.Win32Exception;
import com.sun.jna.platform.win32.WinBase;


public class WinLoginAuth {

    public static void main(String [] args){

        String username = "qatest";
        String password = "change_me";
        String domain = "senthadev";

        int logonType = WinBase.LOGON32_LOGON_INTERACTIVE;
        int logonProvider = WinBase.LOGON32_PROVIDER_DEFAULT;

        IWindowsAuthProvider authProvider = new WindowsAuthProviderImpl();
        IWindowsIdentity userIdentity = null;
        IWindowsImpersonationContext impoContext = null;

            userIdentity = authProvider.logonDomainUserEx(username, domain, password, logonType, logonProvider);
            System.out.println("Welcome :" + userIdentity.getFqn());
            impoContext = userIdentity.impersonate();
            System.out.println("Current thread is running as :" + Advapi32Util.getUserName());

            //Access the resource behind SSO
            URL url = new URL("");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            //do the job as senthadev\qates
        catch(Win32Exception winEx){
            System.out.println("Oops!! " + winEx.getMessage());
        catch(Exception ex){
            System.out.println("Main Oops!! " + ex.getMessage());
            if (impoContext != null)

            if (userIdentity != null)
            authProvider = null;


By @Senthadev