|
@@ -16,29 +16,25 @@
|
|
|
|
|
|
package org.springframework.security.ldap.userdetails;
|
|
package org.springframework.security.ldap.userdetails;
|
|
|
|
|
|
-import org.springframework.beans.BeansException;
|
|
|
|
-import org.springframework.context.ApplicationContext;
|
|
|
|
-import org.springframework.context.ApplicationContextAware;
|
|
|
|
-import org.springframework.security.config.GrantedAuthorityDefaults;
|
|
|
|
-import org.springframework.security.core.GrantedAuthority;
|
|
|
|
-import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
|
|
|
-import org.springframework.security.ldap.SpringSecurityLdapTemplate;
|
|
|
|
-import org.springframework.ldap.core.ContextSource;
|
|
|
|
-import org.springframework.ldap.core.DirContextOperations;
|
|
|
|
-import org.springframework.ldap.core.LdapTemplate;
|
|
|
|
-import org.springframework.util.Assert;
|
|
|
|
-
|
|
|
|
-import org.apache.commons.logging.Log;
|
|
|
|
-import org.apache.commons.logging.LogFactory;
|
|
|
|
-
|
|
|
|
-import javax.naming.directory.SearchControls;
|
|
|
|
-
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.ArrayList;
|
|
import java.util.Collection;
|
|
import java.util.Collection;
|
|
import java.util.HashSet;
|
|
import java.util.HashSet;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
import java.util.Set;
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
+import javax.naming.directory.SearchControls;
|
|
|
|
+
|
|
|
|
+import org.apache.commons.logging.Log;
|
|
|
|
+import org.apache.commons.logging.LogFactory;
|
|
|
|
+
|
|
|
|
+import org.springframework.ldap.core.ContextSource;
|
|
|
|
+import org.springframework.ldap.core.DirContextOperations;
|
|
|
|
+import org.springframework.ldap.core.LdapTemplate;
|
|
|
|
+import org.springframework.security.core.GrantedAuthority;
|
|
|
|
+import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
|
|
|
+import org.springframework.security.ldap.SpringSecurityLdapTemplate;
|
|
|
|
+import org.springframework.util.Assert;
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* The default strategy for obtaining user role information from the directory.
|
|
* The default strategy for obtaining user role information from the directory.
|
|
* <p>
|
|
* <p>
|
|
@@ -101,7 +97,7 @@ import java.util.Set;
|
|
* @author Luke Taylor
|
|
* @author Luke Taylor
|
|
* @author Filip Hanik
|
|
* @author Filip Hanik
|
|
*/
|
|
*/
|
|
-public class DefaultLdapAuthoritiesPopulator implements LdapAuthoritiesPopulator, ApplicationContextAware {
|
|
|
|
|
|
+public class DefaultLdapAuthoritiesPopulator implements LdapAuthoritiesPopulator {
|
|
// ~ Static fields/initializers
|
|
// ~ Static fields/initializers
|
|
// =====================================================================================
|
|
// =====================================================================================
|
|
|
|
|
|
@@ -144,7 +140,7 @@ public class DefaultLdapAuthoritiesPopulator implements LdapAuthoritiesPopulator
|
|
/**
|
|
/**
|
|
* The role prefix that will be prepended to each role name
|
|
* The role prefix that will be prepended to each role name
|
|
*/
|
|
*/
|
|
- private GrantedAuthorityDefaults rolePrefix = new GrantedAuthorityDefaults("ROLE_");
|
|
|
|
|
|
+ private String rolePrefix = "ROLE_";
|
|
/**
|
|
/**
|
|
* Should we convert the role name to uppercase
|
|
* Should we convert the role name to uppercase
|
|
*/
|
|
*/
|
|
@@ -164,7 +160,7 @@ public class DefaultLdapAuthoritiesPopulator implements LdapAuthoritiesPopulator
|
|
public DefaultLdapAuthoritiesPopulator(ContextSource contextSource,
|
|
public DefaultLdapAuthoritiesPopulator(ContextSource contextSource,
|
|
String groupSearchBase) {
|
|
String groupSearchBase) {
|
|
Assert.notNull(contextSource, "contextSource must not be null");
|
|
Assert.notNull(contextSource, "contextSource must not be null");
|
|
- ldapTemplate = new SpringSecurityLdapTemplate(contextSource);
|
|
|
|
|
|
+ this.ldapTemplate = new SpringSecurityLdapTemplate(contextSource);
|
|
getLdapTemplate().setSearchControls(getSearchControls());
|
|
getLdapTemplate().setSearchControls(getSearchControls());
|
|
this.groupSearchBase = groupSearchBase;
|
|
this.groupSearchBase = groupSearchBase;
|
|
|
|
|
|
@@ -172,7 +168,8 @@ public class DefaultLdapAuthoritiesPopulator implements LdapAuthoritiesPopulator
|
|
logger.info("groupSearchBase is null. No group search will be performed.");
|
|
logger.info("groupSearchBase is null. No group search will be performed.");
|
|
}
|
|
}
|
|
else if (groupSearchBase.length() == 0) {
|
|
else if (groupSearchBase.length() == 0) {
|
|
- logger.info("groupSearchBase is empty. Searches will be performed from the context source base");
|
|
|
|
|
|
+ logger.info(
|
|
|
|
+ "groupSearchBase is empty. Searches will be performed from the context source base");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -201,6 +198,7 @@ public class DefaultLdapAuthoritiesPopulator implements LdapAuthoritiesPopulator
|
|
* @param user the user who's authorities are required
|
|
* @param user the user who's authorities are required
|
|
* @return the set of roles granted to the user.
|
|
* @return the set of roles granted to the user.
|
|
*/
|
|
*/
|
|
|
|
+ @Override
|
|
public final Collection<GrantedAuthority> getGrantedAuthorities(
|
|
public final Collection<GrantedAuthority> getGrantedAuthorities(
|
|
DirContextOperations user, String username) {
|
|
DirContextOperations user, String username) {
|
|
String userDn = user.getNameInNamespace();
|
|
String userDn = user.getNameInNamespace();
|
|
@@ -217,8 +215,8 @@ public class DefaultLdapAuthoritiesPopulator implements LdapAuthoritiesPopulator
|
|
roles.addAll(extraRoles);
|
|
roles.addAll(extraRoles);
|
|
}
|
|
}
|
|
|
|
|
|
- if (defaultRole != null) {
|
|
|
|
- roles.add(defaultRole);
|
|
|
|
|
|
+ if (this.defaultRole != null) {
|
|
|
|
+ roles.add(this.defaultRole);
|
|
}
|
|
}
|
|
|
|
|
|
List<GrantedAuthority> result = new ArrayList<GrantedAuthority>(roles.size());
|
|
List<GrantedAuthority> result = new ArrayList<GrantedAuthority>(roles.size());
|
|
@@ -236,13 +234,13 @@ public class DefaultLdapAuthoritiesPopulator implements LdapAuthoritiesPopulator
|
|
|
|
|
|
if (logger.isDebugEnabled()) {
|
|
if (logger.isDebugEnabled()) {
|
|
logger.debug("Searching for roles for user '" + username + "', DN = " + "'"
|
|
logger.debug("Searching for roles for user '" + username + "', DN = " + "'"
|
|
- + userDn + "', with filter " + groupSearchFilter
|
|
|
|
|
|
+ + userDn + "', with filter " + this.groupSearchFilter
|
|
+ " in search base '" + getGroupSearchBase() + "'");
|
|
+ " in search base '" + getGroupSearchBase() + "'");
|
|
}
|
|
}
|
|
|
|
|
|
Set<String> userRoles = getLdapTemplate().searchForSingleAttributeValues(
|
|
Set<String> userRoles = getLdapTemplate().searchForSingleAttributeValues(
|
|
- getGroupSearchBase(), groupSearchFilter,
|
|
|
|
- new String[] { userDn, username }, groupRoleAttribute);
|
|
|
|
|
|
+ getGroupSearchBase(), this.groupSearchFilter,
|
|
|
|
+ new String[] { userDn, username }, this.groupRoleAttribute);
|
|
|
|
|
|
if (logger.isDebugEnabled()) {
|
|
if (logger.isDebugEnabled()) {
|
|
logger.debug("Roles from search: " + userRoles);
|
|
logger.debug("Roles from search: " + userRoles);
|
|
@@ -250,11 +248,11 @@ public class DefaultLdapAuthoritiesPopulator implements LdapAuthoritiesPopulator
|
|
|
|
|
|
for (String role : userRoles) {
|
|
for (String role : userRoles) {
|
|
|
|
|
|
- if (convertToUpperCase) {
|
|
|
|
|
|
+ if (this.convertToUpperCase) {
|
|
role = role.toUpperCase();
|
|
role = role.toUpperCase();
|
|
}
|
|
}
|
|
|
|
|
|
- authorities.add(new SimpleGrantedAuthority(rolePrefix.getRolePrefix() + role));
|
|
|
|
|
|
+ authorities.add(new SimpleGrantedAuthority(this.rolePrefix + role));
|
|
}
|
|
}
|
|
|
|
|
|
return authorities;
|
|
return authorities;
|
|
@@ -265,7 +263,7 @@ public class DefaultLdapAuthoritiesPopulator implements LdapAuthoritiesPopulator
|
|
}
|
|
}
|
|
|
|
|
|
protected String getGroupSearchBase() {
|
|
protected String getGroupSearchBase() {
|
|
- return groupSearchBase;
|
|
|
|
|
|
+ return this.groupSearchBase;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -301,7 +299,7 @@ public class DefaultLdapAuthoritiesPopulator implements LdapAuthoritiesPopulator
|
|
*/
|
|
*/
|
|
public void setRolePrefix(String rolePrefix) {
|
|
public void setRolePrefix(String rolePrefix) {
|
|
Assert.notNull(rolePrefix, "rolePrefix must not be null");
|
|
Assert.notNull(rolePrefix, "rolePrefix must not be null");
|
|
- this.rolePrefix = new GrantedAuthorityDefaults(rolePrefix);
|
|
|
|
|
|
+ this.rolePrefix = rolePrefix;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -314,7 +312,7 @@ public class DefaultLdapAuthoritiesPopulator implements LdapAuthoritiesPopulator
|
|
public void setSearchSubtree(boolean searchSubtree) {
|
|
public void setSearchSubtree(boolean searchSubtree) {
|
|
int searchScope = searchSubtree ? SearchControls.SUBTREE_SCOPE
|
|
int searchScope = searchSubtree ? SearchControls.SUBTREE_SCOPE
|
|
: SearchControls.ONELEVEL_SCOPE;
|
|
: SearchControls.ONELEVEL_SCOPE;
|
|
- searchControls.setSearchScope(searchScope);
|
|
|
|
|
|
+ this.searchControls.setSearchScope(searchScope);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -334,7 +332,7 @@ public class DefaultLdapAuthoritiesPopulator implements LdapAuthoritiesPopulator
|
|
* @see org.springframework.security.ldap.SpringSecurityLdapTemplate
|
|
* @see org.springframework.security.ldap.SpringSecurityLdapTemplate
|
|
*/
|
|
*/
|
|
protected SpringSecurityLdapTemplate getLdapTemplate() {
|
|
protected SpringSecurityLdapTemplate getLdapTemplate() {
|
|
- return ldapTemplate;
|
|
|
|
|
|
+ return this.ldapTemplate;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -344,7 +342,7 @@ public class DefaultLdapAuthoritiesPopulator implements LdapAuthoritiesPopulator
|
|
* @see #setGroupRoleAttribute(String)
|
|
* @see #setGroupRoleAttribute(String)
|
|
*/
|
|
*/
|
|
protected final String getGroupRoleAttribute() {
|
|
protected final String getGroupRoleAttribute() {
|
|
- return groupRoleAttribute;
|
|
|
|
|
|
+ return this.groupRoleAttribute;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -354,7 +352,7 @@ public class DefaultLdapAuthoritiesPopulator implements LdapAuthoritiesPopulator
|
|
* @see #setGroupSearchFilter(String)
|
|
* @see #setGroupSearchFilter(String)
|
|
*/
|
|
*/
|
|
protected final String getGroupSearchFilter() {
|
|
protected final String getGroupSearchFilter() {
|
|
- return groupSearchFilter;
|
|
|
|
|
|
+ return this.groupSearchFilter;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -364,7 +362,7 @@ public class DefaultLdapAuthoritiesPopulator implements LdapAuthoritiesPopulator
|
|
* @see #setRolePrefix(String)
|
|
* @see #setRolePrefix(String)
|
|
*/
|
|
*/
|
|
protected final String getRolePrefix() {
|
|
protected final String getRolePrefix() {
|
|
- return this.rolePrefix.getRolePrefix();
|
|
|
|
|
|
+ return this.rolePrefix;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -374,7 +372,7 @@ public class DefaultLdapAuthoritiesPopulator implements LdapAuthoritiesPopulator
|
|
* @see #setConvertToUpperCase(boolean)
|
|
* @see #setConvertToUpperCase(boolean)
|
|
*/
|
|
*/
|
|
protected final boolean isConvertToUpperCase() {
|
|
protected final boolean isConvertToUpperCase() {
|
|
- return convertToUpperCase;
|
|
|
|
|
|
+ return this.convertToUpperCase;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -384,7 +382,7 @@ public class DefaultLdapAuthoritiesPopulator implements LdapAuthoritiesPopulator
|
|
* @see #setDefaultRole(String)
|
|
* @see #setDefaultRole(String)
|
|
*/
|
|
*/
|
|
private GrantedAuthority getDefaultRole() {
|
|
private GrantedAuthority getDefaultRole() {
|
|
- return defaultRole;
|
|
|
|
|
|
+ return this.defaultRole;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -393,16 +391,6 @@ public class DefaultLdapAuthoritiesPopulator implements LdapAuthoritiesPopulator
|
|
* @return the search controls
|
|
* @return the search controls
|
|
*/
|
|
*/
|
|
private SearchControls getSearchControls() {
|
|
private SearchControls getSearchControls() {
|
|
- return searchControls;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- @Override
|
|
|
|
- public void setApplicationContext(ApplicationContext context) throws
|
|
|
|
- BeansException {
|
|
|
|
- String[] beanNames = context.getBeanNamesForType(GrantedAuthorityDefaults.class);
|
|
|
|
- if (beanNames.length == 1) {
|
|
|
|
- this.rolePrefix = context.getBean(beanNames[0], GrantedAuthorityDefaults.class);
|
|
|
|
- }
|
|
|
|
|
|
+ return this.searchControls;
|
|
}
|
|
}
|
|
-
|
|
|
|
}
|
|
}
|