Example usage for javax.naming Context SECURITY_PRINCIPAL

List of usage examples for javax.naming Context SECURITY_PRINCIPAL

Introduction

In this page you can find the example usage for javax.naming Context SECURITY_PRINCIPAL.

Prototype

String SECURITY_PRINCIPAL

To view the source code for javax.naming Context SECURITY_PRINCIPAL.

Click Source Link

Document

Constant that holds the name of the environment property for specifying the identity of the principal for authenticating the caller to the service.

Usage

From source file:org.projectforge.business.ldap.LdapConnector.java

private Hashtable<String, String> createEnv(final String user, final String password) {
    // Set up the environment for creating the initial context
    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, ldapConfig.getCompleteServerUrl());
    final String authentication = ldapConfig.getAuthentication();
    if (StringUtils.isNotBlank(authentication) == true) {
        env.put(Context.SECURITY_AUTHENTICATION, ldapConfig.getAuthentication());
        if ("none".equals(authentication) == false && user != null && password != null) {
            env.put(Context.SECURITY_PRINCIPAL, user);
            env.put(Context.SECURITY_CREDENTIALS, password);
        }/*from  w  ww. ja  v a2  s.com*/
    }
    if (ldapConfig != null && StringUtils.isNotBlank(ldapConfig.getSslCertificateFile()) == true) {
        env.put("java.naming.ldap.factory.socket", "org.projectforge.business.ldap.MySSLSocketFactory");
    }
    log.info("Trying to connect the LDAP server: url=[" + ldapConfig.getCompleteServerUrl()
            + "], authentication=[" + ldapConfig.getAuthentication() + "], principal=[" + user + "]");
    return env;
}

From source file:org.apache.directory.server.core.jndi.LdapJndiPropertiesTest.java

License:asdf

@Test
public void testAuthWithNoCredsEnv() throws Exception {
    Hashtable<String, Object> env = new Hashtable<String, Object>();
    env.put(Context.SECURITY_PRINCIPAL, "");
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    try {/*  w  w  w.j av a2  s.c  om*/
        LdapJndiProperties.getLdapJndiProperties(env);
        fail("should never get here");
    } catch (ConfigurationException e) {
    }
}

From source file:org.malaguna.cmdit.service.ldap.LDAPBase.java

public DirContext getDirContext() {
    DirContext ctx = null;/* w  ww.j  ava  2 s.  c o m*/
    String cadena = "uid=" + user + "," + context;
    Hashtable<String, String> entorno = new Hashtable<String, String>();

    entorno.put(Context.PROVIDER_URL, server);
    entorno.put(Context.SECURITY_PRINCIPAL, cadena);
    entorno.put(Context.SECURITY_CREDENTIALS, password);
    entorno.put(Context.INITIAL_CONTEXT_FACTORY, initContext);

    try {
        ctx = new InitialDirContext(entorno);
    } catch (NamingException e) {
        logger.error(messages.getMessage("err.ldap.attribute", new Object[] { e }, Locale.getDefault()));
    }

    return ctx;
}

From source file:de.sub.goobi.helper.ldap.Ldap.java

/**
 * create new user in LDAP-directory.//from   ww  w . ja v  a  2  s  .co  m
 *
 * @param inBenutzer
 *            User object
 * @param inPasswort
 *            String
 */
public void createNewUser(User inBenutzer, String inPasswort)
        throws NamingException, NoSuchAlgorithmException, IOException {

    if (!ConfigCore.getBooleanParameter("ldap_readonly", false)) {
        Hashtable<String, String> env = getLdapConnectionSettings();
        env.put(Context.SECURITY_PRINCIPAL, ConfigCore.getParameter("ldap_adminLogin"));
        env.put(Context.SECURITY_CREDENTIALS, ConfigCore.getParameter("ldap_adminPassword"));

        LdapUser dr = new LdapUser();
        dr.configure(inBenutzer, inPasswort, getNextUidNumber());
        DirContext ctx = new InitialDirContext(env);
        ctx.bind(getUserDN(inBenutzer), dr);
        ctx.close();
        setNextUidNumber();
        Helper.setMeldung(null, Helper.getTranslation("ldapWritten") + " "
                + serviceManager.getUserService().getFullName(inBenutzer), "");
        /*
         * check if HomeDir exists, else create it
         */
        logger.debug("HomeVerzeichnis pruefen");
        URI homePath = URI.create(getUserHomeDirectory(inBenutzer));
        if (!new File(homePath).exists()) {
            logger.debug("HomeVerzeichnis existiert noch nicht");
            serviceManager.getFileService().createDirectoryForUser(homePath, inBenutzer.getLogin());
            logger.debug("HomeVerzeichnis angelegt");
        } else {
            logger.debug("HomeVerzeichnis existiert schon");
        }
    } else {
        Helper.setMeldung(Helper.getTranslation("ldapIsReadOnly"));
    }
}

