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