Java Code Examples for org.springframework.security.ldap.DefaultSpringSecurityContextSource#setUserDn()

The following examples show how to use org.springframework.security.ldap.DefaultSpringSecurityContextSource#setUserDn() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LdapAuthenticationProviderConfigurer.java    From gravitee-management-rest-api with Apache License 2.0 6 votes vote down vote up
private DefaultSpringSecurityContextSource build() throws Exception {
    DefaultSpringSecurityContextSource contextSource = new DefaultSpringSecurityContextSource(
            getProviderUrl());
    if (managerDn != null) {
        contextSource.setUserDn(managerDn);
        if (managerPassword == null) {
            throw new IllegalStateException(
                    "managerPassword is required if managerDn is supplied");
        }
        contextSource.setPassword(managerPassword);
    }
    contextSource = postProcess(contextSource);
    if (url != null) {
        return contextSource;
    }
    ApacheDSContainer apacheDsContainer = new ApacheDSContainer(root, ldif);
    apacheDsContainer.setPort(getPort());
    postProcess(apacheDsContainer);
    return contextSource;
}
 
Example 2
Source File: LdapContextSourceFactory.java    From gravitee-management-rest-api with Apache License 2.0 6 votes vote down vote up
private DefaultSpringSecurityContextSource build() throws Exception {
            DefaultSpringSecurityContextSource contextSource = new DefaultSpringSecurityContextSource(
                    getProviderUrl());
            if (managerDn != null) {
                contextSource.setUserDn(managerDn);
                if (managerPassword == null) {
                    throw new IllegalStateException(
                            "managerPassword is required if managerDn is supplied");
                }
                contextSource.setPassword(managerPassword);
            }
//            contextSource = postProcess(contextSource);
            if (url != null) {
                return contextSource;
            }
            ApacheDSContainer embeddedApacheContainer = new ApacheDSContainer(root, ldif);
            embeddedApacheContainer.setPort(getPort());
            apacheDsContainer = embeddedApacheContainer;
//            postProcess(apacheDsContainer);
            return contextSource;
        }
 
Example 3
Source File: LdapConfig.java    From fiat with Apache License 2.0 5 votes vote down vote up
@Bean
SpringSecurityLdapTemplate springSecurityLdapTemplate() throws Exception {
  DefaultSpringSecurityContextSource contextSource =
      new DefaultSpringSecurityContextSource(configProps.url);
  contextSource.setUserDn(configProps.managerDn);
  contextSource.setPassword(configProps.managerPassword);
  contextSource.afterPropertiesSet();

  return new SpringSecurityLdapTemplate(contextSource);
}
 
Example 4
Source File: LdapAuthenticationProvider.java    From gravitee-management-rest-api with Apache License 2.0 5 votes vote down vote up
@Override
public SecurityConfigurer configure() throws Exception {
    LOGGER.info("Configuring an LDAP Identity Provider");

    LdapAuthenticationProviderConfigurer<AuthenticationManagerBuilder> ldapAuthenticationProviderConfigurer =
            new LdapAuthenticationProviderConfigurer<>();

    // Create LDAP context
    DefaultSpringSecurityContextSource contextSource = new DefaultSpringSecurityContextSource(
            environment.getProperty("context.url"));
    contextSource.setBase(environment.getProperty("context.base"));
    contextSource.setUserDn(environment.getProperty("context.username"));
    contextSource.setPassword(environment.getProperty("context.password"));
    contextSource.afterPropertiesSet();

    ldapAuthenticationProviderConfigurer
            .userSearchBase(environment.getProperty("authentication.user.base", ""))
            .userSearchFilter(environment.getProperty("authentication.user.filter"))
            .groupSearchBase(environment.getProperty("authentication.group.base", ""))
            .groupSearchFilter(environment.getProperty("authentication.group.filter", "(uniqueMember={0})"))
            .groupRoleAttribute(environment.getProperty("authentication.group.role.attribute", "cn"))
            .rolePrefix("");

    DefaultLdapAuthoritiesPopulator populator = new DefaultLdapAuthoritiesPopulator(contextSource,
            environment.getProperty("authentication.group.base", ""));
    populator.setRolePrefix("");
    populator.setGroupRoleAttribute(environment.getProperty("authentication.group.role.attribute", "cn"));
    populator.setGroupSearchFilter(environment.getProperty("authentication.group.filter", "(uniqueMember={0})"));

    ldapAuthenticationProviderConfigurer.ldapAuthoritiesPopulator(populator).contextSource(contextSource);

    // set up LDAP mapper
    UserDetailsContextPropertiesMapper userDetailsContextPropertiesMapper = new UserDetailsContextPropertiesMapper();
    userDetailsContextPropertiesMapper.setEnvironment(environment);
    userDetailsContextPropertiesMapper.afterPropertiesSet();
    ldapAuthenticationProviderConfigurer.userDetailsContextMapper(userDetailsContextPropertiesMapper);

    return ldapAuthenticationProviderConfigurer;
}
 
Example 5
Source File: LDAPAuthenticator.java    From para with Apache License 2.0 4 votes vote down vote up
/**
 * Default constructor.
 * @param ldapSettings LDAP config map for an app
 */
public LDAPAuthenticator(Map<String, String> ldapSettings) {
	if (ldapSettings != null && ldapSettings.containsKey("security.ldap.server_url")) {
		String serverUrl = ldapSettings.get("security.ldap.server_url");
		String baseDN = ldapSettings.get("security.ldap.base_dn");
		String bindDN = Utils.noSpaces(ldapSettings.get("security.ldap.bind_dn"), "%20");
		String bindPass = ldapSettings.get("security.ldap.bind_pass");
		String userSearchBase = ldapSettings.get("security.ldap.user_search_base");
		String userSearchFilter = ldapSettings.get("security.ldap.user_search_filter");
		String userDnPattern = ldapSettings.get("security.ldap.user_dn_pattern");
		String passAttribute = ldapSettings.get("security.ldap.password_attribute");
		boolean usePasswordComparison = ldapSettings.containsKey("security.ldap.compare_passwords");

		DefaultSpringSecurityContextSource contextSource =
				new DefaultSpringSecurityContextSource(Arrays.asList(serverUrl), baseDN);
		contextSource.setAuthenticationSource(new SpringSecurityAuthenticationSource());
		contextSource.setCacheEnvironmentProperties(false);
		if (!bindDN.isEmpty()) {
			// this is usually not required for authentication - leave blank
			contextSource.setUserDn(bindDN);
		}
		if (!bindPass.isEmpty()) {
			// this is usually not required for authentication - leave blank
			contextSource.setPassword(bindPass);
		}
		LdapUserSearch userSearch = new FilterBasedLdapUserSearch(userSearchBase, userSearchFilter, contextSource);

		if (usePasswordComparison) {
			PasswordComparisonAuthenticator p = new PasswordComparisonAuthenticator(contextSource);
			p.setPasswordAttributeName(passAttribute);
			p.setUserDnPatterns(getUserDnPatterns(userDnPattern));
			p.setUserSearch(userSearch);
			authenticator = p;
		} else {
			BindAuthenticator b = new BindAuthenticator(contextSource);
			b.setUserDnPatterns(getUserDnPatterns(userDnPattern));
			b.setUserSearch(userSearch);
			authenticator = b;
		}
	}
}