Sorry if I don't get the exact phrases here, I'm pretty new to this area...
I'm using Spring LDAP to authenticate/validate users. I want to be able to get notification sfrom LDAP once there have been changes like removing or updating users.
I know I can have something like a scheduled task that will run and will check the specific users or groups i'm interested in, but I'm looking for something that gives me unsolicited notifications.
I looked online and found the following:
http://docs.oracle.com/javase/tutorial/jndi/ldap/unsol.html
and this looks promising, but i don't understand how to use it, plus I don't think spring really supports it and that i'll have to use the jndi classes, like in the attached link.
Also, it looks like the only notifications I will get are Notice of disconnection: https://www.rfc-editor.org/rfc/rfc4511#section-4.4 is that true?
And finally, I used the sample code I found, but I didn't get any notification from my AD server, is it because I'll only get notification about disconnection, or is there a setting i need to set in AD to enable these notifications?
Here is the sample code. I tried several DNs for the lookup and the ctx.addNamingListener
, but maybe someone has a better idea for what I need to use there.
class RegUnsol {
public static void main(String[] args) {
// Set up environment for creating initial context
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://MY_AD_IP");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL,
"CN=Administrator,CN=Users,DC=sanity,DC=local");
env.put(Context.SECURITY_CREDENTIALS, "SOME_PASSWORD");
try {
// Get event context for registering listener
EventContext ctx = new InitialContext(env)
.lookup("CN=Users,DC=sanity,DC=local");
// Create listener
NamingListener listener = new UnsolListener();
// Register listener with context (all targets equivalent)
ctx.addNamingListener("CN=Users,DC=sanity,DC=local",
EventContext.ONELEVEL_SCOPE, listener);
// Wait 1 minutes for listener to receive events
try {
for (int i = 0; i < 5; i++) {
Thread.sleep(60000);
}
} catch (InterruptedException e) {
System.out.println("sleep interrupted");
}
// Not strictly necessary if we're going to close context anyhow
ctx.removeNamingListener(listener);
// Close context when we're done
ctx.close();
} catch (NamingException e) {
e.printStackTrace();
}
}
/**
* A sample UnsolicitedNotificationListener.
*/
static class UnsolListener implements UnsolicitedNotificationListener {
@Override
public void notificationReceived(UnsolicitedNotificationEvent evt) {
System.out.println("received: " + evt);
}
@Override
public void namingExceptionThrown(NamingExceptionEvent evt) {
System.out.println(">>> UnsolListener got an exception");
evt.getException().printStackTrace();
}
}
}