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