【问题标题】:translate LDAP from LdapCtxFactory to Spring ldap将 LDAP 从 LdapCtxFactory 转换为 Spring ldap
【发布时间】:2016-03-28 16:15:14
【问题描述】:

以下函数成功验证了我的用户。我一直在尝试使用“ldap-authentication-provider”将此代码转换为 Spring XML,但到目前为止没有成功(身份验证返回错误,我无法调试/记录)。

public void auth(String user, String password)
{

    try
    {
        Hashtable<String, String> env = new Hashtable<String, String>();
        env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
        env.put(Context.PROVIDER_URL, "ldap://172.18.0.240:389");
        // 
        env.put(Context.SECURITY_AUTHENTICATION, "simple");
        env.put(Context.SECURITY_PRINCIPAL, "PFA\\"+user);
        env.put(Context.SECURITY_CREDENTIALS, password);

        // Create the initial context

        DirContext ctx = new InitialDirContext(env);
        boolean result = ctx != null;

        if(ctx != null)
            ctx.close();

        return result;
    }
    catch (Exception e)
    {           
        return false;
    }
}

理论上(使用网络上的教程)它应该看起来像这样:

<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-4.0.xsd">

<http auto-config="true">
    <intercept-url pattern="/resources/**" access="permitAll" />
    <intercept-url pattern="/auth" access="permitAll" />
    <intercept-url pattern="/favicon.ico" access="permitAll" />
    <intercept-url pattern="/dashboard" access="hasRole('ROLE_USER')" />
    <intercept-url pattern="/**" access="hasRole('ROLE_USER')" />
    <form-login 
        login-page="/auth" 
        default-target-url="/dashboard" 
        authentication-failure-url="/auth?error" 
        username-parameter="username"
        password-parameter="password" />
    <logout logout-success-url="/auth?logout" />
    <!-- enable csrf protection -->
    <csrf/>
</http>

<authentication-manager>
     <ldap-authentication-provider user-dn-pattern="uid=PFA\\{0}"/>
</authentication-manager>


<ldap-server url="ldap://172.18.0.240:389/" />

</beans:beans>

如果使用以下身份验证代替 LDAP,它就可以正常工作:

<authentication-provider>
    <user-service>
        <user name="peter" password="verysecurepassword" authorities="ROLE_USER" />
    </user-service>
</authentication-provider>

我做错了什么?

编辑:

在获得有关我们使用的活动目录的更多信息后,我编辑了 XML:

<authentication-manager>
     <ldap-authentication-provider user-search-filter="(uid={0})"
      user-search-base="ou=Company"/>
</authentication-manager>


<ldap-server url="ldap://172.18.0.240:389/dc=pfa,dc=local" />

我自己的用户的完整路径是:pfa.local/Company/Users Company/Office/Suboffice/

当我现在尝试登录时,我收到以下错误消息:

原因:LDAP 处理过程中出现未分类的异常;嵌套异常是 javax.naming.NamingException:[LDAP:错误代码 1 - 000004DC:LdapErr:DSID-0C09072B,注释:为了执行此操作,必须在连接上完成成功绑定。,数据 0,v2580];其余名称 'ou=Company'

这似乎与能够查询活动目录有关。但是,对于 LdapCtxFactory 示例,这不是必需的。我可以解决这个问题吗?

【问题讨论】:

  • 什么是错误,你能显示堆栈跟踪吗?
  • 没有显示错误,但是页面被转发到“auth?error”,表示认证失败。
  • 当我删除我的自定义登录页面时,我确实收到以下错误消息:“您的登录尝试不成功,请再试一次。原因:凭据错误”由于相同的凭据适用于 LdapCtxFactory,因此必须de xml 有问题

标签: java spring spring-mvc ldap


【解决方案1】:

随着您更新的配置和更新的错误代码,您似乎需要修复绑定并且配置中缺少“ou”。

查看Spring configuration of LDAP authentication 和帖子“Spring Security AD LDAP: error code 1 - 000004DC: LdapErr: DSID-0C0906E8

【讨论】:

  • 您建议我执行您链接的帖子中提供的 3 个步骤。但我想直接对用户进行身份验证,而不需要管理员凭据。可以使用 DOMAIN/user 登录方法。 Spring 仍然可以实现这一点吗?
猜你喜欢
  • 2011-02-18
  • 2012-01-26
  • 1970-01-01
  • 2017-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多