use of javax.naming.directory.InitialDirContext in project cachecloud by sohutv.
the class LoginUtil method passportCheck.
public static boolean passportCheck(String username, String password) {
//LDAP登陆地址
String ldapUrl = ConstUtils.LDAP_URL;
if (StringUtils.isBlank(ldapUrl)) {
logger.warn("ldap url is empty!!");
return true;
}
if (ConstUtils.IS_DEBUG) {
logger.warn("isDebug=true return");
return true;
}
Hashtable<String, String> env = new Hashtable<String, String>();
env.put("java.naming.factory.initial", "com.sun.jndi.ldap.LdapCtxFactory");
env.put("java.naming.provider.url", ldapUrl);
env.put("java.naming.security.authentication", "simple");
env.put("java.naming.security.principal", username + ConstUtils.EMAIL_SUFFIX);
env.put("java.naming.security.credentials", password);
DirContext ctx = null;
try {
ctx = new InitialDirContext(env);
if (ctx != null) {
return true;
}
} catch (Exception e) {
logger.error("username {} passportCheck: " + e.getMessage(), username, e);
} finally {
if (ctx != null) {
try {
ctx.close();
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
}
}
return false;
}
use of javax.naming.directory.InitialDirContext in project OpenAM by OpenRock.
the class Step4 method getLdapHostAndPort.
// Method to get hostname and port number with the
// provided Domain Name for Active Directory user data store.
private String[] getLdapHostAndPort(String domainName) throws NamingException, IOException {
if (!domainName.endsWith(".")) {
domainName += '.';
}
DirContext ictx = null;
// The resource record type A is defined in RFC 1035.
try {
Hashtable env = new Hashtable();
env.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.dns.DnsContextFactory");
ictx = new InitialDirContext(env);
Attributes attributes = ictx.getAttributes(domainName, new String[] { "A" });
Attribute attrib = attributes.get("A");
if (attrib == null) {
throw new NamingException();
}
} catch (NamingException e) {
// throw exception.
throw e;
}
// then look for the LDAP server
String serverHostName = null;
String serverPortStr = null;
final String ldapServer = "_ldap._tcp." + domainName;
try {
// Attempting to resolve ldapServer to SRV record.
// This is a mechanism defined in MSDN, querying
// SRV records for _ldap._tcp.DOMAINNAME.
// and get host and port from domain.
Attributes attributes = ictx.getAttributes(ldapServer, new String[] { "SRV" });
Attribute attr = attributes.get("SRV");
if (attr == null) {
throw new NamingException();
}
String[] srv = attr.get().toString().split(" ");
String hostNam = srv[3];
serverHostName = hostNam.substring(0, hostNam.length() - 1);
if ((serverHostName != null) && serverHostName.length() > 0) {
getContext().setSessionAttribute(SessionAttributeNames.USER_STORE_HOST, serverHostName);
}
serverPortStr = srv[2];
} catch (NamingException e) {
// throw exception.
throw e;
}
// try to connect to LDAP port to make sure this machine
// has LDAP service
int serverPort = Integer.parseInt(serverPortStr);
if ((serverPort > 0) && (serverPort < 65535)) {
getContext().setSessionAttribute(SessionAttributeNames.USER_STORE_PORT, serverPortStr);
}
try {
new Socket(serverHostName, serverPort).close();
} catch (IOException e) {
throw e;
}
String[] hostAndPort = new String[2];
hostAndPort[0] = serverHostName;
hostAndPort[1] = serverPortStr;
return hostAndPort;
}
use of javax.naming.directory.InitialDirContext in project wildfly by wildfly.
the class ExternalContextBindingTestCase method testWithActualLDAPContext.
private void testWithActualLDAPContext(boolean withCache) throws Exception {
InitialContext ctx = null;
InitialDirContext ldapContext1 = null;
InitialDirContext ldapContext2 = null;
try {
ctx = new InitialContext();
String initialDirContext = withCache ? "java:global/ldap-cache" : "java:global/ldap";
LOGGER.debug("looking up " + initialDirContext + " ....");
ldapContext1 = (InitialDirContext) ctx.lookup(initialDirContext);
ldapContext2 = (InitialDirContext) ctx.lookup(initialDirContext);
Assert.assertNotNull(ldapContext1);
Assert.assertNotNull(ldapContext2);
if (withCache) {
Assert.assertSame(ldapContext1, ldapContext2);
} else {
Assert.assertNotSame(ldapContext1, ldapContext2);
}
LOGGER.debug("acquired external LDAP context: " + ldapContext1.toString());
LdapCtx c = (LdapCtx) ldapContext1.lookup("dc=jboss,dc=org");
c = (LdapCtx) c.lookup("ou=People");
Attributes attributes = c.getAttributes("uid=jduke");
Assert.assertTrue(attributes.get("description").contains("awesome"));
// resource injection
LookupEjb ejb = (LookupEjb) ctx.lookup("java:module/LookupEjb");
Assert.assertNotNull(ejb);
c = ejb.getLdapCtx();
Assert.assertNotNull(c);
c = (LdapCtx) c.lookup("ou=People");
attributes = c.getAttributes("uid=jduke");
Assert.assertTrue(attributes.get("description").contains("awesome"));
} finally {
if (ctx != null) {
ctx.close();
}
if (ldapContext1 != null) {
ldapContext1.close();
}
if (ldapContext2 != null) {
ldapContext2.close();
}
}
}
use of javax.naming.directory.InitialDirContext in project Activiti by Activiti.
the class LDAPConnectionUtil method createDirectoryContext.
public static InitialDirContext createDirectoryContext(LDAPConfigurator ldapConfigurator, String principal, String credentials) {
Properties properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY, ldapConfigurator.getInitialContextFactory());
properties.put(Context.PROVIDER_URL, ldapConfigurator.getServer() + ":" + ldapConfigurator.getPort());
properties.put(Context.SECURITY_AUTHENTICATION, ldapConfigurator.getSecurityAuthentication());
properties.put(Context.SECURITY_PRINCIPAL, principal);
properties.put(Context.SECURITY_CREDENTIALS, credentials);
if (ldapConfigurator.getCustomConnectionParameters() != null) {
for (String customParameter : ldapConfigurator.getCustomConnectionParameters().keySet()) {
properties.put(customParameter, ldapConfigurator.getCustomConnectionParameters().get(customParameter));
}
}
InitialDirContext context;
try {
context = new InitialDirContext(properties);
} catch (NamingException e) {
LOGGER.warn("Could not create InitialDirContext for LDAP connection : " + e.getMessage());
throw new ActivitiException("Could not create InitialDirContext for LDAP connection : " + e.getMessage(), e);
}
return context;
}
use of javax.naming.directory.InitialDirContext in project Activiti by Activiti.
the class LDAPTemplate method execute.
public <T> T execute(LDAPCallBack<T> ldapCallBack) {
InitialDirContext initialDirContext = null;
try {
initialDirContext = LDAPConnectionUtil.creatDirectoryContext(ldapConfigurator);
} catch (Exception e) {
LOGGER.info("Could not create LDAP connection : " + e.getMessage(), e);
}
T result = ldapCallBack.executeInContext(initialDirContext);
LDAPConnectionUtil.closeDirectoryContext(initialDirContext);
return result;
}
Aggregations