SEC-2690: String[]->List<String>

Use Collections rather than Arrays since Collections can be immutable.
This commit is contained in:
Rob Winch
2014-07-28 11:15:48 -05:00
parent 15c837d5de
commit 1761b29e58
6 changed files with 53 additions and 49 deletions

View File

@@ -153,12 +153,12 @@ public class SpringSecurityLdapTemplate extends LdapTemplate {
public Set<String> searchForSingleAttributeValues(final String base, final String filter, final Object[] params,
final String attributeName) {
String[] attributeNames = new String[]{attributeName};
Set<Map<String, String[]>> multipleAttributeValues = searchForMultipleAttributeValues(base, filter, params, attributeNames);
Set<Map<String, List<String>>> multipleAttributeValues = searchForMultipleAttributeValues(base, filter, params, attributeNames);
Set<String> result = new HashSet<String>();
for (Map<String, String[]> map : multipleAttributeValues) {
String[] values = map.get(attributeName);
if (values != null && values.length > 0) {
result.addAll(Arrays.asList(values));
for (Map<String, List<String>> map : multipleAttributeValues) {
List<String> values = map.get(attributeName);
if (values != null) {
result.addAll(values);
}
}
return result;
@@ -178,7 +178,7 @@ public class SpringSecurityLdapTemplate extends LdapTemplate {
* The attribute name is the key for each set of values. In addition each map contains the DN as a String
* with the key predefined key {@link #DN_KEY}.
*/
public Set<Map<String, String[]>> searchForMultipleAttributeValues(final String base, final String filter, final Object[] params,
public Set<Map<String, List<String>>> searchForMultipleAttributeValues(final String base, final String filter, final Object[] params,
final String[] attributeNames) {
// Escape the params acording to RFC2254
Object[] encodedParams = new String[params.length];
@@ -190,12 +190,12 @@ public class SpringSecurityLdapTemplate extends LdapTemplate {
String formattedFilter = MessageFormat.format(filter, encodedParams);
logger.debug("Using filter: " + formattedFilter);
final HashSet<Map<String, String[]>> set = new HashSet<Map<String, String[]>>();
final HashSet<Map<String, List<String>>> set = new HashSet<Map<String, List<String>>>();
ContextMapper roleMapper = new ContextMapper() {
public Object mapFromContext(Object ctx) {
DirContextAdapter adapter = (DirContextAdapter) ctx;
Map<String, String[]> record = new HashMap<String, String[]>();
Map<String, List<String>> record = new HashMap<String, List<String>>();
if (attributeNames == null || attributeNames.length == 0) {
try {
for (NamingEnumeration ae = adapter.getAttributes().getAll(); ae.hasMore(); ) {
@@ -210,7 +210,7 @@ public class SpringSecurityLdapTemplate extends LdapTemplate {
extractStringAttributeValues(adapter, record, attributeName);
}
}
record.put(DN_KEY, new String[]{getAdapterDN(adapter)});
record.put(DN_KEY, Arrays.asList(getAdapterDN(adapter)));
set.add(record);
return null;
}
@@ -246,7 +246,7 @@ public class SpringSecurityLdapTemplate extends LdapTemplate {
* @param record - the map holding the attribute names and values
* @param attributeName - the name for which to fetch the values from
*/
protected void extractStringAttributeValues(DirContextAdapter adapter, Map<String, String[]> record, String attributeName) {
protected void extractStringAttributeValues(DirContextAdapter adapter, Map<String, List<String>> record, String attributeName) {
Object[] values = adapter.getObjectAttributes(attributeName);
if (values == null || values.length == 0) {
logger.debug("No attribute value found for '" + attributeName + "'");
@@ -265,7 +265,7 @@ public class SpringSecurityLdapTemplate extends LdapTemplate {
}
}
}
record.put(attributeName, svalues.toArray(new String[svalues.size()]));
record.put(attributeName, svalues);
}
/**

View File

@@ -17,6 +17,8 @@ package org.springframework.security.ldap.userdetails;
import org.springframework.security.core.GrantedAuthority;
import java.util.Collections;
import java.util.List;
import java.util.Map;
/**
@@ -30,7 +32,7 @@ public class LdapAuthority implements GrantedAuthority {
private String dn;
private String role;
private Map<String, String[]> attributes;
private Map<String, List<String>> attributes;
/**
* Constructs an LdapAuthority that has a role and a DN but no other attributes
@@ -49,7 +51,7 @@ public class LdapAuthority implements GrantedAuthority {
* @param dn
* @param attributes
*/
public LdapAuthority(String role, String dn, Map<String, String[]> attributes) {
public LdapAuthority(String role, String dn, Map<String, List<String>> attributes) {
if (role == null) throw new NullPointerException("role can not be null");
this.role = role;
this.dn = dn;
@@ -61,7 +63,7 @@ public class LdapAuthority implements GrantedAuthority {
*
* @return the LDAP attributes, map can be null
*/
public Map<String, String[]> getAttributes() {
public Map<String, List<String>> getAttributes() {
return attributes;
}
@@ -80,13 +82,13 @@ public class LdapAuthority implements GrantedAuthority {
* @param name the attribute name
* @return a String array, never null but may be zero length
*/
public String[] getAttributeValues(String name) {
String[] result = null;
public List<String> getAttributeValues(String name) {
List<String> result = null;
if (attributes != null) {
result = attributes.get(name);
}
if (result == null) {
result = new String[0];
result = Collections.emptyList();
}
return result;
}
@@ -98,11 +100,11 @@ public class LdapAuthority implements GrantedAuthority {
* @return the first attribute value for a specified attribute, may be null
*/
public String getFirstAttributeValue(String name) {
String[] result = getAttributeValues(name);
if (result.length > 0) {
return result[0];
} else {
List<String> result = getAttributeValues(name);
if (result.isEmpty()) {
return null;
} else {
return result.get(0);
}
}

View File

@@ -22,10 +22,7 @@ import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.ldap.SpringSecurityLdapTemplate;
import org.springframework.util.StringUtils;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.*;
/**
* A LDAP authority populator that can recursively search static nested groups. <p>An example of nested groups can be
@@ -185,7 +182,7 @@ public class NestedLdapAuthoritiesPopulator extends DefaultLdapAuthoritiesPopula
getAttributeNames().add(getGroupRoleAttribute());
}
Set<Map<String, String[]>> userRoles = getLdapTemplate().searchForMultipleAttributeValues(
Set<Map<String, List<String>>> userRoles = getLdapTemplate().searchForMultipleAttributeValues(
getGroupSearchBase(),
getGroupSearchFilter(),
new String[]{userDn, username},
@@ -195,12 +192,14 @@ public class NestedLdapAuthoritiesPopulator extends DefaultLdapAuthoritiesPopula
logger.debug("Roles from search: " + userRoles);
}
for (Map<String, String[]> record : userRoles) {
for (Map<String, List<String>> record : userRoles) {
boolean circular = false;
String dn = record.get(SpringSecurityLdapTemplate.DN_KEY)[0];
String[] roleValues = record.get(getGroupRoleAttribute());
String dn = record.get(SpringSecurityLdapTemplate.DN_KEY).get(0);
List<String> roleValues = record.get(getGroupRoleAttribute());
Set<String> roles = new HashSet<String>();
roles.addAll(Arrays.asList(roleValues != null ? roleValues : new String[0]));
if(roleValues != null) {
roles.addAll(roleValues);
}
for (String role : roles) {
if (isConvertToUpperCase()) {
role = role.toUpperCase();