首页 文章

Spring LDAP身份验证(自动或不自动?)

提问于
浏览
5

我通读了Spring LDAP reference docs并且无法确定针对LDAP服务器的用户身份验证是否自动化 .

通过"automated"我的意思是如果你在 ContextSource 中提供userDn和密码,它会在bean实例化时自动发生 . 也就是说,程序员永远不必调用 LdapTemplate.authenticate(...) - 它发生"behind-the-scenes" .

所以我想知道

  • 如果Spring LDAP身份验证是自动的

  • 如果有字段我可以设置更改此行为

谢谢,
KTM


编辑:我在我写的一些代码的上下文中问这个问题 . 以下 ContextSource 是我的beans文件中的上下文源之一,用户可以选择使用它 . 它用于在运行时配置userDn和密码(出于安全原因) . 我想知道LDAP应用程序是否实际使用我在运行时在身份验证中收集的userDn / password . (验证是否在我的代码执行之前?它是否忽略我的代码配置的userDn / password字段?)

public class RuntimeContext extends LdapContextSource {

    public RuntimeContext() {
        super();
        if (!resolveAuthInfo()) {
            System.out.println("Failed to resolve auth info. Exiting...");
            System.exit(1);
        }
    }

    public boolean resolveAuthInfo()
    {
        String myUserDn, myPassword;
        try {
            BufferedReader br = new BufferedReader(
                    new InputStreamReader(System.in));
            System.out.print("userDn: ");
            myUserDn = br.readLine();
            System.out.print("password: ");
            myPassword = br.readLine();
        } catch (IOException e) {
            return false;
        }
        super.setUserDn(myUserDn);
        super.setPassword(myPassword);
        return true;
    }
}

1 回答

  • 1

    我想知道LDAP应用程序是否实际使用我在运行时在身份验证中收集的userDn /密码 .

    http://static.springsource.org/spring-security/site/docs/3.0.x/reference/ldap.html

    它将使用您在运行时收集的userDn和密码 . 根据您配置bean的方式,LDAP身份验证将使用Spring中的两个路径之一:

    • 绑定身份验证(使用 BindAuthenticator

    • 密码比较(使用 PasswordComparisonAuthenticator

    这些验证器在 LdapAuthenticationProvider 的上下文中调用,可以在安全命名空间配置中配置为验证器:

    <authentication-manager alias="authenticationManager">
        <authentication-provider user-service-ref="usernamePasswordUserDetailsService">
            <password-encoder ref="passwordEncoder">
                <salt-source ref="saltSource"/>
            </password-encoder>
        </authentication-provider>
        <authentication-provider ref="ldapAuthenticationProvider"/>
    </authentication-manager>
    

    调用 UsernamePasswordAuthenticationFilter 时(通过/ auth / login页面):

    <http auto-config="true">
        <form-login login-page="/auth/login"
                    login-processing-url="/auth/j_security_check"/>
        <logout invalidate-session="true" logout-url="/auth/logout"/>
    </http>
    

    使用用户名和密码创建令牌 . LdapAuthenticationProvider 响应该令牌类型:

    public class LdapAuthenticationProvider implements AuthenticationProvider, MessageSourceAware {
    
        ...
    
        public boolean supports(Class<?> authentication) {
            return (UsernamePasswordAuthenticationToken.class.isAssignableFrom(authentication));
        }
    }
    

    并使用您在 LdapContextSource 中存储的信息进行身份验证 .

相关问题