Skip to content

Instantly share code, notes, and snippets.

@tbarker9
Last active December 19, 2015 23:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tbarker9/6032590 to your computer and use it in GitHub Desktop.
Save tbarker9/6032590 to your computer and use it in GitHub Desktop.
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

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