@Override
    public boolean login() throws LoginException {

        Callback[] callbacks = new Callback[2];

        callbacks[0] = new NameCallback("User name");
        callbacks[1] = new PasswordCallback("Password", false);
        try {
            handler.handle(callbacks);
        } catch (IOException | UnsupportedCallbackException ioe) {
            throw (LoginException)new LoginException().initCause(ioe);
        }

        String password;

        String username = ((NameCallback)callbacks[0]).getName();
        if (username == null)
        	return false;

        if (((PasswordCallback)callbacks[1]).getPassword() != null)
        	password = new String(((PasswordCallback)callbacks[1]).getPassword());
        else
        	password="";

        // authenticate will throw LoginException
        // in case of failed authentication
        authenticate(username, password);

        user = new UserPrincipal(username);
        succeeded = true;
        return true;
    }
@Test
    public void testAuthenticatedViaBindOnAnonConnection() throws Exception {
        LoginContext context = new LoginContext("AnonBindCheckUserLDAPLogin", new CallbackHandler() {
            @Override
            public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
                for (int i = 0; i < callbacks.length; i++) {
                    if (callbacks[i] instanceof NameCallback) {
                        ((NameCallback) callbacks[i]).setName("first");
                    } else if (callbacks[i] instanceof PasswordCallback) {
                        ((PasswordCallback) callbacks[i]).setPassword("wrongSecret".toCharArray());
                    } else {
                        throw new UnsupportedCallbackException(callbacks[i]);
                    }
                }
            }
        });
        try {
            context.login();
            fail("Should have failed authenticating");
        } catch (FailedLoginException expected) {
        }
    }