org.springframework.security.ldap.userdetails.DefaultLdapAuthoritiesPopulator Java Examples

The following examples show how to use org.springframework.security.ldap.userdetails.DefaultLdapAuthoritiesPopulator. 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: WebSecurityConfig.java    From taskana with Apache License 2.0 6 votes vote down vote up
@Bean
public LdapAuthoritiesPopulator authoritiesPopulator() {
  Function<Map<String, List<String>>, GrantedAuthority> authorityMapper =
      record -> {
        String role = record.get("spring.security.ldap.dn").get(0);
        return new SimpleGrantedAuthority(role);
      };

  DefaultLdapAuthoritiesPopulator populator =
      new DefaultLdapAuthoritiesPopulator(
          defaultSpringSecurityContextSource(), ldapGroupSearchBase);
  populator.setGroupSearchFilter(ldapGroupSearchFilter);
  populator.setSearchSubtree(true);
  populator.setRolePrefix("");
  populator.setAuthorityMapper(authorityMapper);
  return populator;
}
 
Example #2
Source File: WebSecurityConfig.java    From taskana with Apache License 2.0 6 votes vote down vote up
@Bean
public LdapAuthoritiesPopulator authoritiesPopulator() {
  Function<Map<String, List<String>>, GrantedAuthority> authorityMapper =
      record -> {
        String role = record.get("spring.security.ldap.dn").get(0);
        return new SimpleGrantedAuthority(role);
      };

  DefaultLdapAuthoritiesPopulator populator =
      new DefaultLdapAuthoritiesPopulator(
          defaultSpringSecurityContextSource(), ldapGroupSearchBase);
  populator.setGroupSearchFilter(ldapGroupSearchFilter);
  populator.setSearchSubtree(true);
  populator.setRolePrefix("");
  populator.setAuthorityMapper(authorityMapper);
  return populator;
}
 
Example #3
Source File: AtlasLdapAuthenticationProvider.java    From atlas with Apache License 2.0 5 votes vote down vote up
private DefaultLdapAuthoritiesPopulator getDefaultLdapAuthoritiesPopulator(
        LdapContextSource ldapContextSource) {
    DefaultLdapAuthoritiesPopulator defaultLdapAuthoritiesPopulator = new DefaultLdapAuthoritiesPopulator(
            ldapContextSource, ldapGroupSearchBase);
    defaultLdapAuthoritiesPopulator
            .setGroupRoleAttribute(ldapGroupRoleAttribute);
    defaultLdapAuthoritiesPopulator
            .setGroupSearchFilter(ldapGroupSearchFilter);
    defaultLdapAuthoritiesPopulator.setIgnorePartialResultException(true);
    return defaultLdapAuthoritiesPopulator;
}
 
Example #4
Source File: SecurityConfig.java    From Spring-Security-Third-Edition with MIT License 5 votes vote down vote up
@Bean
public LdapAuthoritiesPopulator authoritiesPopulator(){
    return new DefaultLdapAuthoritiesPopulator(contextSource(),
            "ou=Groups"){{
                setGroupSearchFilter("(uniqueMember={0})");
    }};
}
 
Example #5
Source File: AuthConfiguration.java    From apollo with Apache License 2.0 5 votes vote down vote up
@Bean
public LdapAuthenticationProvider ldapAuthProvider() {
  BindAuthenticator bindAuthenticator = new BindAuthenticator(ldapContextSource);
  bindAuthenticator.setUserSearch(userSearch());
  DefaultLdapAuthoritiesPopulator defaultAuthAutoConfiguration = new DefaultLdapAuthoritiesPopulator(
      ldapContextSource, null);
  defaultAuthAutoConfiguration.setIgnorePartialResultException(true);
  defaultAuthAutoConfiguration.setSearchSubtree(true);
  // Rewrite the logic of LdapAuthenticationProvider with ApolloLdapAuthenticationProvider,
  // use userId in LDAP system instead of userId input by user.
  return new ApolloLdapAuthenticationProvider(
      bindAuthenticator, defaultAuthAutoConfiguration, ldapExtendProperties);
}
 
Example #6
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 #7
Source File: AtlasLdapAuthenticationProvider.java    From incubator-atlas with Apache License 2.0 5 votes vote down vote up
private DefaultLdapAuthoritiesPopulator getDefaultLdapAuthoritiesPopulator(
        LdapContextSource ldapContextSource) {
    DefaultLdapAuthoritiesPopulator defaultLdapAuthoritiesPopulator = new DefaultLdapAuthoritiesPopulator(
            ldapContextSource, ldapGroupSearchBase);
    defaultLdapAuthoritiesPopulator
            .setGroupRoleAttribute(ldapGroupRoleAttribute);
    defaultLdapAuthoritiesPopulator
            .setGroupSearchFilter(ldapGroupSearchFilter);
    defaultLdapAuthoritiesPopulator.setIgnorePartialResultException(true);
    return defaultLdapAuthoritiesPopulator;
}
 
