2011-01-25 109 views
4

我通过Spring读取LDAP reference docs,并且无法确定针对LDAP服务器的用户认证是否自动执行。春季LDAP认证(自动或不是?)

“自动化”我的意思是,如果您在您的ContextSource中提供了userDn和密码,它会自动发生在bean实例化中。也就是说,程序员永远不需要拨打LdapTemplate.authenticate(...) - 它发生在“幕后”。

所以我想知道

  1. 如果春天LDAP身份验证自动
  2. 如果有域,我可以设置为改变这种行为

感谢,
KTM


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

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; 
    } 
} 
+0

添加了一些相关的源代码 – ktm5124 2011-01-25 19:27:04

回答

1

我想知道LDAP应用程序是否会真正使用的用户DN /我在验证中在运行时收集的密码。

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

它将使用您收集在运行时用户DN和密码。基于你如何配置你的bean,LDAP认证将采用两条路径之一在春:

  1. 绑定认证(使用BindAuthenticator
  2. 密码比较(使用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 /登录页):

<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做认证的信息。