package test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Hashtable;
import javax.naming.NamingEnumeration;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
public class theLdap {
public static void main(String aaa[]){
PreparedStatement pstmt = null;
ResultSet rs = null;
Connection conn = null;
DirContext ctx = null;
NamingEnumeration m_ne = null;
theLdap dao = new theLdap();
try {
ctx = getLdapConnection(); // ldap 접속
String[] attrIDs = {"cn", "sn", "employeeNumber", "businessCategory", "description", "carLicense", "displayName","homePhone","registeredAddress","userpassword"};
//파라미터로 넘어온 인증서 일련번호를 가지고 LDAP filter를 조립
String filter = "(uid=22)";
SearchResult sr = null;
SearchControls constraints = new SearchControls();
//검색범위를 하위디렉토까지 검색(SUBTREE)
//OBJECT_SCOPE
//이름 첨부 객체를 검색합니다.
//ONELEVEL_SCOPE
//이름 첨부 컨텍스트 의 1개 의 레벨을 검색합니다.
//SUBTREE_SCOPE
constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
if (attrIDs != null)
constraints.setReturningAttributes(attrIDs);
//검색을 시작할 BASE DN을 설정하고, 검색
m_ne = ctx.search("CN=USERS", filter, constraints);
if(m_ne != null){
while(m_ne.hasMoreElements()){
//System.out.println(m_ne.nextElement());
sr = (SearchResult)m_ne.next();
//dn출력시 BASE DN은 제외하고 출력된다.
System.out.println(sr.getName()+","+"dc=or,dc=kr");
for (int i=0; i< attrIDs.length; i++) { //attrIDs member count
System.out.println(i + ": " + attrIDs[i] + " : " +
sr.getAttributes().get(attrIDs[i]).get());
}
}
}
/*
// 수정.
while(rs.next()){
ModificationItem[] modItem = new ModificationItem[8]; // 수정할 아이템의 생성
// 속성값 수정.
//modItem[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, new BasicAttribute("mail", "geisel@wizards.com"));
// 속성값 추가
//modItem[1] = new ModificationItem(DirContext.ADD_ATTRIBUTE, new BasicAttribute("telephonenumber", "+1 555 555 5555"));
// 속성값 삭제 jpegphoto
//modItem[2] = new ModificationItem(DirContext.REMOVE_ATTRIBUTE, new BasicAttribute("jpegphoto"));
modItem[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE,new BasicAttribute("cn", rs.getString("NMKO") == null ? "" : rs.getString("NMKO")));
modItem[1] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE,new BasicAttribute("businessCategory","businessCategory")); //rs.getString("BIZCD") == null ? "" : rs.getString("BIZCD")));
modItem[2] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE,new BasicAttribute("description", "description")); //rs.getString("DEPTCD") == null ? "" : rs.getString("DEPTCD")));
modItem[3] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE,new BasicAttribute("carLicense", "carLicense")); //rs.getString("PSID") == null ? "" : rs.getString("PSID")));
modItem[4] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE,new BasicAttribute("displayName","displayName")); //rs.getString("NMKO") == null ? "" : rs.getString("NMKO")));
modItem[5] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE,new BasicAttribute("homePhone", "homePhone")); //rs.getString("EMPTEL") == null ? "" : rs.getString("EMPTEL")));
modItem[6] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE,new BasicAttribute("registeredAddress", "registeredAddress")); //rs.getString("EMAIL") == null ? "" : rs.getString("EMAIL")));
modItem[7] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE,new BasicAttribute("userpassword", "userpassword")); //rs.getString("PSWD") == null ? "" : rs.getString("PSWD")));
//modItem[8] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE,new BasicAttribute("sn", rs.getString("EMPID") == null ? "" : rs.getString("EMPID")));
//modItem[9] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE,new BasicAttribute("employeeNumber", rs.getString("EMPID") == null ? "" : rs.getString("EMPID")));
// Perform requested modifications on named object
ctx.modifyAttributes("uid="+rs.getString("EMPID")+",cn=users", modItem); //ldap정보 수정.
}
*/
/*
// 삭제
while(rs.next()){
ctx.destroySubcontext("uid="+rs.getString("EMPID")+",cn=users"); //ldap정보 삭제
}
*/
/*
// 등록.
while(rs.next()){
try{
ctx.destroySubcontext("uid="+rs.getString("EMPID")+",cn=users"); //ldap정보 삭제
System.out.println("삭제 성공");
}catch(Exception e){
System.out.println("삭제 실패");
}
Attributes mods = new BasicAttributes(true);
mods.put("objectclass","top");
mods.put("objectclass","person");
mods.put("objectclass","organizationalPerson");
mods.put("objectclass","inetOrgPerson");
mods.put("cn",rs.getString("NMKO") == null ? "" : rs.getString("NMKO")); //이름
mods.put("sn",rs.getString("EMPID") == null ? "" : rs.getString("EMPID")); //성
mods.put("employeeNumber",rs.getString("EMPID") == null ? "" : rs.getString("EMPID"));
mods.put("businessCategory",rs.getString("BIZCD") == null ? "" : rs.getString("BIZCD"));
mods.put("description",rs.getString("DEPTCD") == null ? "" : rs.getString("DEPTCD"));
mods.put("carLicense",rs.getString("PSID") == null ? "" : rs.getString("PSID"));
mods.put("displayName",rs.getString("NMKO") == null ? "" : rs.getString("NMKO"));
mods.put("homePhone",rs.getString("EMPTEL") == null ? "" : rs.getString("EMPTEL"));
mods.put("registeredAddress",rs.getString("EMAIL") == null ? "" : rs.getString("EMAIL"));
mods.put("userpassword",rs.getString("PSWD") == null ? "" : rs.getString("PSWD"));
ctx.createSubcontext("uid="+rs.getString("EMPID")+",cn=users", mods);
}
*/
System.out.println("==================성공========================");
close(ctx ,pstmt,rs,conn);
}catch(Exception e){
close(ctx ,pstmt,rs,conn);
e.printStackTrace();
}finally{
close(ctx ,pstmt,rs,conn);
}
}
/**
* 커넥션 닫기.
* @param ctx
* @param pstmt
* @param rs
* @param conn
*/
public static void close( DirContext ctx ,
PreparedStatement pstmt ,
ResultSet rs ,
Connection conn)
{
if(rs != null) try { rs.close(); } catch(SQLException e){ e.printStackTrace(); }
if(pstmt!= null) try { pstmt.close();} catch(SQLException e){ e.printStackTrace(); }
if(conn != null) try { conn.close();} catch(SQLException e){ e.printStackTrace(); }
if(ctx != null) try { ctx.close(); } catch(Exception e){ e.printStackTrace(); }
}
/**
* ldap 커넥션.
* @return
*/
public static DirContext getLdapConnection(){
Hashtable htable = new Hashtable();
DirContext initCtx1 = null;
DirContext ctx = null;
try{
// connection 정보
htable.put("java.naming.factory.initial", "com.sun.jndi.ldap.LdapCtxFactory");
htable.put("java.naming.security.authentication", "Simple");
htable.put("java.naming.provider.url", "ldap://localhost:389");
htable.put("java.naming.security.principal", "cn=test");
htable.put("java.naming.security.credentials", "test1");
initCtx1 = new InitialDirContext(htable);
ctx = (DirContext) initCtx1.lookup("dc=or,dc=kr");
return ctx;
}catch(Exception e){
e.printStackTrace();
}
return ctx;
}
}