From source file:org.eclipselabs.etrack.util.security.ldap.impl.LdapService.java

void activate(Map<?, ?> configuration) throws NamingException {
    this.idSuffix = (String) configuration.get(CONFIG_ID_SUFFIX);
    this.url = (String) configuration.get(CONFIG_URL);
    this.baseDN = (String) configuration.get(CONFIG_BASE_DN);
    this.userSearchBase = (String) configuration.get(CONFIG_USER_SEARCH_BASE);
    this.userFilter = (String) configuration.get(CONFIG_USER_FILTER);

    String managerDN = (String) configuration.get(CONFIG_MANAGER_DN);
    String managerPassword = (String) configuration.get(CONFIG_MANAGER_PASSWORD);

    searchEnvironment = new Hashtable<String, String>();
    searchEnvironment.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    searchEnvironment.put(Context.PROVIDER_URL, url);

    if (managerDN != null) {
        searchEnvironment.put(Context.SECURITY_AUTHENTICATION, "simple");
        searchEnvironment.put(Context.SECURITY_PRINCIPAL, managerDN);
        searchEnvironment.put(Context.SECURITY_CREDENTIALS, managerPassword);
    } else/*from  w  w w. j a va2  s.c  o  m*/
        searchEnvironment.put(Context.SECURITY_AUTHENTICATION, "none");
}

From source file:com.photon.phresco.ldap.impl.LDAPManagerImpl.java

@Override
public User authenticate(Credentials credentials) throws PhrescoException {
    if (isDebugEnabled) {
        S_LOGGER.debug("Entering Method LDAPManagerImpl.authenticate(Credentials credentials)");
    }/*  w  ww  . j  av a2s.co  m*/
    String userName = credentials.getUsername();
    String passwordEncoded = credentials.getPassword();
    byte[] decodedBytes = Base64.decodeBase64(passwordEncoded);
    String password = new String(decodedBytes);
    Properties env = new Properties();
    env.put(Context.INITIAL_CONTEXT_FACTORY, ldapConfig.getLdapContextFactory());
    env.put(Context.PROVIDER_URL, ldapConfig.getLdapUrl());
    env.put(Context.SECURITY_PRINCIPAL, getUserPrincipal(userName));
    env.put(Context.SECURITY_CREDENTIALS, password);

    DirContext dc = null;
    try {
        dc = new InitialDirContext(env);
        if (isDebugEnabled) {
            S_LOGGER.debug("authenticate() Login Success for " + userName);
        }
        return getUser(credentials, dc);
    } catch (Exception e) {
        e.printStackTrace();
        if (isDebugEnabled) {
            S_LOGGER.debug("authenticate() Login Failed for " + userName);
        }
        return new User();
    } finally {
        try {
            if (dc != null) {
                dc.close();
            }
        } catch (NamingException e) {
            throw new PhrescoException(e);
        }
    }
}

From source file:de.interseroh.report.test.security.LdapServerTest.java

@Test
public void testJndiSun() throws NamingException {
    Hashtable<String, String> contextParams = new Hashtable<String, String>();
    contextParams.put(Context.PROVIDER_URL, "ldap://ldap.xxx:389");
    contextParams.put(Context.SECURITY_PRINCIPAL, USER_LDAP);
    contextParams.put(Context.SECURITY_CREDENTIALS, PASSWORD_LDAP);
    contextParams.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");

    DirContext dirContext = new InitialDirContext(contextParams);

    Attributes attributes = dirContext.getAttributes("", new String[] { "namingContexts" });
    Attribute attribute = attributes.get("namingContexts");
    NamingEnumeration<?> all = attribute.getAll();
    while (all.hasMore()) {
        String next = (String) all.next();
        logger.info(next);//ww w. j  a  v a2 s. c  om
    }
}

