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 private LdapUserDetailsMapper userDetailsMapper = new LdapUserDetailsMapper();
53
54
55 private LdapUserSearch userSearch;
56 protected MessageSourceAccessor messages = AcegiMessageSource.getAccessor();
57
58
59
60
61
62 private String dnSuffix = "";
63
64
65 private String[] userAttributes = null;
66
67
68
69 private MessageFormat[] userDnFormat = null;
70
71
72
73
74
75
76
77
78 public AbstractLdapAuthenticator(InitialDirContextFactory initialDirContextFactory) {
79 this.setInitialDirContextFactory(initialDirContextFactory);
80 }
81
82
83
84
85 public void afterPropertiesSet() throws Exception {
86 Assert.isTrue((userDnFormat != null) || (userSearch != null),
87 "Either an LdapUserSearch or DN pattern (or both) must be supplied.");
88 }
89
90
91
92
93
94
95 private void setInitialDirContextFactory(InitialDirContextFactory initialDirContextFactory) {
96 Assert.notNull(initialDirContextFactory, "initialDirContextFactory must not be null.");
97 this.initialDirContextFactory = initialDirContextFactory;
98
99 String rootDn = initialDirContextFactory.getRootDn();
100
101 if (rootDn.length() > 0) {
102 dnSuffix = "," + rootDn;
103 }
104 }
105
106 protected InitialDirContextFactory getInitialDirContextFactory() {
107 return initialDirContextFactory;
108 }
109
110 public String[] getUserAttributes() {
111 return userAttributes;
112 }
113
114 protected LdapEntryMapper getUserDetailsMapper() {
115 return userDetailsMapper;
116 }
117
118
119
120
121
122
123
124
125
126
127 protected List getUserDns(String username) {
128 if (userDnFormat == null) {
129 return new ArrayList(0);
130 }
131
132 List userDns = new ArrayList(userDnFormat.length);
133 String[] args = new String[] {username};
134
135 synchronized (userDnFormat) {
136 for (int i = 0; i < userDnFormat.length; i++) {
137 userDns.add(userDnFormat[i].format(args) + dnSuffix);
138 }
139 }
140
141 return userDns;
142 }
143
144 protected LdapUserSearch getUserSearch() {
145 return userSearch;
146 }
147
148 public void setMessageSource(MessageSource messageSource) {
149 Assert.notNull("Message source must not be null");
150 this.messages = new MessageSourceAccessor(messageSource);
151 }
152
153
154
155
156
157
158 public void setUserAttributes(String[] userAttributes) {
159 Assert.notNull(userAttributes, "The userAttributes property cannot be set to null");
160 this.userAttributes = userAttributes;
161 }
162
163 public void setUserDetailsMapper(LdapUserDetailsMapper userDetailsMapper) {
164 Assert.notNull("userDetailsMapper must not be null");
165 this.userDetailsMapper = userDetailsMapper;
166 }
167
168
169
170
171
172
173
174
175 public void setUserDnPatterns(String[] dnPattern) {
176 Assert.notNull(dnPattern, "The array of DN patterns cannot be set to null");
177
178 userDnFormat = new MessageFormat[dnPattern.length];
179
180 for (int i = 0; i < dnPattern.length; i++) {
181 userDnFormat[i] = new MessageFormat(dnPattern[i]);
182 }
183 }
184
185 public void setUserSearch(LdapUserSearch userSearch) {
186 Assert.notNull(userSearch, "The userSearch cannot be set to null");
187 this.userSearch = userSearch;
188 }
189 }