1
1
/*
2
- * Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
2
+ * Copyright 2002-2023 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
29
29
import org .springframework .core .log .LogMessage ;
30
30
import org .springframework .security .authentication .AnonymousAuthenticationToken ;
31
31
import org .springframework .security .authentication .AuthenticationDetailsSource ;
32
- import org .springframework .security .authentication .UsernamePasswordAuthenticationToken ;
33
32
import org .springframework .security .authentication .event .InteractiveAuthenticationSuccessEvent ;
34
33
import org .springframework .security .cas .ServiceProperties ;
34
+ import org .springframework .security .cas .authentication .CasServiceTicketAuthenticationToken ;
35
35
import org .springframework .security .cas .web .authentication .ServiceAuthenticationDetails ;
36
36
import org .springframework .security .cas .web .authentication .ServiceAuthenticationDetailsSource ;
37
37
import org .springframework .security .core .Authentication ;
41
41
import org .springframework .security .web .authentication .AbstractAuthenticationProcessingFilter ;
42
42
import org .springframework .security .web .authentication .AuthenticationFailureHandler ;
43
43
import org .springframework .security .web .authentication .SimpleUrlAuthenticationFailureHandler ;
44
+ import org .springframework .security .web .context .HttpSessionSecurityContextRepository ;
44
45
import org .springframework .security .web .util .matcher .AntPathRequestMatcher ;
45
46
import org .springframework .security .web .util .matcher .RequestMatcher ;
46
47
import org .springframework .util .Assert ;
63
64
* <tt>filterProcessesUrl</tt>.
64
65
* <p>
65
66
* Processing the service ticket involves creating a
66
- * <code>UsernamePasswordAuthenticationToken </code> which uses
67
- * {@link #CAS_STATEFUL_IDENTIFIER} for the <code>principal</code> and the opaque ticket
68
- * string as the <code>credentials</code>.
67
+ * <code>CasServiceTicketAuthenticationToken </code> which uses
68
+ * {@link CasServiceTicketAuthenticationToken #CAS_STATEFUL_IDENTIFIER} for the
69
+ * <code>principal</code> and the opaque ticket string as the <code>credentials</code>.
69
70
* <h2>Obtaining Proxy Granting Tickets</h2>
70
71
* <p>
71
72
* If specified, the filter can also monitor the <code>proxyReceptorUrl</code>. The filter
88
89
* {@link ServiceAuthenticationDetails#getServiceUrl()} will be used for the service url.
89
90
* <p>
90
91
* Processing the proxy ticket involves creating a
91
- * <code>UsernamePasswordAuthenticationToken </code> which uses
92
- * {@link #CAS_STATELESS_IDENTIFIER} for the <code>principal</code> and the opaque ticket
93
- * string as the <code>credentials</code>. When a proxy ticket is successfully
94
- * authenticated, the FilterChain continues and the
92
+ * <code>CasServiceTicketAuthenticationToken </code> which uses
93
+ * {@link CasServiceTicketAuthenticationToken #CAS_STATELESS_IDENTIFIER} for the
94
+ * <code>principal</code> and the opaque ticket string as the <code>credentials</code>.
95
+ * When a proxy ticket is successfully authenticated, the FilterChain continues and the
95
96
* <code>authenticationSuccessHandler</code> is not used.
96
97
* <h2>Notes about the <code>AuthenticationManager</code></h2>
97
98
* <p>
98
99
* The configured <code>AuthenticationManager</code> is expected to provide a provider
99
- * that can recognise <code>UsernamePasswordAuthenticationToken </code>s containing this
100
+ * that can recognise <code>CasServiceTicketAuthenticationToken </code>s containing this
100
101
* special <code>principal</code> name, and process them accordingly by validation with
101
102
* the CAS server. Additionally, it should be capable of using the result of
102
103
* {@link ServiceAuthenticationDetails#getServiceUrl()} as the service when validating the
175
176
*/
176
177
public class CasAuthenticationFilter extends AbstractAuthenticationProcessingFilter {
177
178
178
- /**
179
- * Used to identify a CAS request for a stateful user agent, such as a web browser.
180
- */
181
- public static final String CAS_STATEFUL_IDENTIFIER = "_cas_stateful_" ;
182
-
183
- /**
184
- * Used to identify a CAS request for a stateless user agent, such as a remoting
185
- * protocol client (e.g. Hessian, Burlap, SOAP etc). Results in a more aggressive
186
- * caching strategy being used, as the absence of a <code>HttpSession</code> will
187
- * result in a new authentication attempt on every request.
188
- */
189
- public static final String CAS_STATELESS_IDENTIFIER = "_cas_stateless_" ;
190
-
191
179
/**
192
180
* The last portion of the receptor url, i.e. /proxy/receptor
193
181
*/
@@ -207,6 +195,7 @@ public class CasAuthenticationFilter extends AbstractAuthenticationProcessingFil
207
195
public CasAuthenticationFilter () {
208
196
super ("/login/cas" );
209
197
setAuthenticationFailureHandler (new SimpleUrlAuthenticationFailureHandler ());
198
+ setSecurityContextRepository (new HttpSessionSecurityContextRepository ());
210
199
}
211
200
212
201
@ Override
@@ -238,14 +227,15 @@ public Authentication attemptAuthentication(HttpServletRequest request, HttpServ
238
227
CommonUtils .readAndRespondToProxyReceptorRequest (request , response , this .proxyGrantingTicketStorage );
239
228
return null ;
240
229
}
241
- boolean serviceTicketRequest = serviceTicketRequest (request , response );
242
- String username = serviceTicketRequest ? CAS_STATEFUL_IDENTIFIER : CAS_STATELESS_IDENTIFIER ;
243
- String password = obtainArtifact (request );
244
- if (password == null ) {
230
+ String serviceTicket = obtainArtifact (request );
231
+ if (serviceTicket == null ) {
245
232
this .logger .debug ("Failed to obtain an artifact (cas ticket)" );
246
- password = "" ;
233
+ serviceTicket = "" ;
247
234
}
248
- UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken (username , password );
235
+ boolean serviceTicketRequest = serviceTicketRequest (request , response );
236
+ CasServiceTicketAuthenticationToken authRequest = serviceTicketRequest
237
+ ? CasServiceTicketAuthenticationToken .stateful (serviceTicket )
238
+ : CasServiceTicketAuthenticationToken .stateless (serviceTicket );
249
239
authRequest .setDetails (this .authenticationDetailsSource .buildDetails (request ));
250
240
return this .getAuthenticationManager ().authenticate (authRequest );
251
241
}
0 commit comments