2016-08-27 15 views
10

मैं एलडीएपी से उपयोगकर्ताओं को एक सेवा खाते के साथ प्रमाणित करने की कोशिश कर रहा हूं। मैं ctx = new InitialDirContext (env) पर त्रुटि से नीचे आ रहा हूं;जावा में एक सेवा खाते के साथ एलडीएपी प्रमाणीकरण

[LDAP: त्रुटि कोड 49 - 8009030C: LdapErr: DSID-0C0903A8, टिप्पणी: AcceptSecurityContext त्रुटि, डेटा 2030, v1db1

कोई मेरी मदद कर सकते समझने के लिए जहां मैं गलत जा रहा हूँ ?

यह मेरा जावा फ़ाइल

/** 
* 
*/ 
package com.dei; 

import java.util.Hashtable; 

import javax.naming.AuthenticationException; 
import javax.naming.Context; 
import javax.naming.NameNotFoundException; 
import javax.naming.NamingEnumeration; 
import javax.naming.NamingException; 
import javax.naming.SizeLimitExceededException; 
import javax.naming.directory.Attribute; 
import javax.naming.directory.Attributes; 
import javax.naming.directory.DirContext; 
import javax.naming.directory.InitialDirContext; 
import javax.naming.directory.SearchControls; 
import javax.naming.directory.SearchResult; 

public class LdapConnector { 


     private static final String LDAP_SERVER_PORT = "389"; 
     private static final String LDAP_SERVER = "server"; 
     private static final String LDAP_BASE_DN = "OU=role,OU=roles,OU=de,OU=apps,DC=meta,DC=company,DC=com"; 
     private static final String LDAP_BIND_DN = "cn=service_account";//service account userid provided by LDAP team 
     private static final String LDAP_BIND_PASSWORD = "password";///service account pwd provided by LDAP team 


     public Boolean validateLogin(String userName, String userPassword) { 
      Hashtable<String, String> env = new Hashtable<String, String>(); 
      env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); 
      env.put(Context.PROVIDER_URL, "ldap://" + LDAP_SERVER + ":" + LDAP_SERVER_PORT + "/" + LDAP_BASE_DN); 

      // To get rid of the PartialResultException when using Active Directory 
      env.put(Context.REFERRAL, "follow"); 

      // Needed for the Bind (User Authorized to Query the LDAP server) 
      env.put(Context.SECURITY_AUTHENTICATION, "simple"); 
      env.put(Context.SECURITY_PRINCIPAL, LDAP_BIND_DN); 
      env.put(Context.SECURITY_CREDENTIALS, LDAP_BIND_PASSWORD); 

      DirContext ctx; 
      try { 
       ctx = new InitialDirContext(env); 
      } catch (NamingException e) { 
       throw new RuntimeException(e); 
      } 

      NamingEnumeration<SearchResult> results = null; 

      try { 
       SearchControls controls = new SearchControls(); 
       controls.setSearchScope(SearchControls.SUBTREE_SCOPE); // Search Entire Subtree 
       controls.setCountLimit(1); //Sets the maximum number of entries to be returned as a result of the search 
       controls.setTimeLimit(5000); // Sets the time limit of these SearchControls in milliseconds 

       String searchString = "(&(objectCategory=user)(sAMAccountName=" + userName + "))"; 

       results = ctx.search("", searchString, controls); 

       if (results.hasMore()) { 

        SearchResult result = (SearchResult) results.next(); 
        Attributes attrs = result.getAttributes(); 
        Attribute dnAttr = attrs.get("distinguishedName"); 
        String dn = (String) dnAttr.get(); 

        // User Exists, Validate the Password 

        env.put(Context.SECURITY_PRINCIPAL, dn); 
        env.put(Context.SECURITY_CREDENTIALS, userPassword); 

        new InitialDirContext(env); // Exception will be thrown on Invalid case 
        System.out.println("Login successful"); 
        return true; 
       } 
       else 
        return false; 

      } catch (AuthenticationException e) { // Invalid Login 
       System.out.println("Login failed" +e.getMessage()); 

       return false; 
      } catch (NameNotFoundException e) { // The base context was not found. 
       System.out.println("Login failed" +e.getMessage()); 
       return false; 
      } catch (SizeLimitExceededException e) { 
       throw new RuntimeException("LDAP Query Limit Exceeded, adjust the query to bring back less records", e); 
      } catch (NamingException e) { 
       throw new RuntimeException(e); 
      } finally { 

       if (results != null) { 
        try { results.close(); } catch (Exception e) { /* Do Nothing */ } 
       } 

       if (ctx != null) { 
        try { ctx.close(); } catch (Exception e) { /* Do Nothing */ } 
       } 
      } 
     } 
} 

उत्तर

3

त्रुटि अमान्य क्रेडेंशियल्स के लिए 49 खड़ा है, लेकिन निदान स्ट्रिंग "AcceptSecurityContext त्रुटि, डेटा 2030" है, जिसका अर्थ है "ऐसा कोई ऑब्जेक्ट" LDAP_BIND_DN "cn = यानी service_account "निर्देशिका में नहीं मिला। मेरा अनुमान है कि "cn = service_account" खाता का पूरा डीएन नहीं है।

+0

मुझे लगता है कि हमने यहां गलत छाप छोड़ी है। "मेरा अतिथि" शायद आपका मतलब था "मेरा अनुमान" –

+0

धन्यवाद Vladislav, सही :) –

2

बाइंड ऑपरेशन विफल रहा है, आमतौर पर खाते के साथ किसी समस्या के कारण।

एलडीएपी सर्वर से कनेक्ट करने के लिए उपयोग किए जाने वाले बाइंड खाते के लिए प्रमाण-पत्र सुनिश्चित करें। कोड डेटा 2030 त्रुटि का मतलब है कि उपयोगकर्ता का डीएन अमान्य है।

संबंधित मुद्दे