|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| package org.acegisecurity.providers; |
|
17 |
| |
|
18 |
| import java.lang.reflect.Constructor; |
|
19 |
| import java.lang.reflect.InvocationTargetException; |
|
20 |
| import java.util.Iterator; |
|
21 |
| import java.util.List; |
|
22 |
| import java.util.Properties; |
|
23 |
| |
|
24 |
| import org.acegisecurity.AbstractAuthenticationManager; |
|
25 |
| import org.acegisecurity.AccountExpiredException; |
|
26 |
| import org.acegisecurity.AcegiMessageSource; |
|
27 |
| import org.acegisecurity.Authentication; |
|
28 |
| import org.acegisecurity.AuthenticationException; |
|
29 |
| import org.acegisecurity.AuthenticationServiceException; |
|
30 |
| import org.acegisecurity.BadCredentialsException; |
|
31 |
| import org.acegisecurity.CredentialsExpiredException; |
|
32 |
| import org.acegisecurity.DisabledException; |
|
33 |
| import org.acegisecurity.LockedException; |
|
34 |
| import org.acegisecurity.concurrent.ConcurrentLoginException; |
|
35 |
| import org.acegisecurity.concurrent.ConcurrentSessionController; |
|
36 |
| import org.acegisecurity.concurrent.NullConcurrentSessionController; |
|
37 |
| import org.acegisecurity.event.authentication.AbstractAuthenticationEvent; |
|
38 |
| import org.acegisecurity.event.authentication.AuthenticationFailureBadCredentialsEvent; |
|
39 |
| import org.acegisecurity.event.authentication.AuthenticationFailureConcurrentLoginEvent; |
|
40 |
| import org.acegisecurity.event.authentication.AuthenticationFailureCredentialsExpiredEvent; |
|
41 |
| import org.acegisecurity.event.authentication.AuthenticationFailureDisabledEvent; |
|
42 |
| import org.acegisecurity.event.authentication.AuthenticationFailureExpiredEvent; |
|
43 |
| import org.acegisecurity.event.authentication.AuthenticationFailureLockedEvent; |
|
44 |
| import org.acegisecurity.event.authentication.AuthenticationFailureProviderNotFoundEvent; |
|
45 |
| import org.acegisecurity.event.authentication.AuthenticationFailureProxyUntrustedEvent; |
|
46 |
| import org.acegisecurity.event.authentication.AuthenticationFailureServiceExceptionEvent; |
|
47 |
| import org.acegisecurity.event.authentication.AuthenticationSuccessEvent; |
|
48 |
| import org.acegisecurity.providers.cas.ProxyUntrustedException; |
|
49 |
| import org.acegisecurity.userdetails.UsernameNotFoundException; |
|
50 |
| import org.apache.commons.logging.Log; |
|
51 |
| import org.apache.commons.logging.LogFactory; |
|
52 |
| import org.springframework.beans.factory.InitializingBean; |
|
53 |
| import org.springframework.context.ApplicationEventPublisher; |
|
54 |
| import org.springframework.context.ApplicationEventPublisherAware; |
|
55 |
| import org.springframework.context.MessageSource; |
|
56 |
| import org.springframework.context.MessageSourceAware; |
|
57 |
| import org.springframework.context.support.MessageSourceAccessor; |
|
58 |
| import org.springframework.util.Assert; |
|
59 |
| |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
| |
|
64 |
| |
|
65 |
| |
|
66 |
| |
|
67 |
| |
|
68 |
| |
|
69 |
| |
|
70 |
| |
|
71 |
| |
|
72 |
| |
|
73 |
| |
|
74 |
| |
|
75 |
| |
|
76 |
| |
|
77 |
| |
|
78 |
| |
|
79 |
| |
|
80 |
| |
|
81 |
| |
|
82 |
| |
|
83 |
| |
|
84 |
| |
|
85 |
| |
|
86 |
| |
|
87 |
| |
|
88 |
| |
|
89 |
| |
|
90 |
| |
|
91 |
| |
|
92 |
| |
|
93 |
| |
|
94 |
| |
|
95 |
| |
|
96 |
| |
|
97 |
| |
|
98 |
| |
|
99 |
| |
|
100 |
| |
|
101 |
| |
|
102 |
| public class ProviderManager extends AbstractAuthenticationManager |
|
103 |
| implements InitializingBean, ApplicationEventPublisherAware, |
|
104 |
| MessageSourceAware { |
|
105 |
| |
|
106 |
| |
|
107 |
| private static final Log logger = LogFactory.getLog(ProviderManager.class); |
|
108 |
| |
|
109 |
| |
|
110 |
| |
|
111 |
| private ApplicationEventPublisher applicationEventPublisher; |
|
112 |
| private ConcurrentSessionController sessionController = new NullConcurrentSessionController(); |
|
113 |
| private List providers; |
|
114 |
| protected MessageSourceAccessor messages = AcegiMessageSource.getAccessor(); |
|
115 |
| private Properties exceptionMappings; |
|
116 |
| |
|
117 |
| |
|
118 |
| |
|
119 |
39
| public void afterPropertiesSet() throws Exception {
|
|
120 |
39
| checkIfValidList(this.providers);
|
|
121 |
38
| Assert.notNull(this.messages, "A message source must be set");
|
|
122 |
| |
|
123 |
38
| if (exceptionMappings == null) {
|
|
124 |
37
| exceptionMappings = new Properties();
|
|
125 |
37
| exceptionMappings.put(AccountExpiredException.class.getName(),
|
|
126 |
| AuthenticationFailureExpiredEvent.class.getName()); |
|
127 |
37
| exceptionMappings.put(AuthenticationServiceException.class.getName(),
|
|
128 |
| AuthenticationFailureServiceExceptionEvent.class.getName()); |
|
129 |
37
| exceptionMappings.put(LockedException.class.getName(),
|
|
130 |
| AuthenticationFailureLockedEvent.class.getName()); |
|
131 |
37
| exceptionMappings.put(CredentialsExpiredException.class.getName(),
|
|
132 |
| AuthenticationFailureCredentialsExpiredEvent.class.getName()); |
|
133 |
37
| exceptionMappings.put(DisabledException.class.getName(),
|
|
134 |
| AuthenticationFailureDisabledEvent.class.getName()); |
|
135 |
37
| exceptionMappings.put(BadCredentialsException.class.getName(),
|
|
136 |
| AuthenticationFailureBadCredentialsEvent.class.getName()); |
|
137 |
37
| exceptionMappings.put(UsernameNotFoundException.class.getName(),
|
|
138 |
| AuthenticationFailureBadCredentialsEvent.class.getName()); |
|
139 |
37
| exceptionMappings.put(ConcurrentLoginException.class.getName(),
|
|
140 |
| AuthenticationFailureConcurrentLoginEvent.class.getName()); |
|
141 |
37
| exceptionMappings.put(ProviderNotFoundException.class.getName(),
|
|
142 |
| AuthenticationFailureProviderNotFoundEvent.class.getName()); |
|
143 |
37
| exceptionMappings.put(ProxyUntrustedException.class.getName(),
|
|
144 |
| AuthenticationFailureProxyUntrustedEvent.class.getName()); |
|
145 |
37
| doAddExtraDefaultExceptionMappings(exceptionMappings);
|
|
146 |
| } |
|
147 |
| } |
|
148 |
| |
|
149 |
79
| private void checkIfValidList(List listToCheck) {
|
|
150 |
79
| if ((listToCheck == null) || (listToCheck.size() == 0)) {
|
|
151 |
2
| throw new IllegalArgumentException(
|
|
152 |
| "A list of AuthenticationManagers is required"); |
|
153 |
| } |
|
154 |
| } |
|
155 |
| |
|
156 |
| |
|
157 |
| |
|
158 |
| |
|
159 |
| |
|
160 |
| |
|
161 |
| |
|
162 |
| |
|
163 |
37
| protected void doAddExtraDefaultExceptionMappings(
|
|
164 |
| Properties exceptionMappings) {} |
|
165 |
| |
|
166 |
| |
|
167 |
| |
|
168 |
| |
|
169 |
| |
|
170 |
| |
|
171 |
| |
|
172 |
| |
|
173 |
| |
|
174 |
| |
|
175 |
| |
|
176 |
| |
|
177 |
| |
|
178 |
| |
|
179 |
| |
|
180 |
| |
|
181 |
| |
|
182 |
| |
|
183 |
| |
|
184 |
| |
|
185 |
| |
|
186 |
| |
|
187 |
| |
|
188 |
| |
|
189 |
| |
|
190 |
8
| public Authentication doAuthentication(Authentication authentication)
|
|
191 |
| throws AuthenticationException { |
|
192 |
8
| Iterator iter = providers.iterator();
|
|
193 |
| |
|
194 |
8
| Class toTest = authentication.getClass();
|
|
195 |
| |
|
196 |
8
| AuthenticationException lastException = null;
|
|
197 |
| |
|
198 |
8
| while (iter.hasNext()) {
|
|
199 |
9
| AuthenticationProvider provider = (AuthenticationProvider) iter.next();
|
|
200 |
| |
|
201 |
9
| if (provider.supports(toTest)) {
|
|
202 |
8
| logger.debug("Authentication attempt using "
|
|
203 |
| + provider.getClass().getName()); |
|
204 |
| |
|
205 |
8
| Authentication result = null;
|
|
206 |
| |
|
207 |
8
| try {
|
|
208 |
8
| result = provider.authenticate(authentication);
|
|
209 |
5
| sessionController.checkAuthenticationAllowed(result);
|
|
210 |
| } catch (AuthenticationException ae) { |
|
211 |
3
| lastException = ae;
|
|
212 |
3
| result = null;
|
|
213 |
| } |
|
214 |
| |
|
215 |
8
| if (result != null) {
|
|
216 |
4
| sessionController.registerSuccessfulAuthentication(result);
|
|
217 |
4
| applicationEventPublisher.publishEvent(new AuthenticationSuccessEvent(
|
|
218 |
| result)); |
|
219 |
| |
|
220 |
4
| return result;
|
|
221 |
| } |
|
222 |
| } |
|
223 |
| } |
|
224 |
| |
|
225 |
4
| if (lastException == null) {
|
|
226 |
1
| lastException = new ProviderNotFoundException(messages.getMessage(
|
|
227 |
| "ProviderManager.providerNotFound", |
|
228 |
| new Object[] {toTest.getName()}, |
|
229 |
| "No AuthenticationProvider found for {0}")); |
|
230 |
| } |
|
231 |
| |
|
232 |
| |
|
233 |
4
| String className = exceptionMappings.getProperty(lastException.getClass()
|
|
234 |
| .getName()); |
|
235 |
4
| AbstractAuthenticationEvent event = null;
|
|
236 |
| |
|
237 |
4
| if (className != null) {
|
|
238 |
4
| try {
|
|
239 |
4
| Class clazz = getClass().getClassLoader().loadClass(className);
|
|
240 |
4
| Constructor constructor = clazz.getConstructor(new Class[] {Authentication.class, AuthenticationException.class});
|
|
241 |
4
| Object obj = constructor.newInstance(new Object[] {authentication, lastException});
|
|
242 |
4
| Assert.isInstanceOf(AbstractAuthenticationEvent.class, obj,
|
|
243 |
| "Must be an AbstractAuthenticationEvent"); |
|
244 |
4
| event = (AbstractAuthenticationEvent) obj;
|
|
245 |
| } catch (ClassNotFoundException ignored) {} |
|
246 |
| catch (NoSuchMethodException ignored) {} |
|
247 |
| catch (IllegalAccessException ignored) {} |
|
248 |
| catch (InstantiationException ignored) {} |
|
249 |
| catch (InvocationTargetException ignored) {} |
|
250 |
| } |
|
251 |
| |
|
252 |
4
| if (event != null) {
|
|
253 |
4
| applicationEventPublisher.publishEvent(event);
|
|
254 |
| } else { |
|
255 |
0
| if (logger.isDebugEnabled()) {
|
|
256 |
0
| logger.debug("No event was found for the exception "
|
|
257 |
| + lastException.getClass().getName()); |
|
258 |
| } |
|
259 |
| } |
|
260 |
| |
|
261 |
| |
|
262 |
4
| throw lastException;
|
|
263 |
| } |
|
264 |
| |
|
265 |
1
| public List getProviders() {
|
|
266 |
1
| return this.providers;
|
|
267 |
| } |
|
268 |
| |
|
269 |
| |
|
270 |
| |
|
271 |
| |
|
272 |
| |
|
273 |
| |
|
274 |
| |
|
275 |
| |
|
276 |
3
| public ConcurrentSessionController getSessionController() {
|
|
277 |
3
| return sessionController;
|
|
278 |
| } |
|
279 |
| |
|
280 |
37
| public void setApplicationEventPublisher(
|
|
281 |
| ApplicationEventPublisher applicationEventPublisher) { |
|
282 |
37
| this.applicationEventPublisher = applicationEventPublisher;
|
|
283 |
| } |
|
284 |
| |
|
285 |
34
| public void setMessageSource(MessageSource messageSource) {
|
|
286 |
34
| this.messages = new MessageSourceAccessor(messageSource);
|
|
287 |
| } |
|
288 |
| |
|
289 |
| |
|
290 |
| |
|
291 |
| |
|
292 |
| |
|
293 |
| |
|
294 |
| |
|
295 |
| |
|
296 |
| |
|
297 |
40
| public void setProviders(List newList) {
|
|
298 |
40
| checkIfValidList(newList);
|
|
299 |
| |
|
300 |
39
| Iterator iter = newList.iterator();
|
|
301 |
| |
|
302 |
39
| while (iter.hasNext()) {
|
|
303 |
40
| Object currentObject = null;
|
|
304 |
| |
|
305 |
40
| try {
|
|
306 |
40
| currentObject = iter.next();
|
|
307 |
| |
|
308 |
40
| AuthenticationProvider attemptToCast = (AuthenticationProvider) currentObject;
|
|
309 |
| } catch (ClassCastException cce) { |
|
310 |
1
| throw new IllegalArgumentException("AuthenticationProvider "
|
|
311 |
| + currentObject.getClass().getName() |
|
312 |
| + " must implement AuthenticationProvider"); |
|
313 |
| } |
|
314 |
| } |
|
315 |
| |
|
316 |
38
| this.providers = newList;
|
|
317 |
| } |
|
318 |
| |
|
319 |
| |
|
320 |
| |
|
321 |
| |
|
322 |
| |
|
323 |
| |
|
324 |
| |
|
325 |
| |
|
326 |
1
| public void setSessionController(
|
|
327 |
| ConcurrentSessionController sessionController) { |
|
328 |
1
| this.sessionController = sessionController;
|
|
329 |
| } |
|
330 |
| } |