1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.springframework.security.providers.ldap.authenticator;
17
18 import org.springframework.security.SpringSecurityMessageSource;
19 import org.springframework.security.ldap.LdapUserSearch;
20 import org.springframework.security.providers.ldap.LdapAuthenticator;
21 import org.springframework.beans.factory.InitializingBean;
22 import org.springframework.context.MessageSource;
23 import org.springframework.context.MessageSourceAware;
24 import org.springframework.context.support.MessageSourceAccessor;
25 import org.springframework.ldap.core.ContextSource;
26 import org.springframework.util.Assert;
27
28 import java.text.MessageFormat;
29 import java.util.ArrayList;
30 import java.util.Collections;
31 import java.util.List;
32
33
34
35
36
37
38
39
40 public abstract class AbstractLdapAuthenticator implements LdapAuthenticator, InitializingBean, MessageSourceAware {
41
42
43 private ContextSource contextSource;
44
45
46 private LdapUserSearch userSearch;
47 protected MessageSourceAccessor messages = SpringSecurityMessageSource.getAccessor();
48
49
50 private String[] userAttributes = null;
51
52
53
54 private MessageFormat[] userDnFormat = null;
55
56
57
58
59
60
61
62
63 public AbstractLdapAuthenticator(ContextSource contextSource) {
64 Assert.notNull(contextSource, "contextSource must not be null.");
65 this.contextSource = contextSource;
66 }
67
68
69
70 public void afterPropertiesSet() throws Exception {
71 Assert.isTrue((userDnFormat != null) || (userSearch != null),
72 "Either an LdapUserSearch or DN pattern (or both) must be supplied.");
73 }
74
75 protected ContextSource getContextSource() {
76 return contextSource;
77 }
78
79 public String[] getUserAttributes() {
80 return userAttributes;
81 }
82
83
84
85
86
87
88
89
90 protected List getUserDns(String username) {
91 if (userDnFormat == null) {
92 return Collections.EMPTY_LIST;
93 }
94
95 List userDns = new ArrayList(userDnFormat.length);
96 String[] args = new String[] {username};
97
98 synchronized (userDnFormat) {
99 for (int i = 0; i < userDnFormat.length; i++) {
100 userDns.add(userDnFormat[i].format(args));
101 }
102 }
103
104 return userDns;
105 }
106
107 protected LdapUserSearch getUserSearch() {
108 return userSearch;
109 }
110
111 public void setMessageSource(MessageSource messageSource) {
112 Assert.notNull("Message source must not be null");
113 this.messages = new MessageSourceAccessor(messageSource);
114 }
115
116
117
118
119
120
121 public void setUserAttributes(String[] userAttributes) {
122 Assert.notNull(userAttributes, "The userAttributes property cannot be set to null");
123 this.userAttributes = userAttributes;
124 }
125
126
127
128
129
130
131
132 public void setUserDnPatterns(String[] dnPattern) {
133 Assert.notNull(dnPattern, "The array of DN patterns cannot be set to null");
134
135 userDnFormat = new MessageFormat[dnPattern.length];
136
137 for (int i = 0; i < dnPattern.length; i++) {
138 userDnFormat[i] = new MessageFormat(dnPattern[i]);
139 }
140 }
141
142 public void setUserSearch(LdapUserSearch userSearch) {
143 Assert.notNull(userSearch, "The userSearch cannot be set to null");
144 this.userSearch = userSearch;
145 }
146 }