JNDI API

這裡主要記載我在安裝測試機器時,所做的API存取嘗試。

Hashtable<String, Object> env = new Hashtable<String, Object>();
env.put(Context.INITIAL_CONTEXT_FACTORY,
	"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL,
	"ldap://192.168.1.13:389/dc=testldap,dc=org");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, "cn=admin,dc=testldap,dc=org");
env.put(Context.SECURITY_CREDENTIALS, "123456");
 
DirContext ctx = null;
try {
	ctx = new InitialDirContext(env);
 
	NamingEnumeration<SearchResult> srs = ctx.search("", null);
	while (srs.hasMore()) {
		SearchResult sr = srs.next();
		System.out.println(sr);
	}
} catch (NamingException e) {
	throw new RuntimeException(e);
} finally {
	closeDirContext(ctx);
}

Output:

cn=admin: null:null:{userpassword=userPassword: [B@677327b6, description=description: LDAP administrator, objectclass=objectClass: simpleSecurityObject, organizationalRole, cn=cn: admin}
ou=sw: null:null:{ou=ou: sw, objectclass=objectClass: top, organizationalUnit}
DirContext ctx = new InitialDirContext();
try {
	Attributes attrs = ctx.getAttributes("ldap://10.134.15.131:389/", new String[]{"supportedSASLMechanisms"});
	System.out.println(attrs);
} finally {
	closeDirContext(ctx);
}

Output:

{supportedsaslmechanisms=supportedSASLMechanisms: DIGEST-MD5, CRAM-MD5, NTLM}