Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
A basic ldap example so I don't have to keep looking this up
import javax.naming.Context
* 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 =
username =
pass =
searchScope =
usernameAttribute = "sAMAccountName"
SearchControls searchControls = new SearchControls()
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 =, filter, searchControls)
//result.each {
// it.getAttributes().get("memberOf").all.each {
// println it
// }
//user info
result.each {
it.getAttributes().all.each {
println it
Copy link

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