2017-05-31 56 views
0

我必須使用LDAP將用戶驗證到我的系統中,我的問題是我有很多組織,並且我沒有列出用戶組織。LDAP在不知道他的情況下驗證用戶uid

我有這樣的代碼工作迫使知道組織:

ldap.validate("uid=" + this.email + ",ou="+ThisIsTheOrImForcing+",ou=users,dc=myDC,dc=com,dc=br", this.password); 

我的驗證方法:

public boolean validate(String principal, String password){ 
    // Set up the environment for creating the initial context 
    Hashtable env = new Hashtable(); 
    env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory"); 
    //env.put(Context.PROVIDER_URL, "ldap://IP:PORT/DC=opus"); 
    //env.put(Context.PROVIDER_URL, "ldap://IP:PORT/dc=mydc,dc=com,dc=br"); 
    env.put(Context.PROVIDER_URL, "ldap://IP:PORT/dc=mydc,dc=com,dc=br"); 

    //Authenticate as S. User and password 
    env.put(Context.SECURITY_AUTHENTICATION, "simple"); 
    env.put(Context.SECURITY_PRINCIPAL,principal); 
    env.put(Context.SECURITY_CREDENTIALS, password); 

    try{ 
     // Create the initial context 
     DirContext ctx = new InitialDirContext(env); 
     return true; 
    } catch(AuthenticationException ae) { 
     return false; 
    } catch(NamingException ne) { 
     return false; 
    } 
} 

我有VAR ThisIsTheOrImForcing,我如何可以驗證用戶無需知道他的組織,我必須先搜索它,如何?

回答

0

通常,uid是唯一的標識。搜索操作用於將給定的uid映射到其專有名稱,因此您不必擔心目錄中帳戶的確切位置。

1

LDAP中的簡單綁定需要您提供用戶的DN,因此您需要了解組織的原因。

有兩種方法來對付它:

  • 讓您的目錄對於具有屬性uid = this.email用戶搜索,找回了自己的DN,並與發現的DN進行身份驗證來驗證用戶

  • 使用其他方式進行身份驗證,如SASL機制:See this for more information。您還將需要尋找您的目錄配置支持哪種機制

+0

我認爲在這裏實現的LDAP的結構現在允許我搜索用戶的DN,但我會試一試。 – AgamenonD2

相關問題