protected AuthenticationInfo queryForAuthenticationInfo(AuthenticationToken token, LdapContextFactory ldapContextFactory)
            throws NamingException {

        UsernamePasswordToken upToken = (UsernamePasswordToken) token;

        // Binds using the username and password provided by the user.
        LdapContext ctx = null;
        try {
            ctx = ldapContextFactory.getLdapContext(upToken.getUsername(), String.valueOf(upToken.getPassword()));
        } finally {
            LdapUtils.closeContext(ctx);
        }

        return buildAuthenticationInfo(upToken.getUsername(), upToken.getPassword());
    }
Builds an {@link AuthenticationInfo} object by querying the active directory LDAP context for the
specified username.  This method binds to the LDAP server using the provided username and password -
which, if successful, indicates that the password is correct.
<p/>
This method can be overridden by subclasses to query the LDAP server in a more complex way.

@param token              the authentication token provided by the user.
@param ldapContextFactory the factory used to build connections to the LDAP server.
@return an {@link AuthenticationInfo} instance containing information retrieved from LDAP.
@throws NamingException if any LDAP errors occur during the search.
core/src/main/java/org/apache/shiro/realm/activedirectory/ActiveDirectoryRealm.java