Example #8
Source File: LdapAuthentication.java    From osiam with MIT License 5 votes vote down vote up
@Bean
@Autowired
public OsiamLdapAuthenticationProvider osiamLdapAuthenticationProvider(SCIMUserProvisioning userProvisioning,
                                                                       @Value("${osiam.ldap.sync-user-data:true}") boolean syncUserData) {
    return new OsiamLdapAuthenticationProvider(
            bindAuthenticator(),
            new DefaultLdapAuthoritiesPopulator(contextSource(), ""),
            new OsiamLdapUserContextMapper(ldapToScimAttributeMapping()),
            userProvisioning,
            syncUserData);
}
 
Example #9
Source File: AtlasLdapAuthenticationProvider.java    From atlas with Apache License 2.0 4 votes vote down vote up
private Authentication getLdapBindAuthentication(
        Authentication authentication) {
    try {
        if (isDebugEnabled) {
            LOG.debug("==> AtlasLdapAuthenticationProvider getLdapBindAuthentication");
        }
        String userName = authentication.getName();
        String userPassword = "";
        if (authentication.getCredentials() != null) {
            userPassword = authentication.getCredentials().toString();
        }

        LdapContextSource ldapContextSource = getLdapContextSource();

        DefaultLdapAuthoritiesPopulator defaultLdapAuthoritiesPopulator = getDefaultLdapAuthoritiesPopulator(ldapContextSource);

        if (ldapUserSearchFilter == null
                || ldapUserSearchFilter.trim().isEmpty()) {
            ldapUserSearchFilter = "(uid={0})";
        }

        FilterBasedLdapUserSearch userSearch = new FilterBasedLdapUserSearch(
                ldapBase, ldapUserSearchFilter, ldapContextSource);
        userSearch.setSearchSubtree(true);

        BindAuthenticator bindAuthenticator = getBindAuthenticator(
                userSearch, ldapContextSource);

        LdapAuthenticationProvider ldapAuthenticationProvider = new LdapAuthenticationProvider(
                bindAuthenticator, defaultLdapAuthoritiesPopulator);

        if (userName != null && userPassword != null
                && !userName.trim().isEmpty()
                && !userPassword.trim().isEmpty()) {
            final List<GrantedAuthority> grantedAuths = getAuthorities(userName);
            final UserDetails principal = new User(userName, userPassword,
                    grantedAuths);
            final Authentication finalAuthentication = new UsernamePasswordAuthenticationToken(
                    principal, userPassword, grantedAuths);
            authentication = ldapAuthenticationProvider.authenticate(finalAuthentication);
            if(groupsFromUGI) {
                authentication = getAuthenticationWithGrantedAuthorityFromUGI(authentication);
            }
            return authentication;
        } else {
            LOG.error("LDAP Authentication::userName or userPassword is null or empty for userName "
                    + userName);
        }
    } catch (Exception e) {
        LOG.error(" getLdapBindAuthentication LDAP Authentication Failed:", e);
    }
    if (isDebugEnabled) {
        LOG.debug("<== AtlasLdapAuthenticationProvider getLdapBindAuthentication");
    }
    return authentication;
}
 
