2014-01-31 3 views
2

죄송합니다. JNDI의 멍청한 놈입니다. JNDI를 사용하여 간단한 인증을 사용하여 LDAPS에 연결하려고합니다.하지만 데이터를 가져온 후 연결 방법을 알 수 없습니다. 예를 들어 내 나무입니다JNDI를 사용하여 ldap에서 데이터를 가져올 수있는 방법

public static void main(String[] args) { 

// Set up environment for creating initial context 
Hashtable<String, String> env = new Hashtable<String, String>(11); 
env.put(Context.INITIAL_CONTEXT_FACTORY, 
    "com.sun.jndi.ldap.LdapCtxFactory"); 
env.put(Context.PROVIDER_URL, "ldaps://myadress:636"); 

// Authenticate as S. User and password "mysecret" 
env.put(Context.SECURITY_AUTHENTICATION, "simple"); 
env.put(Context.SECURITY_PRINCIPAL, "my BASE DN"); 
env.put(Context.SECURITY_CREDENTIALS, "mypass"); 


try { 
    // Create initial context 
    DirContext ctx = new InitialDirContext(env); 
    // Close the context when we're done 
    ctx.close(); 
} catch (NamingException e) { 
    e.printStackTrace(); 
} 
} 

난 내 트리 및 일부 데이터,하지만 어떻게 얻을 원하는

DirContext ctx = new InitialDirContext(env);` 

후 .. : :입니까?

-ou=people,dc=info,dc=uni,dc=com 

---ou=students 
-----uid=5tey37 

내가 어떻게 할 수있는 uid에 대한 데이터를 얻으시겠습니까?

미안 해요에 대한 멍청한 놈이야, 미안 내 영어

답변

2

당신은 특정 매개 변수를 사용하여 context에서 검색을 호출합니다. 귀하의 예에서는 특정 uid을 기반으로 컨텍스트 검색을 수행하고 디렉터리 개체에 해당하는 attributes을 모두 사용할 수 있습니다.

아래의 예는, 당신은 검색을 조정할 원하는 디렉토리에

// Create initial context 
DirContext ctx = new InitialDirContext(env); 

String searchBase = "ou=people"; 
SearchControls searchCtls = new SearchControls(); 

// Specify the search scope 
searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE); 

String uid = "5tey37"; 
String searchFilter = " (uid=" + uid + ") "; 

NamingEnumeration<?> namingEnum = ctx.search(searchBase,searchFilter, searchCtls); 
while (namingEnum.hasMore()) { 
    SearchResult result = (SearchResult) namingEnum.next(); 
    // GET STUFF 
    Attributes attrs = result.getAttributes(); 
    System.out.println(attrs.get("uid")); 
... 

} 
namingEnum.close(); 
// Close the context when we're done 
ctx.close(); 
+0

큰! 덕분에 많은 특정 속성 수 있습니다! – FelasDroid

+0

@FelasDroid 너는 천만에 – PopoFibo

관련 문제