2016-11-21 185 views
0

對LDAP和Active Directory不熟悉。我剛剛在Windows Server 2012中安裝了與Active Directory相關的功能,並且所有配置都已完成,並且還創建了一個用戶。無法通過JAVA中的LDAP連接到Active Directory服務

當我嘗試通過LDAP與Active Directory連接時出現以下錯誤。

Exception in thread "main" javax.naming.AuthenticationException: [LDAP: error code 49 - 80090308: LdapErr: DSID-0C0903C5, comment: AcceptSecurityContext error, data 52e, v2580] 
at com.sun.jndi.ldap.LdapCtx.mapErrorCode(LdapCtx.java:3067) 
at com.sun.jndi.ldap.LdapCtx.processReturnCode(LdapCtx.java:3013) 
at com.sun.jndi.ldap.LdapCtx.processReturnCode(LdapCtx.java:2815) 
at com.sun.jndi.ldap.LdapCtx.connect(LdapCtx.java:2729) 
at com.sun.jndi.ldap.LdapCtx.<init>(LdapCtx.java:296) 
at com.sun.jndi.ldap.LdapCtxFactory.getUsingURL(LdapCtxFactory.java:175) 
at com.sun.jndi.ldap.LdapCtxFactory.getUsingURLs(LdapCtxFactory.java:193) 
at com.sun.jndi.ldap.LdapCtxFactory.getLdapCtxInstance(LdapCtxFactory.java:136) 
at com.sun.jndi.ldap.LdapCtxFactory.getInitialContext(LdapCtxFactory.java:66) 
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:667) 
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288) 
at javax.naming.InitialContext.init(InitialContext.java:223) 
at javax.naming.ldap.InitialLdapContext.<init>(InitialLdapContext.java:134) 
at com.infomindz.sample.ldap.LDAPTest.main(LDAPTest.java:79) 

我已經嘗試了一些在網上的文章給出的解決方案,但我仍然得到同樣的錯誤,不知道如何解決這種類型的異常。隨着下面的代碼只有我在下面的代碼行本身很堅持的錯誤嘗試

public static void main(String[] args) throws NamingException 
{ 

    final String ldapSearchBase = "dc=sample,dc=com"; 

    final String ldapUsername = "sampleAdminUser"; 
    final String ldapPassword = "password"; 

    final String ldapAccountToLookup = "sampleUser"; 


    Hashtable env = new Hashtable(); 
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); 
    env.put(Context.PROVIDER_URL, "ldap://XX.XX.XX.XX:389"); 
    env.put(Context.SECURITY_AUTHENTICATION, "simple"); 
    if(ldapUsername != null) 
    { 
     env.put(Context.SECURITY_PRINCIPAL, ldapUsername); 
    } 
    if(ldapPassword != null) 
    { 
     env.put(Context.SECURITY_CREDENTIALS, ldapPassword); 
    } 

    LdapContext ctx1 = new InitialLdapContext(env, null); 
    LDAPTest ldap = new LDAPTest(); 

    SearchResult srLdapUser = ldap.findAccountByAccountName(ctx1, ldapSearchBase, ldapAccountToLookup); 

    System.out.println("srLdapUser :" + srLdapUser); 

} 

從我的代碼:

LdapContext的CTX1 =新InitialLdapContext(ENV,NULL);

如果我在這方面有所不同,請張貼你的上下文。 在此先感謝。

+0

'LDAP:錯誤代碼49'表示無效憑證。你確定他們是正確的嗎? – RobAu

+0

@RobAu是的憑據都是正確的。爲什麼我對此有信心,因爲我通過使用相同的用戶名和密碼登錄到系統。 –

回答

1

您需要提供用戶的RDN作爲SECURITY_PRINCIPAL,例如「cn = Administrator」或完整的DN,如「cn = Administrator,ou = xyz,dc = infomindz,dc = com」。

要進行交叉檢查,您可以安裝LDAP目錄工作室(https://directory.apache.org/studio/)等LDAP瀏覽器,並嘗試連接到AD以提供管理員用戶的RDN或DN。

+0

我不認爲你需要這樣的微軟AD。 – RobAu

+0

@RobAu我認爲它對於JNDI是必需的,而不管你連接的是哪個LDAP服務器。 – Roshith

+0

Microsoft Active Directory具有LDAP服務器端功能的概念,稱爲不明確名稱解析(https://ldapwiki.com/wiki/Ambiguous%20Name%20Resolution),允許使用某些高級綁定值。 – jwilleke

0

可以通過下面的更新的代碼,我用LDAP查詢

ldapUsername域控制器的管理員用戶獲取用戶和用戶的距離Active Directory中細節實現。

ldapPassword是管理員用戶的密碼。

ldapAccountToLookup是必須在給定域下搜索的用戶名。

public static void main(String[] args) throws NamingException 
{ 

    final String ldapSearchBase = "dc=NewForest,dc=sample,dc=com"; 

    final String ldapUsername = "sampleAdminUser"; 
    final String ldapPassword = "Password"; 

    final String ldapAccountToLookup = "sampleUser"; 


    Hashtable env = new Hashtable(); 
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); 
    env.put(Context.PROVIDER_URL, "ldap://XX.XX.XX.XX:389"); 
    env.put(Context.SECURITY_AUTHENTICATION, "simple"); 
    env.put(Context.SECURITY_PRINCIPAL, ldapUsername); 
    env.put(Context.SECURITY_CREDENTIALS, ldapPassword); 
    DirContext ctx = new InitialDirContext(env); 


    LDAPTest ldap = new LDAPTest(); 

    SearchResult srLdapUser = ldap.findAccountByAccountName(ctx, ldapSearchBase, ldapAccountToLookup); 
    System.out.println("srLdapUser :" + srLdapUser); 

}