Example #10
Source File: AtlasLdapAuthenticationProvider.java    From incubator-atlas with Apache License 2.0 4 votes vote down vote up
private Authentication getLdapBindAuthentication(
        Authentication authentication) {
    try {
        if (isDebugEnabled) {
            LOG.debug("==> AtlasLdapAuthenticationProvider getLdapBindAuthentication");
        }
        String userName = authentication.getName();
        String userPassword = "";
        if (authentication.getCredentials() != null) {
            userPassword = authentication.getCredentials().toString();
        }

        LdapContextSource ldapContextSource = getLdapContextSource();

        DefaultLdapAuthoritiesPopulator defaultLdapAuthoritiesPopulator = getDefaultLdapAuthoritiesPopulator(ldapContextSource);

        if (ldapUserSearchFilter == null
                || ldapUserSearchFilter.trim().isEmpty()) {
            ldapUserSearchFilter = "(uid={0})";
        }

        FilterBasedLdapUserSearch userSearch = new FilterBasedLdapUserSearch(
                ldapBase, ldapUserSearchFilter, ldapContextSource);
        userSearch.setSearchSubtree(true);

        BindAuthenticator bindAuthenticator = getBindAuthenticator(
                userSearch, ldapContextSource);

        LdapAuthenticationProvider ldapAuthenticationProvider = new LdapAuthenticationProvider(
                bindAuthenticator, defaultLdapAuthoritiesPopulator);

        if (userName != null && userPassword != null
                && !userName.trim().isEmpty()
                && !userPassword.trim().isEmpty()) {
            final List<GrantedAuthority> grantedAuths = getAuthorities(userName);
            final UserDetails principal = new User(userName, userPassword,
                    grantedAuths);
            final Authentication finalAuthentication = new UsernamePasswordAuthenticationToken(
                    principal, userPassword, grantedAuths);
            authentication = ldapAuthenticationProvider.authenticate(finalAuthentication);
            if(groupsFromUGI) {
                authentication = getAuthenticationWithGrantedAuthorityFromUGI(authentication);
            }
            return authentication;
        } else {
            LOG.error("LDAP Authentication::userName or userPassword is null or empty for userName "
                    + userName);
        }
    } catch (Exception e) {
        LOG.error(" getLdapBindAuthentication LDAP Authentication Failed:", e);
    }
    if (isDebugEnabled) {
        LOG.debug("<== AtlasLdapAuthenticationProvider getLdapBindAuthentication");
    }
    return authentication;
}
 
Example #11
Source File: RangerAuthenticationProvider.java    From ranger with Apache License 2.0 4 votes vote down vote up
private Authentication getLdapBindAuthentication(Authentication authentication) {
	try {
		String rangerLdapURL = PropertiesUtil.getProperty("ranger.ldap.url", "");
		String rangerLdapUserDNPattern = PropertiesUtil.getProperty("ranger.ldap.user.dnpattern", "");
		String rangerLdapGroupSearchBase = PropertiesUtil.getProperty("ranger.ldap.group.searchbase", "");
		String rangerLdapGroupSearchFilter = PropertiesUtil.getProperty("ranger.ldap.group.searchfilter", "");
		String rangerLdapGroupRoleAttribute = PropertiesUtil.getProperty("ranger.ldap.group.roleattribute", "");
		String rangerLdapDefaultRole = PropertiesUtil.getProperty("ranger.ldap.default.role", "ROLE_USER");
		String rangerLdapBase = PropertiesUtil.getProperty("ranger.ldap.base.dn", "");
		String rangerLdapBindDN = PropertiesUtil.getProperty("ranger.ldap.bind.dn", "");
		String rangerLdapBindPassword = PropertiesUtil.getProperty("ranger.ldap.bind.password", "");
		String rangerLdapReferral = PropertiesUtil.getProperty("ranger.ldap.referral", "follow");
		String rangerLdapUserSearchFilter = PropertiesUtil.getProperty("ranger.ldap.user.searchfilter", "(uid={0})");
		boolean rangerIsStartTlsEnabled = Boolean.valueOf(PropertiesUtil.getProperty(
				"ranger.ldap.starttls", "false"));
		String userName = authentication.getName();
		String userPassword = "";
		if (authentication.getCredentials() != null) {
			userPassword = authentication.getCredentials().toString();
		}

		LdapContextSource ldapContextSource = new DefaultSpringSecurityContextSource(rangerLdapURL);
		ldapContextSource.setUserDn(rangerLdapBindDN);
		ldapContextSource.setPassword(rangerLdapBindPassword);
		ldapContextSource.setReferral(rangerLdapReferral);
		ldapContextSource.setCacheEnvironmentProperties(false);
		ldapContextSource.setAnonymousReadOnly(false);
		ldapContextSource.setPooled(true);
		if (rangerIsStartTlsEnabled) {
			ldapContextSource.setPooled(false);
			ldapContextSource.setAuthenticationStrategy(new DefaultTlsDirContextAuthenticationStrategy());
		}
		ldapContextSource.afterPropertiesSet();

		DefaultLdapAuthoritiesPopulator defaultLdapAuthoritiesPopulator = new DefaultLdapAuthoritiesPopulator(ldapContextSource, rangerLdapGroupSearchBase);
		defaultLdapAuthoritiesPopulator.setGroupRoleAttribute(rangerLdapGroupRoleAttribute);
		defaultLdapAuthoritiesPopulator.setGroupSearchFilter(rangerLdapGroupSearchFilter);
		defaultLdapAuthoritiesPopulator.setIgnorePartialResultException(true);

		//String searchFilter="(uid={0})";
		if (rangerLdapUserSearchFilter==null||rangerLdapUserSearchFilter.trim().isEmpty()) {
			rangerLdapUserSearchFilter="(uid={0})";
		}
		FilterBasedLdapUserSearch userSearch=new FilterBasedLdapUserSearch(rangerLdapBase, rangerLdapUserSearchFilter,ldapContextSource);
		userSearch.setSearchSubtree(true);

		BindAuthenticator bindAuthenticator = new BindAuthenticator(ldapContextSource);
		bindAuthenticator.setUserSearch(userSearch);
		String[] userDnPatterns = new String[] { rangerLdapUserDNPattern };
		bindAuthenticator.setUserDnPatterns(userDnPatterns);
		bindAuthenticator.afterPropertiesSet();

		LdapAuthenticationProvider ldapAuthenticationProvider = new LdapAuthenticationProvider(bindAuthenticator,defaultLdapAuthoritiesPopulator);

		if (userName != null && userPassword != null && !userName.trim().isEmpty()&& !userPassword.trim().isEmpty()) {
			final List<GrantedAuthority> grantedAuths = new ArrayList<>();
			grantedAuths.add(new SimpleGrantedAuthority(rangerLdapDefaultRole));
			final UserDetails principal = new User(userName, userPassword,grantedAuths);
			final Authentication finalAuthentication = new UsernamePasswordAuthenticationToken(principal, userPassword, grantedAuths);

			authentication = ldapAuthenticationProvider.authenticate(finalAuthentication);
			authentication=getAuthenticationWithGrantedAuthority(authentication);
			return authentication;
		} else {
			return authentication;
		}
	} catch (Exception e) {
		logger.debug("LDAP Authentication Failed:", e);
	}
	return authentication;
}
 
