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

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

Introduction

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

Prototype

public void setUsePooling(boolean usePooling) 

Source Link

Document

Determines whether or not LdapContext pooling is enabled for connections made using the system user account.

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  w w  .j  a  v  a 2 s.c om*/

    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.ldap.realms.SimpleLdapManager.java

License:Open Source License

protected LdapContextFactory getLdapContextFactory() throws LdapDAOException {
    DefaultLdapContextFactory defaultLdapContextFactory = new DefaultLdapContextFactory();

    if (this.getLdapConfiguration() == null || this.getLdapConfiguration().readConnectionInfo() == null) {
        throw new LdapDAOException("Ldap connection is not configured.");
    }/*from   www .  j  av a2  s.  c om*/

    CConnectionInfo connInfo = this.getLdapConfiguration().readConnectionInfo();

    String url;
    try {
        url = new LdapURL(connInfo.getProtocol(), connInfo.getHost(), connInfo.getPort(),
                connInfo.getSearchBase()).toString();
    } catch (MalformedURLException e) {
        // log an error, because the user could still log in and fix the config.
        this.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());

    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.");
    }/*  w w w .  j a v a 2  s  .  c om*/

    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;
}