| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
package org.acegisecurity.providers.x509; |
| 17 |
|
|
| 18 |
|
import org.acegisecurity.AcegiMessageSource; |
| 19 |
|
import org.acegisecurity.Authentication; |
| 20 |
|
import org.acegisecurity.AuthenticationException; |
| 21 |
|
import org.acegisecurity.BadCredentialsException; |
| 22 |
|
|
| 23 |
|
import org.acegisecurity.providers.AuthenticationProvider; |
| 24 |
|
import org.acegisecurity.providers.x509.cache.NullX509UserCache; |
| 25 |
|
|
| 26 |
|
import org.acegisecurity.userdetails.UserDetails; |
| 27 |
|
|
| 28 |
|
import org.apache.commons.logging.Log; |
| 29 |
|
import org.apache.commons.logging.LogFactory; |
| 30 |
|
|
| 31 |
|
import org.springframework.beans.factory.InitializingBean; |
| 32 |
|
|
| 33 |
|
import org.springframework.context.MessageSource; |
| 34 |
|
import org.springframework.context.MessageSourceAware; |
| 35 |
|
import org.springframework.context.support.MessageSourceAccessor; |
| 36 |
|
|
| 37 |
|
import org.springframework.util.Assert; |
| 38 |
|
|
| 39 |
|
import java.security.cert.X509Certificate; |
| 40 |
|
|
| 41 |
|
|
| 42 |
|
|
| 43 |
|
|
| 44 |
|
|
| 45 |
|
|
| 46 |
|
|
| 47 |
|
|
| 48 |
|
|
| 49 |
5 |
public class X509AuthenticationProvider implements AuthenticationProvider, InitializingBean, MessageSourceAware { |
| 50 |
|
|
| 51 |
|
|
| 52 |
3 |
private static final Log logger = LogFactory.getLog(X509AuthenticationProvider.class); |
| 53 |
|
|
| 54 |
|
|
| 55 |
|
|
| 56 |
5 |
protected MessageSourceAccessor messages = AcegiMessageSource.getAccessor(); |
| 57 |
|
private X509AuthoritiesPopulator x509AuthoritiesPopulator; |
| 58 |
5 |
private X509UserCache userCache = new NullX509UserCache(); |
| 59 |
|
|
| 60 |
|
|
| 61 |
|
|
| 62 |
|
public void afterPropertiesSet() throws Exception { |
| 63 |
2 |
Assert.notNull(userCache, "An x509UserCache must be set"); |
| 64 |
2 |
Assert.notNull(x509AuthoritiesPopulator, "An X509AuthoritiesPopulator must be set"); |
| 65 |
1 |
Assert.notNull(this.messages, "A message source must be set"); |
| 66 |
1 |
} |
| 67 |
|
|
| 68 |
|
|
| 69 |
|
|
| 70 |
|
|
| 71 |
|
|
| 72 |
|
|
| 73 |
|
|
| 74 |
|
|
| 75 |
|
|
| 76 |
|
|
| 77 |
|
|
| 78 |
|
|
| 79 |
|
|
| 80 |
|
|
| 81 |
|
|
| 82 |
|
public Authentication authenticate(Authentication authentication) |
| 83 |
|
throws AuthenticationException { |
| 84 |
4 |
if (!supports(authentication.getClass())) { |
| 85 |
1 |
return null; |
| 86 |
|
} |
| 87 |
|
|
| 88 |
3 |
if (logger.isDebugEnabled()) { |
| 89 |
0 |
logger.debug("X509 authentication request: " + authentication); |
| 90 |
|
} |
| 91 |
|
|
| 92 |
3 |
X509Certificate clientCertificate = (X509Certificate) authentication.getCredentials(); |
| 93 |
|
|
| 94 |
3 |
if (clientCertificate == null) { |
| 95 |
1 |
throw new BadCredentialsException(messages.getMessage("X509AuthenticationProvider.certificateNull", |
| 96 |
|
"Certificate is null")); |
| 97 |
|
} |
| 98 |
|
|
| 99 |
2 |
UserDetails user = userCache.getUserFromCache(clientCertificate); |
| 100 |
|
|
| 101 |
2 |
if (user == null) { |
| 102 |
2 |
logger.debug("Authenticating with certificate " + clientCertificate); |
| 103 |
2 |
user = x509AuthoritiesPopulator.getUserDetails(clientCertificate); |
| 104 |
1 |
userCache.putUserInCache(clientCertificate, user); |
| 105 |
|
} |
| 106 |
|
|
| 107 |
1 |
X509AuthenticationToken result = new X509AuthenticationToken(user, clientCertificate, user.getAuthorities()); |
| 108 |
|
|
| 109 |
1 |
result.setDetails(authentication.getDetails()); |
| 110 |
|
|
| 111 |
1 |
return result; |
| 112 |
|
} |
| 113 |
|
|
| 114 |
|
public void setMessageSource(MessageSource messageSource) { |
| 115 |
0 |
this.messages = new MessageSourceAccessor(messageSource); |
| 116 |
0 |
} |
| 117 |
|
|
| 118 |
|
public void setX509AuthoritiesPopulator(X509AuthoritiesPopulator x509AuthoritiesPopulator) { |
| 119 |
3 |
this.x509AuthoritiesPopulator = x509AuthoritiesPopulator; |
| 120 |
3 |
} |
| 121 |
|
|
| 122 |
|
public void setX509UserCache(X509UserCache cache) { |
| 123 |
0 |
this.userCache = cache; |
| 124 |
0 |
} |
| 125 |
|
|
| 126 |
|
public boolean supports(Class authentication) { |
| 127 |
4 |
return X509AuthenticationToken.class.isAssignableFrom(authentication); |
| 128 |
|
} |
| 129 |
|
} |