Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A basic ldap example so I don't have to keep looking this up
import javax.naming.Context
import javax.naming.directory.InitialDirContext
import javax.naming.directory.SearchControls
/**
* Created with IntelliJ IDEA on 7/18/13
* @author Tommy Barker
*/
def config = new ConfigSlurper().parse(new File("${System.getProperty("user.home")}/.metridoc/MetridocConfig.groovy").toURI().toURL())
url = config.ldap.server.url
searchBase = config.ldap.search.base
username = config.ldap.search.user
pass = config.ldap.search.pass
searchScope = config.ldap.search.scope
usernameAttribute = "sAMAccountName"
SearchControls searchControls = new SearchControls()
searchControls.setSearchScope(searchScope)
def env = new Hashtable()
env[Context.INITIAL_CONTEXT_FACTORY] = "com.sun.jndi.ldap.LdapCtxFactory"
// Non-anonymous access for the search.
env[Context.SECURITY_AUTHENTICATION] = "simple"
env[Context.SECURITY_PRINCIPAL] = username
env[Context.SECURITY_CREDENTIALS] = pass
env[Context.PROVIDER_URL] = url
ctx = new InitialDirContext(env)
String filter = "($usernameAttribute=tbarker)"
def result = ctx.search(searchBase, filter, searchControls)
//groups
//result.each {
// it.getAttributes().get("memberOf").all.each {
// println it
// }
//}
//user info
result.each {
it.getAttributes().all.each {
println it
}
}
@tbarker9
Copy link
Author

tbarker9 commented Jul 18, 2013

Use for Active Directory ldap. Put this here so I don't have to keep on figuring out how to use this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment