Java Code Examples for org.springframework.security.ldap.SpringSecurityLdapTemplate#setSearchControls()

The following examples show how to use org.springframework.security.ldap.SpringSecurityLdapTemplate#setSearchControls() . 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: FilterLdapByGroupUserSearch.java    From apollo with Apache License 2.0 5 votes vote down vote up
@Override
public DirContextOperations searchForUser(String username) {
  if (logger.isDebugEnabled()) {
    logger.debug("Searching for user '" + username + "', with user search " + this);
  }
  SpringSecurityLdapTemplate template = new SpringSecurityLdapTemplate(this.contextSource);
  template.setSearchControls(searchControls);
  return template
      .searchForObject(groupBase, groupSearch, ctx -> {
        if (!MEMBER_UID_ATTR_NAME.equals(groupMembershipAttrName)) {
          String[] members = ((DirContextAdapter) ctx)
              .getStringAttributes(groupMembershipAttrName);
          for (String item : members) {
            LdapName memberDn = LdapUtils.newLdapName(item);
            LdapName memberRdn = LdapUtils
                .removeFirst(memberDn, LdapUtils.newLdapName(searchBase));
            String rdnValue = LdapUtils.getValue(memberRdn, rdnKey).toString();
            if (rdnValue.equalsIgnoreCase(username)) {
              return new DirContextAdapter(memberRdn.toString());
            }
          }
          throw new UsernameNotFoundException("User " + username + " not found in directory.");
        }
        String[] memberUids = ((DirContextAdapter) ctx)
            .getStringAttributes(groupMembershipAttrName);
        for (String memberUid : memberUids) {
          if (memberUid.equalsIgnoreCase(username)) {
            Name name = searchUserById(memberUid);
            LdapName ldapName = LdapUtils.newLdapName(name);
            LdapName ldapRdn = LdapUtils
                .removeFirst(ldapName, LdapUtils.newLdapName(searchBase));
            return new DirContextAdapter(ldapRdn);
          }
        }
        throw new UsernameNotFoundException("User " + username + " not found in directory.");
      });
}
 
Example 2
Source File: FilterLdapByGroupUserSearch.java    From apollo with Apache License 2.0 4 votes vote down vote up
private Name searchUserById(String userId) {
  SpringSecurityLdapTemplate template = new SpringSecurityLdapTemplate(this.contextSource);
  template.setSearchControls(searchControls);
  return template.searchForObject(query().where(this.loginIdAttrName).is(userId),
      ctx -> ((DirContextAdapter) ctx).getDn());
}