use of javax.naming.directory.InitialDirContext in project iaf by ibissource.
the class LdapSender method loopkupDirContext.
/**
* Retrieves the DirContext from the JNDI environment and sets the <code>providerURL</code> back to <code>ldapProviderURL</code> if specified.
* @throws ParameterException
*/
protected synchronized DirContext loopkupDirContext(Map paramValueMap) throws NamingException, ParameterException {
DirContext dirContext;
if (jndiEnv == null) {
Hashtable newJndiEnv = getJndiEnv();
// newJndiEnv.put("com.sun.jndi.ldap.trace.ber", System.err);//ldap response in log for debug purposes
if (getLdapProviderURL() != null) {
// Overwriting the (realm)providerURL if specified in configuration
newJndiEnv.put("java.naming.provider.url", getLdapProviderURL());
}
if (principalParameterFound) {
newJndiEnv.put(Context.SECURITY_PRINCIPAL, paramValueMap.get("principal"));
newJndiEnv.put(Context.SECURITY_CREDENTIALS, paramValueMap.get("credentials"));
}
if (isUsePooling()) {
// Enable connection pooling
newJndiEnv.put("com.sun.jndi.ldap.connect.pool", "true");
// see http://java.sun.com/products/jndi/tutorial/ldap/connect/config.html
// newJndiEnv.put("com.sun.jndi.ldap.connect.pool.maxsize", "20" );
// newJndiEnv.put("com.sun.jndi.ldap.connect.pool.prefsize", "10" );
// newJndiEnv.put("com.sun.jndi.ldap.connect.pool.timeout", "300000" );
} else {
// Disable connection pooling
newJndiEnv.put("com.sun.jndi.ldap.connect.pool", "false");
}
if (log.isDebugEnabled())
log.debug("created environment for LDAP provider URL [" + newJndiEnv.get("java.naming.provider.url") + "]");
dirContext = new InitialDirContext(newJndiEnv);
if (!principalParameterFound) {
jndiEnv = newJndiEnv;
}
} else {
dirContext = new InitialDirContext(jndiEnv);
}
return dirContext;
// return (DirContext) dirContextTemplate.lookup(""); // return copy to be thread-safe
}
use of javax.naming.directory.InitialDirContext in project stdlib by petergeneric.
the class LDAPUserAuthenticationService method ldapAuthenticate.
private LDAPUserRecord ldapAuthenticate(String inputUsername, final String password) {
// Allow the user to provide domain-slash-user or user@domain in addition to bare "user" with an implied domain
final String username;
final String fullyQualifiedUsername;
if (inputUsername.indexOf('\\') > 0) {
final String[] segments = StringUtils.split(inputUsername, "\\", 2);
final String domain = segments[0];
// get bare username (discard everything before the slash)
username = segments[1];
fullyQualifiedUsername = username + "@" + domain;
} else if (inputUsername.indexOf('@') > 0) {
fullyQualifiedUsername = inputUsername;
// get bare username (discard everything after the @)
username = StringUtils.split(inputUsername, '@')[0];
} else {
// Implied domain
fullyQualifiedUsername = inputUsername + "@" + domain;
username = inputUsername;
}
try {
DirContext ldapContext = null;
try {
Hashtable<String, String> ldapEnv = new Hashtable<>();
ldapEnv.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
ldapEnv.put(Context.PROVIDER_URL, ldapEndpoint);
ldapEnv.put(Context.SECURITY_AUTHENTICATION, "simple");
ldapEnv.put(Context.SECURITY_PRINCIPAL, fullyQualifiedUsername);
ldapEnv.put(Context.SECURITY_CREDENTIALS, password);
// N.B. sometimes takes ~10 seconds
ldapContext = new InitialDirContext(ldapEnv);
final NamingEnumeration<SearchResult> answer;
{
SearchControls search = new SearchControls();
search.setSearchScope(SearchControls.SUBTREE_SCOPE);
search.setReturningAttributes(new String[] { "dn", "name", "samAccountName" });
final String searchFilter = String.format(this.ldapFilter, username);
answer = ldapContext.search(ldapSearchBase, searchFilter, search);
}
while (answer.hasMoreElements()) {
SearchResult sr = answer.next();
Attributes attrs = sr.getAttributes();
final String dn = sr.getNameInNamespace();
final String name = attrs.get("name").get().toString();
final String actualUsername = attrs.get("samAccountName").get().toString();
// Get the direct & indirect group membership data
List<LDAPGroup> groups = getGroups(ldapContext, dn);
return new LDAPUserRecord(actualUsername, name, groups);
}
return null;
} finally {
if (ldapContext != null)
ldapContext.close();
}
} catch (NamingException e) {
throw new RuntimeException("Error accessing LDAP server (incorrect username/password or server connection issue, please try again)", e);
}
}
use of javax.naming.directory.InitialDirContext in project activemq-artemis by apache.
the class SaslKrb5LDAPSecurityTest method testRunning.
@Test
public void testRunning() throws Exception {
Hashtable<String, String> env = new Hashtable<>();
env.put(Context.PROVIDER_URL, "ldap://localhost:1024");
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, PRINCIPAL);
env.put(Context.SECURITY_CREDENTIALS, CREDENTIALS);
DirContext ctx = new InitialDirContext(env);
HashSet<String> set = new HashSet<>();
NamingEnumeration<NameClassPair> list = ctx.list("ou=system");
while (list.hasMore()) {
NameClassPair ncp = list.next();
set.add(ncp.getName());
}
Assert.assertTrue(set.contains("uid=admin"));
Assert.assertTrue(set.contains("ou=users"));
Assert.assertTrue(set.contains("ou=groups"));
Assert.assertTrue(set.contains("ou=configuration"));
Assert.assertTrue(set.contains("prefNodeName=sysPrefRoot"));
ctx.close();
}
use of javax.naming.directory.InitialDirContext in project activemq-artemis by apache.
the class LDAPSecurityTest method testRunning.
@Test
public void testRunning() throws Exception {
Hashtable<String, String> env = new Hashtable<>();
env.put(Context.PROVIDER_URL, "ldap://localhost:1024");
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, PRINCIPAL);
env.put(Context.SECURITY_CREDENTIALS, CREDENTIALS);
DirContext ctx = new InitialDirContext(env);
HashSet<String> set = new HashSet<>();
NamingEnumeration<NameClassPair> list = ctx.list("ou=system");
while (list.hasMore()) {
NameClassPair ncp = list.next();
set.add(ncp.getName());
}
Assert.assertTrue(set.contains("uid=admin"));
Assert.assertTrue(set.contains("ou=users"));
Assert.assertTrue(set.contains("ou=groups"));
Assert.assertTrue(set.contains("ou=configuration"));
Assert.assertTrue(set.contains("prefNodeName=sysPrefRoot"));
ctx.close();
}
use of javax.naming.directory.InitialDirContext in project activemq-artemis by apache.
the class LegacyLDAPSecuritySettingPluginTest method testRunning.
@Test
public void testRunning() throws Exception {
Hashtable<String, String> env = new Hashtable<>();
env.put(Context.PROVIDER_URL, "ldap://localhost:1024");
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, PRINCIPAL);
env.put(Context.SECURITY_CREDENTIALS, CREDENTIALS);
DirContext ctx = new InitialDirContext(env);
HashSet<String> set = new HashSet<>();
NamingEnumeration<NameClassPair> list = ctx.list("ou=system");
while (list.hasMore()) {
NameClassPair ncp = list.next();
set.add(ncp.getName());
}
Assert.assertTrue(set.contains("uid=admin"));
Assert.assertTrue(set.contains("ou=users"));
Assert.assertTrue(set.contains("ou=groups"));
Assert.assertTrue(set.contains("ou=configuration"));
Assert.assertTrue(set.contains("prefNodeName=sysPrefRoot"));
}
Aggregations