【问题标题】:Testing ldap connection测试 ldap 连接
【发布时间】:2012-12-18 06:47:36
【问题描述】:

我想验证用户输入的 ldap 设置。在设置页面上,用户输入 ldap url、manager dn 和密码。我在此页面上有一个“测试设置”按钮,以便用户可以快速验证 ldap 连接。如何轻松快速地做到这一点?

我们的应用程序使用 spring security 并正在向其添加 ldap 身份验证。 我是 java 和 ldap 的新手,非常感谢您指出正确的方向。

谢谢。

【问题讨论】:

    标签: java spring spring-security ldap


    【解决方案1】:

    根据所提供的信息,很难说出您知道什么以及您还不知道什么。所以,我建议你在 java.net LdapTemplate: LDAP Programming in Java Made Simple 上遵循这个有用的教程,并跳过与你无关的章节(它是从 2006 年开始的,但仍然可以)。文中引用的Spring LDAP目前版本为1.3.1。

    如果您现在想不使用 Spring LDAP,可以使用以下传统代码:

    Map<String, String> env = new HashMap<String, String>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "ldap://localhost:389/dc=jayway,dc=se");
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.SECURITY_PRINCIPAL, "uid="+ username +",ou=system"); // replace with user DN
    env.put(Context.SECURITY_CREDENTIALS, password);
    
    DirContext ctx;
    try {
       ctx = new InitialDirContext(env);
    } catch (NamingException e) {
       // handle
    }
    try {
       SearchControls controls = new SearchControls();
       controls.setSearchScope( SearchControls.SUBTREE_SCOPE);
       ctx.search( "", "(objectclass=person)", controls);
       // no need to process the results
    } catch (NameNotFoundException e) {
       // The base context was not found.
       // Just clean up and exit.
    } catch (NamingException e) {
       // exception handling
    } finally {
       // close ctx or do Java 7 try-with-resources http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html
    }
    

    【讨论】:

    • 您的示例运行良好。而且,我也尝试过使用 spring 的 DefaultSpringSecurityContextSource。
    • 上面引用的文章在现已失效的 java.net 站点(LdapTemplate:LDAP Programming in Java Made Simple)已移至此 URL:community.oracle.com/docs/DOC-983546
    • 答案中的 java.net 链接是 functioning archive.org link。 Oracle-DOC-983546 链接重定向到“您无权执行此操作”,即使在使用(免费、未付费、入门级)Oracle 单点登录帐户后也是如此。
    【解决方案2】:

    使用 Spring LDAP 身份验证测试 LDAP 连接:

    即使用 authenticate() 方法:

    ldapTemplate.authenticate(query, password);
    

    甚至更好,使用 getContext() 方法:

    ldapTemplate.getContextSource().getContext(userDn, userPassword));
    

    捕捉org.springframework.ldap.CommunicationException检查连接是否成功。

    完整的代码 sn-p 应如下所示:

    // Create the spring LdapTemplates; i.e. connections to the source and target ldaps:
    try {
        // Note: I'm using the direct LdapTemplate initialization rather than with bean creation (Spring ldap supports both) 
        log.info("Connecting to LDAP " + sourceHost + ":" + sourcePort + "...");    
        LdapContextSource sourceLdapCtx = new LdapContextSource();
        sourceLdapCtx.setUrl("ldap://" + sourceHost + ":" + sourcePort + "/");
        sourceLdapCtx.setUserDn(sourceBindAccount);
        sourceLdapCtx.setPassword(sourcePassword);
        sourceLdapCtx.setDirObjectFactory(DefaultDirObjectFactory.class);
        sourceLdapCtx.afterPropertiesSet();
        sourceLdapTemplate = new LdapTemplate(sourceLdapCtx);
        // Authenticate:
        sourceLdapTemplate.getContextSource().getContext(sourceBindAccount, sourcePassword);
    } catch (Exception e) {
        throw new Exception("Failed to connect to LDAP - " + e.getMessage(), e);
    }
    

    注意:我使用的是spring LDAP 2.3.x 版本:

    <dependency>
        <groupId>org.springframework.ldap</groupId>
        <artifactId>spring-ldap-core</artifactId>
    </dependency>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多