| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
package org.acegisecurity.providers.ldap.authenticator; |
| 17 |
|
|
| 18 |
|
import org.acegisecurity.AcegiMessageSource; |
| 19 |
|
|
| 20 |
|
import org.acegisecurity.ldap.InitialDirContextFactory; |
| 21 |
|
import org.acegisecurity.ldap.LdapEntryMapper; |
| 22 |
|
import org.acegisecurity.ldap.LdapUserSearch; |
| 23 |
|
|
| 24 |
|
import org.acegisecurity.providers.ldap.LdapAuthenticator; |
| 25 |
|
|
| 26 |
|
import org.acegisecurity.userdetails.ldap.LdapUserDetailsMapper; |
| 27 |
|
|
| 28 |
|
import org.springframework.beans.factory.InitializingBean; |
| 29 |
|
|
| 30 |
|
import org.springframework.context.MessageSource; |
| 31 |
|
import org.springframework.context.MessageSourceAware; |
| 32 |
|
import org.springframework.context.support.MessageSourceAccessor; |
| 33 |
|
|
| 34 |
|
import org.springframework.util.Assert; |
| 35 |
|
|
| 36 |
|
import java.text.MessageFormat; |
| 37 |
|
|
| 38 |
|
import java.util.ArrayList; |
| 39 |
|
import java.util.List; |
| 40 |
|
|
| 41 |
|
|
| 42 |
|
|
| 43 |
|
|
| 44 |
|
|
| 45 |
|
|
| 46 |
|
|
| 47 |
|
|
| 48 |
|
public abstract class AbstractLdapAuthenticator implements LdapAuthenticator, InitializingBean, MessageSourceAware { |
| 49 |
|
|
| 50 |
|
|
| 51 |
|
private InitialDirContextFactory initialDirContextFactory; |
| 52 |
19 |
private LdapUserDetailsMapper userDetailsMapper = new LdapUserDetailsMapper(); |
| 53 |
|
|
| 54 |
|
|
| 55 |
|
private LdapUserSearch userSearch; |
| 56 |
19 |
protected MessageSourceAccessor messages = AcegiMessageSource.getAccessor(); |
| 57 |
|
|
| 58 |
|
|
| 59 |
|
|
| 60 |
|
|
| 61 |
|
|
| 62 |
19 |
private String dnSuffix = ""; |
| 63 |
|
|
| 64 |
|
|
| 65 |
19 |
private String[] userAttributes = null; |
| 66 |
|
|
| 67 |
|
|
| 68 |
|
|
| 69 |
19 |
private MessageFormat[] userDnFormat = null; |
| 70 |
|
|
| 71 |
|
|
| 72 |
|
|
| 73 |
|
|
| 74 |
|
|
| 75 |
|
|
| 76 |
|
|
| 77 |
|
|
| 78 |
19 |
public AbstractLdapAuthenticator(InitialDirContextFactory initialDirContextFactory) { |
| 79 |
19 |
this.setInitialDirContextFactory(initialDirContextFactory); |
| 80 |
19 |
} |
| 81 |
|
|
| 82 |
|
|
| 83 |
|
|
| 84 |
|
|
| 85 |
|
public void afterPropertiesSet() throws Exception { |
| 86 |
2 |
Assert.isTrue((userDnFormat != null) || (userSearch != null), |
| 87 |
|
"Either an LdapUserSearch or DN pattern (or both) must be supplied."); |
| 88 |
2 |
} |
| 89 |
|
|
| 90 |
|
|
| 91 |
|
|
| 92 |
|
|
| 93 |
|
|
| 94 |
|
|
| 95 |
|
private void setInitialDirContextFactory(InitialDirContextFactory initialDirContextFactory) { |
| 96 |
19 |
Assert.notNull(initialDirContextFactory, "initialDirContextFactory must not be null."); |
| 97 |
19 |
this.initialDirContextFactory = initialDirContextFactory; |
| 98 |
|
|
| 99 |
19 |
String rootDn = initialDirContextFactory.getRootDn(); |
| 100 |
|
|
| 101 |
19 |
if (rootDn.length() > 0) { |
| 102 |
19 |
dnSuffix = "," + rootDn; |
| 103 |
|
} |
| 104 |
19 |
} |
| 105 |
|
|
| 106 |
|
protected InitialDirContextFactory getInitialDirContextFactory() { |
| 107 |
15 |
return initialDirContextFactory; |
| 108 |
|
} |
| 109 |
|
|
| 110 |
|
public String[] getUserAttributes() { |
| 111 |
13 |
return userAttributes; |
| 112 |
|
} |
| 113 |
|
|
| 114 |
|
protected LdapEntryMapper getUserDetailsMapper() { |
| 115 |
13 |
return userDetailsMapper; |
| 116 |
|
} |
| 117 |
|
|
| 118 |
|
|
| 119 |
|
|
| 120 |
|
|
| 121 |
|
|
| 122 |
|
|
| 123 |
|
|
| 124 |
|
|
| 125 |
|
|
| 126 |
|
|
| 127 |
|
protected List getUserDns(String username) { |
| 128 |
18 |
if (userDnFormat == null) { |
| 129 |
5 |
return new ArrayList(0); |
| 130 |
|
} |
| 131 |
|
|
| 132 |
13 |
List userDns = new ArrayList(userDnFormat.length); |
| 133 |
13 |
String[] args = new String[] {username}; |
| 134 |
|
|
| 135 |
13 |
synchronized (userDnFormat) { |
| 136 |
27 |
for (int i = 0; i < userDnFormat.length; i++) { |
| 137 |
14 |
userDns.add(userDnFormat[i].format(args) + dnSuffix); |
| 138 |
|
} |
| 139 |
13 |
} |
| 140 |
|
|
| 141 |
13 |
return userDns; |
| 142 |
|
} |
| 143 |
|
|
| 144 |
|
protected LdapUserSearch getUserSearch() { |
| 145 |
8 |
return userSearch; |
| 146 |
|
} |
| 147 |
|
|
| 148 |
|
public void setMessageSource(MessageSource messageSource) { |
| 149 |
6 |
Assert.notNull("Message source must not be null"); |
| 150 |
6 |
this.messages = new MessageSourceAccessor(messageSource); |
| 151 |
6 |
} |
| 152 |
|
|
| 153 |
|
|
| 154 |
|
|
| 155 |
|
|
| 156 |
|
|
| 157 |
|
|
| 158 |
|
public void setUserAttributes(String[] userAttributes) { |
| 159 |
1 |
Assert.notNull(userAttributes, "The userAttributes property cannot be set to null"); |
| 160 |
1 |
this.userAttributes = userAttributes; |
| 161 |
1 |
} |
| 162 |
|
|
| 163 |
|
public void setUserDetailsMapper(LdapUserDetailsMapper userDetailsMapper) { |
| 164 |
2 |
Assert.notNull("userDetailsMapper must not be null"); |
| 165 |
2 |
this.userDetailsMapper = userDetailsMapper; |
| 166 |
2 |
} |
| 167 |
|
|
| 168 |
|
|
| 169 |
|
|
| 170 |
|
|
| 171 |
|
|
| 172 |
|
|
| 173 |
|
|
| 174 |
|
|
| 175 |
|
public void setUserDnPatterns(String[] dnPattern) { |
| 176 |
17 |
Assert.notNull(dnPattern, "The array of DN patterns cannot be set to null"); |
| 177 |
|
|
| 178 |
17 |
userDnFormat = new MessageFormat[dnPattern.length]; |
| 179 |
|
|
| 180 |
35 |
for (int i = 0; i < dnPattern.length; i++) { |
| 181 |
18 |
userDnFormat[i] = new MessageFormat(dnPattern[i]); |
| 182 |
|
} |
| 183 |
17 |
} |
| 184 |
|
|
| 185 |
|
public void setUserSearch(LdapUserSearch userSearch) { |
| 186 |
3 |
Assert.notNull(userSearch, "The userSearch cannot be set to null"); |
| 187 |
3 |
this.userSearch = userSearch; |
| 188 |
3 |
} |
| 189 |
|
} |