use of javax.naming.directory.InitialDirContext in project uPortal by Jasig.
the class LDAPGroupStore method getConnection.
protected DirContext getConnection() {
// JNDI boilerplate to connect to an initial context
DirContext context = (DirContext) contexts.get("context");
if (context == null) {
Hashtable jndienv = new Hashtable();
jndienv.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
jndienv.put(Context.SECURITY_AUTHENTICATION, "simple");
if (url.startsWith("ldaps")) {
// Handle SSL connections
String newurl = url.substring(0, 4) + url.substring(5);
jndienv.put(Context.SECURITY_PROTOCOL, "ssl");
jndienv.put(Context.PROVIDER_URL, newurl);
} else {
jndienv.put(Context.PROVIDER_URL, url);
}
if (logonid != null)
jndienv.put(Context.SECURITY_PRINCIPAL, logonid);
if (logonpassword != null)
jndienv.put(Context.SECURITY_CREDENTIALS, logonpassword);
try {
context = new InitialDirContext(jndienv);
} catch (NamingException nex) {
log.error("LDAPGroupStore: unable to get context", nex);
}
contexts.put("context", context);
}
return context;
}
use of javax.naming.directory.InitialDirContext in project platformlayer by platformlayer.
the class ITOpenLdapService method testLdap.
private void testLdap(String ldapUrl, Secret adminPassword) throws NamingException {
Hashtable<String, String> env = new Hashtable<String, String>();
String sp = "com.sun.jndi.ldap.LdapCtxFactory";
env.put(Context.INITIAL_CONTEXT_FACTORY, sp);
env.put(Context.PROVIDER_URL, ldapUrl);
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, "cn=Manager,dc=test,dc=platformlayer,dc=org");
env.put(Context.SECURITY_CREDENTIALS, adminPassword.plaintext());
DirContext ctx = new InitialDirContext(env);
NamingEnumeration results = ctx.list("dc=test,dc=platformlayer,dc=org");
while (results.hasMore()) {
NameClassPair sr = (NameClassPair) results.next();
System.out.println(sr.getNameInNamespace());
}
ctx.close();
}
use of javax.naming.directory.InitialDirContext in project new-cloud by xie-summer.
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 OpenClinica by OpenClinica.
the class SystemController method getLdapModule.
public HashMap<String, Object> getLdapModule(StudyBean studyBean) {
String enabled = CoreResources.getField("ldap.enabled");
String ldapHost = CoreResources.getField("ldap.host");
String username = CoreResources.getField("ldap.userDn");
String password = CoreResources.getField("ldap.password");
String result = "";
Properties env = new Properties();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, ldapHost);
env.put(Context.SECURITY_AUTHENTICATION, "simple");
// replace with user DN
env.put(Context.SECURITY_PRINCIPAL, username);
env.put(Context.SECURITY_CREDENTIALS, password);
DirContext ctx = null;
try {
ctx = new InitialDirContext(env);
result = "ACTIVE";
} catch (Exception e) {
result = "INACTIVE";
}
HashMap<String, String> mapMetadata = new HashMap<>();
mapMetadata.put("ldap.host", ldapHost);
HashMap<String, Object> mapWebService = new HashMap<>();
mapWebService.put("enabled", enabled.equalsIgnoreCase("true") ? "True" : "False");
mapWebService.put("status", result);
mapWebService.put("metadata", mapMetadata);
HashMap<String, Object> mapModule = new HashMap<>();
mapModule.put("Ldap", mapWebService);
return mapModule;
}
use of javax.naming.directory.InitialDirContext in project CzechIdMng by bcvsolutions.
the class AdUserConnectorType method getCertificateFromAD.
/**
* Try to find authority for this certificate in AD (we want to return certificate with the biggest validity).
*/
private X509Certificate getCertificateFromAD(X509Certificate serverCrt, String port, String host, String user, String password) {
X509Certificate resultCertificate = null;
DirContext ldapContext = null;
try {
// Init LDAP context.
Hashtable<String, String> ldapEnv = getAdEnvironment(host, "389", user, password, false);
ldapContext = new InitialDirContext(ldapEnv);
boolean continueSearching = true;
resultCertificate = serverCrt;
while (continueSearching) {
X509Certificate authorityCrtOnAD = findAuthorityCrtOnAD(resultCertificate, ldapContext);
if (authorityCrtOnAD != null) {
// Validate certificate by found authority.
try {
CertificateUtils.verifyCertificate(resultCertificate, authorityCrtOnAD);
} catch (CertificateException ex) {
throw new ResultCodeException(AccResultCode.WIZARD_AD_CONNECTOR_CRT_NOT_TRUSTED, ImmutableMap.of("serialNumber", authorityCrtOnAD.getSerialNumber().toString(16).toUpperCase()), ex);
}
}
if (authorityCrtOnAD == null) {
// No authority certificate was found, previous certificate is result.
continueSearching = false;
} else if (authorityCrtOnAD.getIssuerDN() != null && resultCertificate.getIssuerDN() != null && resultCertificate.getIssuerDN().getName().equals(authorityCrtOnAD.getIssuerDN().getName())) {
// Issuer name in previous and returned authority certificate is same -> returned certificate is result.
resultCertificate = authorityCrtOnAD;
continueSearching = false;
} else if (authorityCrtOnAD.getIssuerDN() == null || Strings.isBlank(authorityCrtOnAD.getIssuerDN().getName())) {
// Found authority certificate doesn't have issuer -> it is result.
resultCertificate = authorityCrtOnAD;
continueSearching = false;
} else {
// Next round.
resultCertificate = authorityCrtOnAD;
}
}
} catch (CommunicationException ex) {
throw new ResultCodeException(AccResultCode.WIZARD_AD_COMMUNICATION_EXCEPTION, ImmutableMap.of("host", host), ex);
} catch (NamingException ex) {
throw new ResultCodeException(AccResultCode.WIZARD_AD_OPERATION_FAILED, ImmutableMap.of("dn", serverCrt != null ? serverCrt.getSubjectDN().getName() : host), ex);
} catch (CertificateException ex) {
throw new CoreException(ex.getLocalizedMessage(), ex);
} finally {
if (ldapContext != null) {
try {
ldapContext.close();
} catch (NamingException e) {
// Only log it.
LOG.error(e.getLocalizedMessage(), e);
}
}
}
return resultCertificate;
}
Aggregations