[LDAP SSL] TEST
package com.imws;
import java.io.File;
import java.io.FileInputStream;
import java.security.KeyStore;
import java.security.cert.Certificate;
import java.security.cert.CertificateFactory;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.*;
import javax.naming.*;
public class TestLpap {
public static void main(String[] args) {
/*TestLpap test = new TestLpap();
test.PrintCertInfo();
test.PrintCertFromKeyStrore();*/
String keystorePath = System.getProperty("java.home") +"/lib/security/jssecacerts";
System.setProperty("javax.net.ssl.keyStore", keystorePath);
System.setProperty("javax.net.ssl.keyStorePassword", "imsi00.!");
Hashtable<String, Object> env = new Hashtable<String, Object>(11);
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://192.168.0.233:20390");
// Specify SSL
env.put(Context.SECURITY_PROTOCOL, "ssl");
//env.put(Context.PROVIDER_URL, "ldap://192.168.0.233:20389");
// Authenticate as S. User and password "mysecret"
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, "eTGlobalUserName=imadmin,eTGlobalUserContainerName=Global Users,eTNamespaceName=CommonObjects,dc=im,dc=eta");
env.put(Context.SECURITY_CREDENTIALS, "imsi00.!");
try {
// Create initial context
DirContext ctx = new InitialDirContext(env);
System.out.println("==========="+ctx.lookup("dc=im,dc=eta"));
SearchControls searchCtls = new SearchControls();
//Specify the attributes to return
String returnedAtts[]={"dn","objectClass"};
searchCtls.setReturningAttributes(returnedAtts);
//Specify the search scope
searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
//specify the LDAP search filter
String searchFilter = "(objectClass=eTNamespace)";
//Specify the Base for the search
String searchBase = "dc=im,dc=eta";
//initialize counter to total the results
int totalResults = 0;
// Search for objects using the filter
NamingEnumeration answer = ctx.search(searchBase, searchFilter, searchCtls);
//Loop through the search results
while (answer.hasMoreElements()) {
SearchResult sr = (SearchResult)answer.next();
totalResults++;
System.out.println(">>>" + sr.getName());
// Print out some of the attributes, catch the exception if the attributes have no values
Attributes attrs = sr.getAttributes();
if (attrs != null) {
try {
System.out.println(" surname: " + attrs.get("dn").get());
System.out.println(" firstname: " + attrs.get("objectClass").get());
}
catch (NullPointerException e) {
System.out.println("Errors listing attributes: " + e);
}
}
}
System.out.println("Total results: " + totalResults);
ctx.close();
// ... do something useful with ctx
// Close the context when we're done
ctx.close();
} catch (NamingException e) {
e.printStackTrace();
}
}
}
'Progamming > LDAP' 카테고리의 다른 글
[LDAP SSL]Connection (0) | 2012.12.04 |
---|---|
[LDAP SSL]InstallCert (0) | 2012.12.04 |
[LDAP SSL]인증서 생성 (0) | 2012.12.04 |