不使用admin / manager帐户的Spring LDAP身份验证

时间:2012-02-09 08:22:22

标签: java spring active-directory ldap

我的要求是验证用户的登录名(用户名和密码),以便能够访问Web应用程序。

使用Java,我能够使用用户提供的登录进行身份验证:

boolean isCredentialsValid = false;

Hashtable env = new Hashtable();        
env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, ldapUrl);
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, username);
env.put(Context.SECURITY_CREDENTIALS, password);

try {
 DirContext ctx = new InitialDirContext(env);
 ctx.close();
 isCredentialsValid = true;
} catch (Exception e) {
 e.printStackTrace(); 
}

System.out.println(isCredentialsValid);

这可以直接在Spring中实现吗?喜欢使用BindAuthenticator或PasswordComparisonAuthenticator?我尝试使用此配置:

<bean id="contextSource"
 class="org.springframework.security.ldap.DefaultSpringSecurityContextSource">
   <constructor-arg value="IP of LDAP Server:389" />
</bean>

<bean id="ldapAuthenticator"
 class="org.springframework.security.providers.ldap.authenticator.BindAuthenticator">
  <constructor-arg ref="contextSource"/>
  <property name="userDNPatterns" >
  <list>
   <value>uid={0}, dc=company,dc=com</value>
  </list>
  </property>
</bean>

<bean id="userSearch"
 class="org.springframework.security.ldap.search.FilterBasedLdapUserSearch">
  <constructor-arg index="0" value="ou=people" />
  <constructor-arg index="1" value="(uid={0})" />
  <constructor-arg index="2" ref="contextSource" />
  <property name="searchSubtree" value="true" />
</bean>

但我正在包围UncategorizedLdapException。

到目前为止,我在Spring LDAP上看到的内容,如果没有提供管理员帐户,我无法直接验证用户的登录信息。

非常感谢任何帮助!

这是堆栈跟踪:

  

LDAP处理期间发生未分类的异常;嵌套异常是javax.naming.NamingException:[LDAP:错误代码1 - 000004DC:LdapErr:DSID-0C0906DC,注释:为了执行此操作,必须在连接上完成成功绑定。,data 0,v1db0];剩余名称'uid=username@company.com';嵌套异常是org.springframework.ldap.UncategorizedLdapException:在LDAP处理期间发生了未分类的异常;嵌套异常是javax.naming.NamingException:[LDAP:错误代码1 - 000004DC:LdapErr:DSID-0C0906DC,注释:为了执行此操作,必须在连接上完成成功绑定。,data 0,v1db0];剩余名称'uid=username@company.com'

3 个答案:

答案 0 :(得分:1)

如果这是针对Windows活动目录的,请尝试使用spring的ActiveDirectoryLdapAuthenticationProvider。

答案 1 :(得分:1)

LdapTemplate(spring-ldap-core 1.3.1RELEASE)在这种情况下是无用的(糟糕的是,我相信它对其他人来说也没用) - 因为在LdapTemplate.authenticate(...)方法上执行搜索,如果没有先成功绑定,显然无法完成。

我在ActiveDirectoryLdapAuthenticationProvider中采用了相同的概念:

  1. 使用旧时尚方式对LDAP进行身份验证

    private static DirContext bindAsUser(String username, String password) {
      final String bindUrl = url;
    
      Hashtable<String, String> env = new Hashtable<String, String>();
      env.put(Context.SECURITY_AUTHENTICATION, "simple");
      String bindPrincipal = username;
      env.put(Context.SECURITY_PRINCIPAL, bindPrincipal);
      env.put(Context.PROVIDER_URL, bindUrl);
      env.put(Context.SECURITY_CREDENTIALS, password);
      env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    
      try {
        return contextFactory.createContext(env);
      } catch (NamingException e) {
        throw new RuntimeException(e);
      }
    }  
    
  2. 使用SpringSecurityLdapTemplate查询LDAP

  3. spring-ldap-core 1.3.1RELEASE的另一个问题,我无法理解为什么它没有合并到spring-security-ldap:3.1。

答案 2 :(得分:0)

您是否为ldap(com.sun.security.auth.module.LdapLoginModule)尝试了JAAS模块?