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

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

Introduction

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

Prototype

public void setAuthentication(String authentication) 

Source Link

Document

Sets the type of LDAP authentication to perform when connecting to the LDAP server.

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.");
    }/*from w w  w  .  j ava 2  s .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.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 w ww.java2  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.");
    }/*from   w w w  .j a v  a 2 s. co m*/

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