From source file:org.apache.ftpserver.usermanager.LdapUserManager.java

/**
 * Instantiate LDAP based <code>UserManager</code> implementation.
 *///  w w w  .j  a  v  a2s  . c  om
public void configure(Configuration config) throws FtpException {

    try {

        // get admin name 
        m_adminName = config.getString("admin", "admin");

        // get ldap parameters
        String url = config.getString("ldap-url");
        String admin = config.getString("ldap-admin-dn");
        String password = config.getString("ldap-admin-password");
        String auth = config.getString("ldap-authentication", "simple");

        m_userBaseDn = config.getString("ldap-user-base-dn");

        // create connection
        Properties adminEnv = new Properties();
        adminEnv.setProperty(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
        adminEnv.setProperty(Context.PROVIDER_URL, url);
        adminEnv.setProperty(Context.SECURITY_AUTHENTICATION, auth);
        adminEnv.setProperty(Context.SECURITY_PRINCIPAL, admin);
        adminEnv.setProperty(Context.SECURITY_CREDENTIALS, password);
        m_adminContext = new InitialDirContext(adminEnv);

        // create objectClass attribute
        m_objClassAttr = new BasicAttribute(OBJ_CLASS, false);
        m_objClassAttr.add("javaObject");
        m_objClassAttr.add("top");

        m_log.info("LDAP user manager opened.");
    } catch (FtpException ex) {
        throw ex;
    } catch (Exception ex) {
        m_log.fatal("LdapUserManager.configure()", ex);
        throw new FtpException("LdapUserManager.configure()", ex);
    }
}

From source file:org.hyperic.hq.plugin.openldap.OpenLDAPMeasurementPlugin.java

public DirContext getDirContext(Properties props) throws NamingException {
    if (this.ctx == null) {
        synchronized (this) {
            if (this.ctx == null) {
                log.debug("[getDirContext] creating new connection");
                Collection rtn = new TreeSet();
                Hashtable ldapEnv = new Hashtable();
                String ldapDriver = props.getProperty("ldapDriver"),
                        ldapHostURL = props.getProperty("ldapHostURL"),
                        ldapAuthType = props.getProperty("ldapAuthType"),
                        ldapPasswd = props.getProperty("ldapPasswd"),
                        ldapTreePathToDN = props.getProperty("ldapTreePathToDN");
                ldapTreePathToDN = (ldapTreePathToDN == null) ? "" : ldapTreePathToDN;
                ldapPasswd = (ldapPasswd == null) ? "" : ldapPasswd;
                ldapPasswd = (ldapPasswd.matches("^\\s*$")) ? "" : ldapPasswd;
                ldapEnv.put(Context.INITIAL_CONTEXT_FACTORY, ldapDriver);
                ldapEnv.put(Context.PROVIDER_URL, ldapHostURL);
                ldapEnv.put(Context.SECURITY_AUTHENTICATION, ldapAuthType);
                ldapEnv.put(Context.SECURITY_PRINCIPAL, ldapTreePathToDN);
                ldapEnv.put(Context.SECURITY_CREDENTIALS, ldapPasswd);
                this.ctx = new InitialDirContext(ldapEnv);
            }// w ww.  j a  v a2s .c o m
        }
    }
    return this.ctx;
}

From source file:org.hyperic.hq.plugin.jboss.JBossUtil.java

public static MBeanServerConnection getMBeanServerConnection(Properties config)
        throws NamingException, RemoteException {
    MBeanServerConnection adaptor;

    Properties props = new Properties();

    for (int i = 0; i < NAMING_PROPS.length; i++) {
        props.setProperty(NAMING_PROPS[i][0], NAMING_PROPS[i][1]);
    }//from ww  w  .  j a  v  a 2 s. c o  m

    props.putAll(config);

    if (props.getProperty(Context.SECURITY_PRINCIPAL) != null) {
        props.setProperty(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
    }

    InitialContext ctx = new InitialContext(props);

    try {
        Object o = ctx.lookup(props.getProperty(PROP_NAMING_CONNECTOR));
        log.debug("=> " + Arrays.asList(o.getClass().getInterfaces()));
        adaptor = (MBeanServerConnection) o;
    } finally {
        ctx.close();
    }

    return adaptor;
}