【问题标题】:LDAP authentication using scala or java使用 scala 或 java 的 LDAP 身份验证
【发布时间】:2016-10-01 17:10:03
【问题描述】:

我在我的机器上创建了一个 LDAP 服务器,并在 LDAP 服务器中创建了一个用户作为管理员,我可以使用 URL 访问它 http://192.158.2.136/phpldapadmin,现在我的应用程序中有一个登录页面,我想要的是当有人输入用户名和密码时我希望我的应用程序从 LDAP 服务器验证这个用户,即我应该检查输入的任何用户名和密码是否相同存在于 LDAP服务器与否。到目前为止,我尝试使用apacheds-all,但找不到将用户名和密码发送到服务器的正确方法,有人可以告诉我如何在javascala 中执行此操作,我应该可以使用 javascala 作为客户端代码验证来自 LDAP 服务器的用户凭据。提前致谢!!

【问题讨论】:

    标签: java scala ldap


    【解决方案1】:

    我终于找到了解决办法:

     object LDAPValidations {
    
        def validateForLDAP(username: String, passcode: String): Boolean = {
    
        val result = Try {
          var props = new Properties
          props.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory")
          props.put(Context.PROVIDER_URL, "ldap://192.168.1.121:389")
          props.put(Context.SECURITY_PRINCIPAL, s"cn=$username,cn=staff,dc=myreutore,dc=local")
          props.put(Context.SECURITY_CREDENTIALS, "administrator")
    
          var context: InitialDirContext = new InitialDirContext(props)
    
          val controls: SearchControls = new SearchControls
          controls.setReturningAttributes(Array[String]("givenName", "sn", "memberOf", "cn"))
          controls.setSearchScope(SearchControls.SUBTREE_SCOPE)
    
          val answers: NamingEnumeration[SearchResult] = context.search("dc=myrtor,dc=local", s"cn=$username", controls)
          val result: SearchResult = answers.nextElement
    
          val user: String = result.getNameInNamespace
          props = new Properties
          props.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory")
          props.put(Context.PROVIDER_URL, "ldap://192.168.1.121:389")
          props.put(Context.SECURITY_PRINCIPAL, user)
          props.put(Context.SECURITY_CREDENTIALS, passcode)
          context = new InitialDirContext(props)
        }
        result match {
          case Success(v) => true
          case Failure(v) => false
        }
    
      }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2012-09-01
      • 1970-01-01
      • 2012-06-27
      • 2014-03-08
      • 2016-10-03
      • 2016-09-12
      • 2015-12-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多