|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| package org.acegisecurity.ui.x509; |
|
17 |
| |
|
18 |
| import org.acegisecurity.Authentication; |
|
19 |
| import org.acegisecurity.AuthenticationException; |
|
20 |
| import org.acegisecurity.AuthenticationManager; |
|
21 |
| import org.acegisecurity.context.SecurityContextHolder; |
|
22 |
| import org.acegisecurity.event.authentication.InteractiveAuthenticationSuccessEvent; |
|
23 |
| import org.acegisecurity.providers.x509.X509AuthenticationToken; |
|
24 |
| import org.acegisecurity.ui.AbstractProcessingFilter; |
|
25 |
| import org.acegisecurity.ui.WebAuthenticationDetails; |
|
26 |
| |
|
27 |
| import org.apache.commons.logging.Log; |
|
28 |
| import org.apache.commons.logging.LogFactory; |
|
29 |
| |
|
30 |
| import org.springframework.beans.factory.InitializingBean; |
|
31 |
| |
|
32 |
| import org.springframework.context.ApplicationEventPublisherAware; |
|
33 |
| import org.springframework.context.ApplicationEventPublisher; |
|
34 |
| |
|
35 |
| import org.springframework.util.Assert; |
|
36 |
| |
|
37 |
| import java.io.IOException; |
|
38 |
| |
|
39 |
| import java.security.cert.X509Certificate; |
|
40 |
| |
|
41 |
| import javax.servlet.*; |
|
42 |
| import javax.servlet.http.HttpServletRequest; |
|
43 |
| import javax.servlet.http.HttpServletResponse; |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| |
|
54 |
| |
|
55 |
| |
|
56 |
| |
|
57 |
| |
|
58 |
| |
|
59 |
| |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
| |
|
64 |
| |
|
65 |
| |
|
66 |
| |
|
67 |
| |
|
68 |
| |
|
69 |
| |
|
70 |
| |
|
71 |
| |
|
72 |
| |
|
73 |
| |
|
74 |
| |
|
75 |
| |
|
76 |
| |
|
77 |
| |
|
78 |
| public class X509ProcessingFilter implements Filter, InitializingBean, |
|
79 |
| ApplicationEventPublisherAware { |
|
80 |
| |
|
81 |
| |
|
82 |
| private static final Log logger = LogFactory.getLog(X509ProcessingFilter.class); |
|
83 |
| |
|
84 |
| |
|
85 |
| |
|
86 |
| private ApplicationEventPublisher eventPublisher; |
|
87 |
| private AuthenticationManager authenticationManager; |
|
88 |
| |
|
89 |
| |
|
90 |
| |
|
91 |
0
| public void setApplicationEventPublisher(ApplicationEventPublisher context) {
|
|
92 |
0
| this.eventPublisher = context;
|
|
93 |
| } |
|
94 |
| |
|
95 |
3
| public void setAuthenticationManager(
|
|
96 |
| AuthenticationManager authenticationManager) { |
|
97 |
3
| this.authenticationManager = authenticationManager;
|
|
98 |
| } |
|
99 |
| |
|
100 |
3
| public void afterPropertiesSet() throws Exception {
|
|
101 |
3
| Assert.notNull(authenticationManager,
|
|
102 |
| "An AuthenticationManager must be set"); |
|
103 |
| } |
|
104 |
| |
|
105 |
2
| public void destroy() {}
|
|
106 |
| |
|
107 |
| |
|
108 |
| |
|
109 |
| |
|
110 |
| |
|
111 |
| |
|
112 |
| |
|
113 |
| |
|
114 |
| |
|
115 |
| |
|
116 |
| |
|
117 |
| |
|
118 |
| |
|
119 |
| |
|
120 |
| |
|
121 |
| |
|
122 |
| |
|
123 |
| |
|
124 |
| |
|
125 |
| |
|
126 |
| |
|
127 |
| |
|
128 |
| |
|
129 |
| |
|
130 |
| |
|
131 |
| |
|
132 |
5
| public void doFilter(ServletRequest request, ServletResponse response,
|
|
133 |
| FilterChain filterChain) throws IOException, ServletException { |
|
134 |
5
| if (!(request instanceof HttpServletRequest)) {
|
|
135 |
1
| throw new ServletException("Can only process HttpServletRequest");
|
|
136 |
| } |
|
137 |
| |
|
138 |
4
| if (!(response instanceof HttpServletResponse)) {
|
|
139 |
1
| throw new ServletException("Can only process HttpServletResponse");
|
|
140 |
| } |
|
141 |
| |
|
142 |
3
| HttpServletRequest httpRequest = (HttpServletRequest) request;
|
|
143 |
3
| HttpServletResponse httpResponse = (HttpServletResponse) response;
|
|
144 |
| |
|
145 |
3
| if (logger.isDebugEnabled()) {
|
|
146 |
0
| logger.debug("Checking secure context token: "
|
|
147 |
| + SecurityContextHolder.getContext().getAuthentication()); |
|
148 |
| } |
|
149 |
| |
|
150 |
3
| if (SecurityContextHolder.getContext().getAuthentication() == null) {
|
|
151 |
3
| Authentication authResult = null;
|
|
152 |
3
| X509Certificate clientCertificate = extractClientCertificate(httpRequest);
|
|
153 |
| |
|
154 |
3
| try {
|
|
155 |
3
| X509AuthenticationToken authRequest = new X509AuthenticationToken(clientCertificate);
|
|
156 |
| |
|
157 |
3
| authRequest.setDetails(new WebAuthenticationDetails(httpRequest));
|
|
158 |
3
| authResult = authenticationManager.authenticate(authRequest);
|
|
159 |
1
| successfulAuthentication(httpRequest, httpResponse, authResult);
|
|
160 |
| } catch (AuthenticationException failed) { |
|
161 |
2
| unsuccessfulAuthentication(httpRequest, httpResponse, failed);
|
|
162 |
| } |
|
163 |
| } |
|
164 |
| |
|
165 |
3
| filterChain.doFilter(request, response);
|
|
166 |
| } |
|
167 |
| |
|
168 |
2
| public void init(FilterConfig ignored) throws ServletException {}
|
|
169 |
| |
|
170 |
| |
|
171 |
| |
|
172 |
| |
|
173 |
| |
|
174 |
| |
|
175 |
| |
|
176 |
| |
|
177 |
| |
|
178 |
| |
|
179 |
| |
|
180 |
1
| protected void successfulAuthentication(HttpServletRequest request,
|
|
181 |
| HttpServletResponse response, Authentication authResult) |
|
182 |
| throws IOException { |
|
183 |
1
| if (logger.isDebugEnabled()) {
|
|
184 |
0
| logger.debug("Authentication success: " + authResult);
|
|
185 |
| } |
|
186 |
| |
|
187 |
1
| SecurityContextHolder.getContext().setAuthentication(authResult);
|
|
188 |
| |
|
189 |
| |
|
190 |
1
| if (this.eventPublisher != null) {
|
|
191 |
0
| eventPublisher.publishEvent(new InteractiveAuthenticationSuccessEvent(
|
|
192 |
| authResult, this.getClass())); |
|
193 |
| } |
|
194 |
| } |
|
195 |
| |
|
196 |
| |
|
197 |
| |
|
198 |
| |
|
199 |
| |
|
200 |
| |
|
201 |
| |
|
202 |
| |
|
203 |
| |
|
204 |
2
| protected void unsuccessfulAuthentication(HttpServletRequest request,
|
|
205 |
| HttpServletResponse response, AuthenticationException failed) { |
|
206 |
2
| SecurityContextHolder.getContext().setAuthentication(null);
|
|
207 |
| |
|
208 |
2
| if (logger.isDebugEnabled()) {
|
|
209 |
0
| logger.debug("Updated SecurityContextHolder to contain null Authentication");
|
|
210 |
| } |
|
211 |
| |
|
212 |
2
| request.getSession().setAttribute(AbstractProcessingFilter.ACEGI_SECURITY_LAST_EXCEPTION_KEY,
|
|
213 |
| failed); |
|
214 |
| } |
|
215 |
| |
|
216 |
3
| private X509Certificate extractClientCertificate(HttpServletRequest request) {
|
|
217 |
3
| X509Certificate[] certs = (X509Certificate[]) request.getAttribute(
|
|
218 |
| "javax.servlet.request.X509Certificate"); |
|
219 |
| |
|
220 |
3
| if ((certs != null) && (certs.length > 0)) {
|
|
221 |
2
| return certs[0];
|
|
222 |
| } |
|
223 |
| |
|
224 |
1
| if (logger.isDebugEnabled()) {
|
|
225 |
0
| logger.debug("No client certificate found in request.");
|
|
226 |
| } |
|
227 |
| |
|
228 |
1
| return null;
|
|
229 |
| } |
|
230 |
| } |