[英]LDAP authentication using scala or java
我在我的機器上創建了一個 LDAP 服務器,並在 LDAP 服務器中創建了一個用戶作為管理員,我可以使用 URL 訪問它,比如http://192.158.2.136/phpldapadmin
,現在我的應用程序中有一個登錄頁面以及什么我想要的是當有人輸入用戶名和密碼時我希望我的應用程序從 LDAP 服務器驗證這個用戶,即我應該檢查輸入的任何用戶名和密碼是否存在於 LDAP 服務器中。到目前為止,我嘗試使用apacheds-all
但不能找到一種將用戶名和密碼發送到服務器的正確方法,有人可以告訴我如何在java
或scala
執行此操作,我應該能夠使用java
或scala
作為客戶端代碼驗證來自 LDAP 服務器的用戶憑據。提前致謝! !
我終於得到了一個解決方案:
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
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.