java - Spring LDAP 身份验证(自动与否?)

标签 java ldap spring-ldap

我通读了Spring LDAP reference docs并且无法弄清楚针对 LDAP 服务器的用户身份验证是否是自动的。

“自动”是指如果您在 ContextSource 中提供 userDn 和密码,它会在 bean 实例化时自动发生。也就是说,程序员永远不必调用 LdapTemplate.authenticate(...) - 它发生在“幕后”。

所以我想知道

  1. 如果 Spring LDAP 身份验证是自动的
  2. 如果有我可以设置的字段来改变这种行为

谢谢,
公里数


编辑:我在我编写的一些代码的上下文中问这个问题。以下 ContextSource 是我的 beans 文件中的上下文源之一,用户可以选择使用它。它用于在运行时配置 userDn 和密码(出于安全原因)。我想知道 LDAP 应用程序是否会在身份验证中实际使用我在运行时收集的 userDn/密码。 (身份验证是否先于我的代码执行?它是否会忽略我的代码配置的 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;
    }
}

最佳答案

I want to know whether the LDAP application will actually use the userDn/password that I collect at runtime in the authentication.

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

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

  1. 绑定(bind)身份验证(使用 BindAuthenticator)
  2. 密码比较(使用 PasswordComparisonAuthenticator)

这些身份 validator 在 LdapAuthenticationProvider 的上下文中调用,可以在安全命名空间配置中将其配置为身份 validator :

<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>

使用用户名和密码创建 token 。 LdapAuthenticationProvider 响应该 token 类型:

public class LdapAuthenticationProvider implements AuthenticationProvider, MessageSourceAware {

    ...

    public boolean supports(Class<?> authentication) {
        return (UsernamePasswordAuthenticationToken.class.isAssignableFrom(authentication));
    }
}

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

关于java - Spring LDAP 身份验证(自动与否?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4797460/

相关文章:

java - 如何使用 java api 将 Ldif 文件中的默认数据添加到 Ldap 服务器?

java - 管理远程服务回调

java - 如何以编程方式正确调整 View 的大小,使其底部与 ConstraintLayout 中其他 View 的顶部对齐?

java - libgdx: "java.exe finished with non-zero exit value 1"构建为 HTML 时

Java LDAP 密码验证

java - Spring LDAP 对象目录映射器注释缺少属性

java - LdapRepository 更新 spring-ldap

java - 如何使用默认值一般地初始化值类型?

java - 为什么 Nginx 以相反的顺序提供客户端 SSL DN?

java - 无法从java中的 Activity 目录的OU中读取用户属性