Last active
December 19, 2015 23:09
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use for Active Directory ldap. Put this here so I don't have to keep on figuring out how to use this.