List of usage examples for javax.naming.ldap InitialLdapContext InitialLdapContext
@SuppressWarnings("unchecked") public InitialLdapContext(Hashtable<?, ?> environment, Control[] connCtls) throws NamingException
From source file:org.nuxeo.ecm.directory.ldap.LDAPSession.java
@Override public boolean authenticate(String username, String password) throws DirectoryException { if (password == null || "".equals(password.trim())) { // never use anonymous bind as a way to authenticate a user in // Nuxeo EP return false; }//w w w.java 2 s .c o m // lookup the user: fetch its dn SearchResult entry; try { entry = getLdapEntry(username); } catch (NamingException e) { throw new DirectoryException("failed to fetch the ldap entry for " + username, e); } if (entry == null) { // no such user => authentication failed return false; } String dn = entry.getNameInNamespace(); Properties env = (Properties) getDirectory().getContextProperties().clone(); env.put(Context.SECURITY_PRINCIPAL, dn); env.put(Context.SECURITY_CREDENTIALS, password); InitialLdapContext authenticationDirContext = null; try { // creating a context does a bind log.debug(String.format("LDAP bind dn='%s'", dn)); // noinspection ResultOfObjectAllocationIgnored authenticationDirContext = new InitialLdapContext(env, null); // force reconnection to prevent from using a previous connection // with an obsolete password (after an user has changed his // password) authenticationDirContext.reconnect(null); log.debug("Bind succeeded, authentication ok"); return true; } catch (NamingException e) { log.debug("Bind failed: " + e.getMessage()); // authentication failed return false; } finally { try { if (authenticationDirContext != null) { authenticationDirContext.close(); } } catch (NamingException e) { log.error("Error closing authentication context when biding dn " + dn, e); return false; } } }
From source file:org.nuxeo.ecm.directory.ldap.MockLdapServer.java
public void shutdownLdapServer() { Hashtable<String, Object> env = new Hashtable<>(new ShutdownConfiguration().toJndiEnvironment()); env.put(Context.INITIAL_CONTEXT_FACTORY, CoreContextFactory.class.getName()); env.put(Context.PROVIDER_URL, BASE_DN); try {/*from w w w . ja v a2 s . c o m*/ new InitialLdapContext(env, null); } catch (Exception e) { throw new ServerSystemPreferenceException("Failed to shutdown ldap server.", e); } }
From source file:org.olat.ldap.LDAPLoginManagerImpl.java
/** * Connect to the LDAP server with System DN and Password Configuration: LDAP URL = olatextconfig.xml (property=ldapURL) System DN = olatextconfig.xml * (property=ldapSystemDN) System PW = olatextconfig.xml (property=ldapSystemPW) * // w w w .j a va2 s .c o m * @return The LDAP connection (LdapContext) or NULL if connect fails * @throws NamingException */ public LdapContext bindSystem() { // set LDAP connection attributes final Hashtable<String, String> env = new Hashtable<String, String>(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, LDAPLoginModule.getLdapUrl()); env.put(Context.SECURITY_AUTHENTICATION, "simple"); env.put(Context.SECURITY_PRINCIPAL, LDAPLoginModule.getLdapSystemDN()); env.put(Context.SECURITY_CREDENTIALS, LDAPLoginModule.getLdapSystemPW()); // check ssl if (LDAPLoginModule.isSslEnabled()) { enableSSL(env); } try { final InitialLdapContext ctx = new InitialLdapContext(env, new Control[] {}); ctx.getConnectControls(); return ctx; } catch (final NamingException e) { logError("NamingException when trying to bind system with DN::" + LDAPLoginModule.getLdapSystemDN() + " and PW::" + LDAPLoginModule.getLdapSystemPW() + " on URL::" + LDAPLoginModule.getLdapUrl(), e); return null; } catch (final Exception e) { logError("Exception when trying to bind system with DN::" + LDAPLoginModule.getLdapSystemDN() + " and PW::" + LDAPLoginModule.getLdapSystemPW() + " on URL::" + LDAPLoginModule.getLdapUrl(), e); return null; } }
From source file:org.olat.ldap.LDAPLoginManagerImpl.java
/** * Connect to LDAP with the User-Name and Password given as parameters Configuration: LDAP URL = olatextconfig.xml (property=ldapURL) LDAP Base = olatextconfig.xml * (property=ldapBase) LDAP Attributes Map = olatextconfig.xml (property=userAttrs) * // w ww .j a va 2s. c o m * @param uid The users LDAP login name (can't be null) * @param pwd The users LDAP password (can't be null) * @return After successful bind Attributes otherwise NULL * @throws NamingException */ public Attributes bindUser(final String uid, final String pwd, final LDAPError errors) { // get user name, password and attributes final String ldapUrl = LDAPLoginModule.getLdapUrl(); final String[] userAttr = LDAPLoginModule.getUserAttrs(); if (uid == null || pwd == null) { if (isLogDebugEnabled()) { logDebug("Error when trying to bind user, missing username or password. Username::" + uid + " pwd::" + pwd); } errors.insert("Username and password must be selected"); return null; } final LdapContext ctx = bindSystem(); if (ctx == null) { errors.insert("LDAP connection error"); return null; } final String userDN = searchUserDN(uid, ctx); if (userDN == null) { logInfo("Error when trying to bind user with username::" + uid + " - user not found on LDAP server" + (LDAPLoginModule.isCacheLDAPPwdAsOLATPwdOnLogin() ? ", trying with OLAT login provider" : "")); errors.insert("Username or password incorrect"); return null; } // Ok, so far so good, user exists. Now try to fetch attributes using the // users credentials final Hashtable<String, String> env = new Hashtable<String, String>(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, ldapUrl); env.put(Context.SECURITY_AUTHENTICATION, "simple"); env.put(Context.SECURITY_PRINCIPAL, userDN); env.put(Context.SECURITY_CREDENTIALS, pwd); if (LDAPLoginModule.isSslEnabled()) { enableSSL(env); } try { final Control[] connectCtls = new Control[] {}; final LdapContext userBind = new InitialLdapContext(env, connectCtls); final Attributes attributes = userBind.getAttributes(userDN, userAttr); userBind.close(); return attributes; } catch (final AuthenticationException e) { logInfo("Error when trying to bind user with username::" + uid + " - invalid LDAP password"); errors.insert("Username or password incorrect"); return null; } catch (final NamingException e) { logError("NamingException when trying to get attributes after binding user with username::" + uid, e); errors.insert("Username or password incorrect"); return null; } }
From source file:org.olat.ldap.manager.LDAPLoginManagerImpl.java
/** * Connect to the LDAP server with System DN and Password * /* w w w. jav a2 s . c o m*/ * Configuration: LDAP URL = ldapContext.xml (property=ldapURL) System DN = * ldapContext.xml (property=ldapSystemDN) System PW = ldapContext.xml * (property=ldapSystemPW) * * @return The LDAP connection (LdapContext) or NULL if connect fails * * @throws NamingException */ public LdapContext bindSystem() { // set LDAP connection attributes Hashtable<String, String> env = new Hashtable<String, String>(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, ldapLoginModule.getLdapUrl()); env.put(Context.SECURITY_AUTHENTICATION, "simple"); env.put(Context.SECURITY_PRINCIPAL, ldapLoginModule.getLdapSystemDN()); env.put(Context.SECURITY_CREDENTIALS, ldapLoginModule.getLdapSystemPW()); if (ldapLoginModule.getLdapConnectionTimeout() != null) { env.put(TIMEOUT_KEY, ldapLoginModule.getLdapConnectionTimeout().toString()); } // check ssl if (ldapLoginModule.isSslEnabled()) { enableSSL(env); } try { InitialLdapContext ctx = new InitialLdapContext(env, new Control[] {}); ctx.getConnectControls(); return ctx; } catch (NamingException e) { log.error("NamingException when trying to bind system with DN::" + ldapLoginModule.getLdapSystemDN() + " and PW::" + ldapLoginModule.getLdapSystemPW() + " on URL::" + ldapLoginModule.getLdapUrl(), e); return null; } catch (Exception e) { log.error("Exception when trying to bind system with DN::" + ldapLoginModule.getLdapSystemDN() + " and PW::" + ldapLoginModule.getLdapSystemPW() + " on URL::" + ldapLoginModule.getLdapUrl(), e); return null; } }
From source file:org.olat.ldap.manager.LDAPLoginManagerImpl.java
/** * //w w w. j a va 2s .c o m * Connect to LDAP with the User-Name and Password given as parameters * * Configuration: LDAP URL = ldapContext.xml (property=ldapURL) LDAP Base = * ldapContext.xml (property=ldapBase) LDAP Attributes Map = * ldapContext.xml (property=userAttrs) * * * @param uid The users LDAP login name (can't be null) * @param pwd The users LDAP password (can't be null) * * @return After successful bind Attributes otherwise NULL * * @throws NamingException */ @Override public Attributes bindUser(String uid, String pwd, LDAPError errors) { // get user name, password and attributes String ldapUrl = ldapLoginModule.getLdapUrl(); String[] userAttr = syncConfiguration.getUserAttributes(); if (uid == null || pwd == null) { if (log.isDebug()) log.debug("Error when trying to bind user, missing username or password. Username::" + uid + " pwd::" + pwd); errors.insert("Username and password must be selected"); return null; } LdapContext ctx = bindSystem(); if (ctx == null) { errors.insert("LDAP connection error"); return null; } String userDN = ldapDao.searchUserDN(uid, ctx); if (userDN == null) { log.info("Error when trying to bind user with username::" + uid + " - user not found on LDAP server" + (ldapLoginModule.isCacheLDAPPwdAsOLATPwdOnLogin() ? ", trying with OLAT login provider" : "")); errors.insert("Username or password incorrect"); return null; } // Ok, so far so good, user exists. Now try to fetch attributes using the // users credentials Hashtable<String, String> env = new Hashtable<String, String>(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, ldapUrl); env.put(Context.SECURITY_AUTHENTICATION, "simple"); env.put(Context.SECURITY_PRINCIPAL, userDN); env.put(Context.SECURITY_CREDENTIALS, pwd); if (ldapLoginModule.getLdapConnectionTimeout() != null) { env.put(TIMEOUT_KEY, ldapLoginModule.getLdapConnectionTimeout().toString()); } if (ldapLoginModule.isSslEnabled()) { enableSSL(env); } try { Control[] connectCtls = new Control[] {}; LdapContext userBind = new InitialLdapContext(env, connectCtls); Attributes attributes = userBind.getAttributes(userDN, userAttr); userBind.close(); return attributes; } catch (AuthenticationException e) { log.info("Error when trying to bind user with username::" + uid + " - invalid LDAP password"); errors.insert("Username or password incorrect"); return null; } catch (NamingException e) { log.error("NamingException when trying to get attributes after binding user with username::" + uid, e); errors.insert("Username or password incorrect"); return null; } }
From source file:org.openiam.idm.srvc.synch.service.generic.LdapAdapterForGenericObject.java
private boolean connect(SynchConfig config) throws NamingException { Hashtable<String, String> envDC = new Hashtable(); System.setProperty("javax.net.ssl.trustStore", keystore); String hostUrl = config.getSrcHost(); // managedSys.getHostUrl(); log.debug("Directory host url:" + hostUrl); envDC.put(Context.PROVIDER_URL, hostUrl); envDC.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); envDC.put(Context.SECURITY_AUTHENTICATION, "simple"); // simple envDC.put(Context.SECURITY_PRINCIPAL, config.getSrcLoginId()); // "administrator@diamelle.local" envDC.put(Context.SECURITY_CREDENTIALS, config.getSrcPassword()); if (hostUrl.contains("ldaps")) { envDC.put(Context.SECURITY_PROTOCOL, "SSL"); }/*from w ww.j ava2 s . com*/ ctx = new InitialLdapContext(envDC, null); if (ctx != null) { return true; } return false; }
From source file:org.openiam.spml2.spi.ldap.LdapConnectorImpl.java
public LdapContext connect(String userName, String password) { //LdapContext ctxLdap = null; Hashtable<String, String> envDC = new Hashtable(); //keystore = secres.getString("KEYSTORE"); System.setProperty("javax.net.ssl.trustStore", keystore); log.debug("Connecting to ldap using principal=" + userName); //envDC.put(Context.PROVIDER_URL,host); envDC.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); envDC.put(Context.SECURITY_AUTHENTICATION, "simple"); // simple envDC.put(Context.SECURITY_PRINCIPAL, userName); //"administrator@diamelle.local" envDC.put(Context.SECURITY_CREDENTIALS, password); // if (protocol != null && protocol.equalsIgnoreCase("SSL")) { // envDC.put(Context.SECURITY_PROTOCOL, protocol); // }/*from www . j av a 2s . co m*/ try { return (new InitialLdapContext(envDC, null)); } catch (NamingException ne) { log.error(ne.getMessage()); } return null; }
From source file:org.projectforge.business.ldap.LdapConnector.java
public LdapContext createContext() { init();//from www .j ava 2 s .co m final Hashtable<String, String> env; final String authentication = ldapConfig.getAuthentication(); if ("none".equals(authentication) == false) { env = createEnv(ldapConfig.getManagerUser(), ldapConfig.getManagerPassword()); } else { env = createEnv(null, null); } try { final LdapContext ctx = new InitialLdapContext(env, null); return ctx; } catch (final NamingException ex) { log.error("While trying to connect LDAP initally: " + ex.getMessage(), ex); throw new RuntimeException(ex); } }
From source file:org.projectforge.business.ldap.LdapConnector.java
public LdapContext createContext(final String username, final String password) throws NamingException { init();//from w w w. j a v a 2s . c o m final Hashtable<String, String> env = createEnv(username, password); final LdapContext ctx = new InitialLdapContext(env, null); return ctx; }