| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
package org.acegisecurity.userdetails.ldap; |
| 17 |
|
|
| 18 |
|
import org.acegisecurity.GrantedAuthorityImpl; |
| 19 |
|
import org.acegisecurity.GrantedAuthority; |
| 20 |
|
|
| 21 |
|
import org.acegisecurity.ldap.LdapEntryMapper; |
| 22 |
|
|
| 23 |
|
import org.apache.commons.logging.Log; |
| 24 |
|
import org.apache.commons.logging.LogFactory; |
| 25 |
|
|
| 26 |
|
import org.springframework.util.Assert; |
| 27 |
|
|
| 28 |
|
import javax.naming.NamingEnumeration; |
| 29 |
|
import javax.naming.NamingException; |
| 30 |
|
import javax.naming.directory.Attribute; |
| 31 |
|
import javax.naming.directory.Attributes; |
| 32 |
|
|
| 33 |
|
|
| 34 |
|
|
| 35 |
|
|
| 36 |
|
|
| 37 |
|
|
| 38 |
|
|
| 39 |
|
|
| 40 |
30 |
public class LdapUserDetailsMapper implements LdapEntryMapper { |
| 41 |
|
|
| 42 |
|
|
| 43 |
31 |
private final Log logger = LogFactory.getLog(LdapUserDetailsMapper.class); |
| 44 |
30 |
private String passwordAttributeName = "userPassword"; |
| 45 |
30 |
private String rolePrefix = "ROLE_"; |
| 46 |
30 |
private String[] roleAttributes = null; |
| 47 |
30 |
private boolean convertToUpperCase = true; |
| 48 |
|
|
| 49 |
|
|
| 50 |
|
|
| 51 |
|
public Object mapAttributes(String dn, Attributes attributes) |
| 52 |
|
throws NamingException { |
| 53 |
18 |
LdapUserDetailsImpl.Essence essence = new LdapUserDetailsImpl.Essence(); |
| 54 |
|
|
| 55 |
18 |
essence.setDn(dn); |
| 56 |
18 |
essence.setAttributes(attributes); |
| 57 |
|
|
| 58 |
18 |
Attribute passwordAttribute = attributes.get(passwordAttributeName); |
| 59 |
|
|
| 60 |
18 |
if (passwordAttribute != null) { |
| 61 |
14 |
essence.setPassword(mapPassword(passwordAttribute)); |
| 62 |
|
} |
| 63 |
|
|
| 64 |
|
|
| 65 |
23 |
for (int i = 0; (roleAttributes != null) && (i < roleAttributes.length); i++) { |
| 66 |
5 |
Attribute roleAttribute = attributes.get(roleAttributes[i]); |
| 67 |
|
|
| 68 |
5 |
if (roleAttribute == null) { |
| 69 |
1 |
logger.debug("Couldn't read role attribute '" + roleAttributes[i] + "' for user " + dn); |
| 70 |
1 |
continue; |
| 71 |
|
} |
| 72 |
|
|
| 73 |
4 |
NamingEnumeration attributeRoles = roleAttribute.getAll(); |
| 74 |
|
|
| 75 |
10 |
while (attributeRoles.hasMore()) { |
| 76 |
6 |
GrantedAuthority authority = createAuthority(attributeRoles.next()); |
| 77 |
|
|
| 78 |
6 |
if (authority != null) { |
| 79 |
5 |
essence.addAuthority(authority); |
| 80 |
|
} else { |
| 81 |
1 |
logger.debug("Failed to create an authority value from attribute with Id: " |
| 82 |
|
+ roleAttribute.getID()); |
| 83 |
|
} |
| 84 |
6 |
} |
| 85 |
|
} |
| 86 |
|
|
| 87 |
18 |
return essence; |
| 88 |
|
} |
| 89 |
|
|
| 90 |
|
|
| 91 |
|
|
| 92 |
|
|
| 93 |
|
|
| 94 |
|
|
| 95 |
|
|
| 96 |
|
|
| 97 |
|
protected String mapPassword(Attribute passwordAttribute) throws NamingException { |
| 98 |
14 |
Object retrievedPassword = passwordAttribute.get(); |
| 99 |
|
|
| 100 |
14 |
if (!(retrievedPassword instanceof String)) { |
| 101 |
|
|
| 102 |
13 |
retrievedPassword = new String((byte[]) retrievedPassword); |
| 103 |
|
} |
| 104 |
|
|
| 105 |
14 |
return (String) retrievedPassword; |
| 106 |
|
|
| 107 |
|
} |
| 108 |
|
|
| 109 |
|
|
| 110 |
|
|
| 111 |
|
|
| 112 |
|
|
| 113 |
|
|
| 114 |
|
|
| 115 |
|
|
| 116 |
|
|
| 117 |
|
|
| 118 |
|
|
| 119 |
|
|
| 120 |
|
|
| 121 |
|
protected GrantedAuthority createAuthority(Object role) { |
| 122 |
6 |
if (role instanceof String) { |
| 123 |
5 |
if (convertToUpperCase) { |
| 124 |
2 |
role = ((String) role).toUpperCase(); |
| 125 |
|
} |
| 126 |
5 |
return new GrantedAuthorityImpl(rolePrefix + role); |
| 127 |
|
} |
| 128 |
1 |
return null; |
| 129 |
|
} |
| 130 |
|
|
| 131 |
|
|
| 132 |
|
|
| 133 |
|
|
| 134 |
|
|
| 135 |
|
|
| 136 |
|
|
| 137 |
|
public void setConvertToUpperCase(boolean convertToUpperCase) { |
| 138 |
1 |
this.convertToUpperCase = convertToUpperCase; |
| 139 |
1 |
} |
| 140 |
|
|
| 141 |
|
|
| 142 |
|
|
| 143 |
|
|
| 144 |
|
|
| 145 |
|
|
| 146 |
|
|
| 147 |
|
public void setPasswordAttributeName(String passwordAttributeName) { |
| 148 |
2 |
this.passwordAttributeName = passwordAttributeName; |
| 149 |
2 |
} |
| 150 |
|
|
| 151 |
|
|
| 152 |
|
|
| 153 |
|
|
| 154 |
|
|
| 155 |
|
|
| 156 |
|
|
| 157 |
|
|
| 158 |
|
public void setRoleAttributes(String[] roleAttributes) { |
| 159 |
4 |
Assert.notNull(roleAttributes, "roleAttributes array cannot be null"); |
| 160 |
4 |
this.roleAttributes = roleAttributes; |
| 161 |
4 |
} |
| 162 |
|
|
| 163 |
|
|
| 164 |
|
|
| 165 |
|
|
| 166 |
|
|
| 167 |
|
public void setRolePrefix(String rolePrefix) { |
| 168 |
1 |
this.rolePrefix = rolePrefix; |
| 169 |
1 |
} |
| 170 |
|
} |