javax.naming.ldap.LdapContext#addToEnvironment ( )源码实例Demo

下面列出了javax.naming.ldap.LdapContext#addToEnvironment ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: james-project   文件: ReadOnlyLDAPUser.java
/**
 * Verifies that the password supplied is actually the user's password, by
 * attempting to rebind to a copy of the LDAP server context using the user's 
 * username and the supplied password.
 * 
 * @param password
 *            The password to validate.
 * @return <code>True</code> if a connection can successfully be established
 *         to the LDAP host using the user's id and the supplied password,
 *         and <code>False</code> otherwise.
 */
@Override
public boolean verifyPassword(String password) {
    boolean result = false;
    LdapContext ldapContext = null;
    try {
        ldapContext = this.ldapContext.newInstance(null);
        ldapContext.addToEnvironment(Context.SECURITY_AUTHENTICATION,
                LdapConstants.SECURITY_AUTHENTICATION_SIMPLE);
        ldapContext.addToEnvironment(Context.SECURITY_PRINCIPAL, userDN);
        ldapContext.addToEnvironment(Context.SECURITY_CREDENTIALS, password);
        ldapContext.reconnect(null);
        result = true;
    } catch (NamingException exception) {
        // no-op
    } finally {
        if (null != ldapContext) {
            try {
                ldapContext.close();
            } catch (NamingException ex) {
                // no-op
            }
        }
    }
    return result;
}
 
protected void applyAuthentication(LdapContext ctx, String userDn, String password) throws NamingException {
	ctx.addToEnvironment(Context.SECURITY_AUTHENTICATION, SIMPLE_AUTHENTICATION);
	ctx.addToEnvironment(Context.SECURITY_PRINCIPAL, userDn);
	ctx.addToEnvironment(Context.SECURITY_CREDENTIALS, password);
	// Force a server call as we have updated the environment (gh-430, gh-502)
	ctx.lookup("");
}
 
protected void applyAuthentication(LdapContext ctx, String userDn, String password) throws NamingException {
	ctx.addToEnvironment(Context.SECURITY_AUTHENTICATION, EXTERNAL_AUTHENTICATION);
}