Example usage for org.apache.shiro.realm.ldap DefaultLdapContextFactory setAdditionalEnvironment

List of usage examples for org.apache.shiro.realm.ldap DefaultLdapContextFactory setAdditionalEnvironment

Introduction

In this page you can find the example usage for org.apache.shiro.realm.ldap DefaultLdapContextFactory setAdditionalEnvironment.

Prototype

public void setAdditionalEnvironment(Map<String, String> additionalEnvironment) 

Source Link

Document

These entries are added to the environment map before initializing the LDAP context.

Usage

From source file:org.sonatype.nexus.ldap.internal.realms.LdapConnectionUtils.java

License:Open Source License

public static LdapContextFactory getLdapContextFactory(final LdapConfiguration configuration,
        final TrustStore trustStore) throws LdapDAOException {
    if (configuration == null) {
        throw new LdapDAOException("Ldap connection is not configured.");
    }/*  w  ww . ja v  a2s .c  o m*/

    DefaultLdapContextFactory defaultLdapContextFactory = new DefaultLdapContextFactory();

    Connection connInfo = configuration.getConnection();
    Host host = connInfo.getHost();

    String url;
    try {
        url = new LdapURL(host.getProtocol().name(), host.getHostName(), host.getPort(),
                connInfo.getSearchBase()).toString();
    } catch (MalformedURLException e) {
        // log an error, because the user could still log in and fix the config.
        log.error("LDAP Configuration is Invalid.");
        throw new LdapDAOException("Invalid LDAP URL: " + e.getMessage());
    }

    defaultLdapContextFactory.setUsePooling(true);
    defaultLdapContextFactory.setUrl(url);
    defaultLdapContextFactory.setSystemUsername(connInfo.getSystemUsername());
    defaultLdapContextFactory.setSystemPassword(connInfo.getSystemPassword());
    defaultLdapContextFactory.setSearchBase(connInfo.getSearchBase());
    defaultLdapContextFactory.setAuthentication(connInfo.getAuthScheme());

    // get the timeout
    Map<String, String> connectionProperties = new HashMap<>();
    connectionProperties.put("com.sun.jndi.ldap.connect.timeout",
            Integer.toString(connInfo.getConnectionTimeout() * 1000));

    // and the realm
    if (connInfo.getSaslRealm() != null) {
        connectionProperties.put("java.naming.security.sasl.realm", connInfo.getSaslRealm());
    }
    defaultLdapContextFactory.setAdditionalEnvironment(connectionProperties);

    if (host.getProtocol() == Connection.Protocol.ldaps && connInfo.getUseTrustStore()) {
        SSLContext sslContext = trustStore.getSSLContext();
        log.debug("Using Nexus SSL Trust Store for accessing {}:{}", host.getHostName(), host.getPort());
        return new SSLLdapContextFactory(sslContext, defaultLdapContextFactory);
    }
    log.debug("Using JVM Trust Store for accessing {}:{}", host.getHostName(), host.getPort());
    return defaultLdapContextFactory;
}

From source file:org.sonatype.security.realms.ldap.internal.realms.LdapConnectionUtils.java

License:Open Source License

public static DefaultLdapContextFactory getLdapContextFactory(LdapConfiguration ldapServer,
        boolean useBackupUrl) throws LdapDAOException {
    DefaultLdapContextFactory defaultLdapContextFactory = new DefaultLdapContextFactory();

    if (ldapServer == null) {
        throw new LdapDAOException("Ldap connection is not configured.");
    }/*from  w w w  .ja va 2  s . com*/

    Connection connInfo = ldapServer.getConnection();

    String url;
    try {
        if (useBackupUrl) {
            url = new LdapURL(connInfo.getBackupHost().getProtocol().name(),
                    connInfo.getBackupHost().getHostName(), connInfo.getBackupHost().getPort(),
                    connInfo.getSearchBase()).toString();
        } else {
            url = new LdapURL(connInfo.getHost().getProtocol().name(), connInfo.getHost().getHostName(),
                    connInfo.getHost().getPort(), connInfo.getSearchBase()).toString();
        }
    } catch (MalformedURLException e) {
        // log an error, because the user could still log in and fix the config.
        logger.error("LDAP Configuration is Invalid.");
        throw new LdapDAOException("Invalid LDAP URL: " + e.getMessage());
    }

    defaultLdapContextFactory.setUsePooling(true);
    defaultLdapContextFactory.setUrl(url);
    defaultLdapContextFactory.setSystemUsername(connInfo.getSystemUsername());
    defaultLdapContextFactory.setSystemPassword(connInfo.getSystemPassword());
    defaultLdapContextFactory.setSearchBase(connInfo.getSearchBase());
    defaultLdapContextFactory.setAuthentication(connInfo.getAuthScheme());

    // get the timeout
    Map<String, String> connectionProperties = new HashMap<String, String>();
    connectionProperties.put("com.sun.jndi.ldap.connect.timeout",
            Integer.toString(ldapServer.getConnection().getConnectionTimeout() * 1000));

    // and the realm
    if (connInfo.getSaslRealm() != null) {
        connectionProperties.put("java.naming.security.sasl.realm", connInfo.getSaslRealm());
    }
    defaultLdapContextFactory.setAdditionalEnvironment(connectionProperties);

    return defaultLdapContextFactory;
}