Example #12
Source File: AuthenticationCheck.java    From ranger with Apache License 2.0 4 votes vote down vote up
private Authentication getLdapBindAuthentication(String ldapUrl, String bindDn, String bindPassword,
                                                 String userName, String userPassword) {
    Authentication result = null;
    try {
        LdapContextSource ldapContextSource = new DefaultSpringSecurityContextSource(ldapUrl);
        ldapContextSource.setUserDn(bindDn);
        ldapContextSource.setPassword(bindPassword);
        ldapContextSource.setReferral("follow");
        ldapContextSource.setCacheEnvironmentProperties(false);
        ldapContextSource.setAnonymousReadOnly(true);
        ldapContextSource.setPooled(true);
        ldapContextSource.afterPropertiesSet();

        DefaultLdapAuthoritiesPopulator defaultLdapAuthoritiesPopulator = new DefaultLdapAuthoritiesPopulator(ldapContextSource, groupSearchBase);
        defaultLdapAuthoritiesPopulator.setGroupRoleAttribute(roleAttribute);
        defaultLdapAuthoritiesPopulator.setGroupSearchFilter(groupSearchFilter);
        defaultLdapAuthoritiesPopulator.setIgnorePartialResultException(true);

        String searchFilter="(uid={0})";
        FilterBasedLdapUserSearch userSearch=new FilterBasedLdapUserSearch(adDomain, searchFilter,ldapContextSource);
        userSearch.setSearchSubtree(true);

        BindAuthenticator bindAuthenticator = new BindAuthenticator(ldapContextSource);
        bindAuthenticator.setUserSearch(userSearch);
        String[] userDnPatterns = new String[] { userDnPattern };
        bindAuthenticator.setUserDnPatterns(userDnPatterns);
        bindAuthenticator.afterPropertiesSet();

        LdapAuthenticationProvider ldapAuthenticationProvider = new LdapAuthenticationProvider(bindAuthenticator,defaultLdapAuthoritiesPopulator);

        if (userName != null && userPassword != null && !userName.trim().isEmpty() && !userPassword.trim().isEmpty()) {
            final List<GrantedAuthority> grantedAuths = new ArrayList<>();
            grantedAuths.add(new SimpleGrantedAuthority("ROLE_USER"));
            final UserDetails principal = new User(userName, userPassword, grantedAuths);
            final Authentication finalAuthentication = new UsernamePasswordAuthenticationToken(principal, userPassword, grantedAuths);

            result = ldapAuthenticationProvider.authenticate(finalAuthentication);
        }
    } catch (BadCredentialsException bce) {
        logFile.println("ERROR: LDAP Authentication Failed. Please verify values for ranger.admin.auth.sampleuser and " +
                "ranger.admin.auth.samplepassword\n");
    } catch (Exception e) {
        logFile.println("ERROR: LDAP Authentication Failed: " + e);
    }
    return result;
}