
LDAP Get Values 함수는 LDAP 서버에서 특정 속성을 검색하는 데 사용되는 함수입니다. 이 함수의 매개 변수는 다음과 같습니다.
- baseDn: LDAP 서버에서 검색할 데이터베이스의 DN(Distinguished Name)
- filter: 검색할 속성을 지정하는 필터
- attributes: 검색할 속성 목록
- connection: LDAP 서버와의 연결 객체
각 매개 변수의 역할은 다음과 같습니다.
- baseDN은 LDAP 서버에서 검색할 데이터베이스의 DN을 지정합니다. 예를 들어, "dc=example,dc=com"과 같이 DN을 지정합니다.
- filter는 검색할 속성을 지정하는 필터를 지정합니다. 예를 들어, "(objectClass=person)"과 같이 필터를 지정합니다.
- attributes는 검색할 속성 목록을 지정합니다. 예를 들어, "cn, sn, mail"과 같이 속성 목록을 지정합니다.
- connection은 LDAP 서버와의 연결 객체를 지정합니다.
LDAP Get Values 함수의 반환 값은 다음과 같습니다.
- 결과 속성 목록: LDAP 서버에서 검색한 속성 목록을 반환합니다.
- 오류 코드: LDAP 서버와의 연결 오류나 검색 오류가 발생한 경우 오류 코드를 반환합니다.
LDAP Get Values 함수를 사용하여 속성을 검색할 때, 다음 오류가 발생할 수 있습니다.
- LDAP 서버와의 연결 오류: LDAP 서버와의 연결이 실패한 경우 오류가 발생합니다.
- 검색 오류: 검색할 속성이 존재하지 않는 경우 오류가 발생합니다.
- 필터 오류: 필터가 잘못된 경우 오류가 발생합니다.
예를 들어, LDAP Get Values 함수를 사용하여 LDAP 서버에서 cn, sn, mail 속성을 검색하는 예제는 다음과 같습니다.
#hostingforum.kr
java
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
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 LDAPGetValuesExample {
public static void main(String[] args) {
String baseDn = "dc=example,dc=com";
String filter = "(objectClass=person)";
String[] attributes = {"cn", "sn", "mail"};
String ldapUrl = "ldap://localhost:389";
try {
DirContext ctx = getLdapContext(ldapUrl, "uid=admin,ou=system", "password");
SearchControls controls = new SearchControls();
controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
NamingEnumeration results = ctx.search(baseDn, filter, controls);
while (results.hasMore()) {
SearchResult result = results.next();
Attributes attrs = result.getAttributes();
Attribute attr = attrs.get("cn");
if (attr != null) {
System.out.println("cn: " + attr.get());
}
attr = attrs.get("sn");
if (attr != null) {
System.out.println("sn: " + attr.get());
}
attr = attrs.get("mail");
if (attr != null) {
System.out.println("mail: " + attr.get());
}
}
} catch (NamingException e) {
System.out.println("LDAP 서버와의 연결 오류: " + e.getMessage());
}
}
private static DirContext getLdapContext(String ldapUrl, String userDn, String password) throws NamingException {
Properties env = new Properties();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, ldapUrl);
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, userDn);
env.put(Context.SECURITY_CREDENTIALS, password);
return new InitialDirContext(env);
}
}
이 예제는 LDAP Get Values 함수를 사용하여 LDAP 서버에서 cn, sn, mail 속성을 검색하는 방법을 보여줍니다.
2025-08-03 21:44