下面列出了怎么用javax.naming.event.EventDirContext的API类实例代码及写法,或者点击链接到github查看源代码。
public void ldapInjectionSunApi(String input) throws NamingException {
//Stub instances
Properties props = new Properties();
props.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
props.put(Context.PROVIDER_URL, "ldap://ldap.example.com");
props.put(Context.REFERRAL, "ignore");
SearchControls ctrls = new SearchControls();
ctrls.setReturningAttributes(new String[]{"givenName", "sn"});
ctrls.setSearchScope(SearchControls.SUBTREE_SCOPE);
//Two context instances mostly usable with sun specific API
LdapCtx context5 = null;
EventDirContext context6 = null; //LdapCtx is the only known class to implements to this interface
NamingEnumeration<SearchResult> answers;
answers = context5.search(new LdapName("dc=People,dc=example,dc=com"), "(uid=" + input + ")", ctrls);
answers = context5.search(new LdapName("dc=People,dc=example,dc=com"), "(uid=" + input + ")", new Object[0], ctrls);
answers = context5.search("dc=People,dc=example,dc=com", "(uid=" + input + ")", ctrls);
answers = context5.search("dc=People,dc=example,dc=com", "(uid=" + input + ")", new Object[0], ctrls);
answers = context6.search(new LdapName("dc=People,dc=example,dc=com"), "(uid=" + input + ")", ctrls);
answers = context6.search(new LdapName("dc=People,dc=example,dc=com"), "(uid=" + input + ")", new Object[0], ctrls);
answers = context6.search("dc=People,dc=example,dc=com", "(uid=" + input + ")", ctrls);
answers = context6.search("dc=People,dc=example,dc=com", "(uid=" + input + ")", new Object[0], ctrls);
}
protected void open() throws NamingException {
if (isContextAlive()) {
return;
}
context = createContext();
eventContext = ((EventDirContext) context.lookup(""));
SearchControls searchControls = new SearchControls();
searchControls.setReturningAttributes(new String[]{roleAttribute});
searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
if (enableListener) {
eventContext.addNamingListener(destinationBase, filter, searchControls, new LDAPNamespaceChangeListener());
}
}
private EventDirContext getEventDirContext(String name) throws NamingException {
EventContext eventContext = getEventContext(name);
if (eventContext instanceof EventDirContext) {
return (EventDirContext) eventContext;
}
throw new NamingException("The given Context is not an instance of "
+ EventDirContext.class.getName());
}
private EventDirContext getEventDirContext(Name name) throws NamingException {
return getEventDirContext(name.get(0));
}