01: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
02: *
03: * Licensed under the Apache License, Version 2.0 (the "License");
04: * you may not use this file except in compliance with the License.
05: * You may obtain a copy of the License at
06: *
07: * http://www.apache.org/licenses/LICENSE-2.0
08: *
09: * Unless required by applicable law or agreed to in writing, software
10: * distributed under the License is distributed on an "AS IS" BASIS,
11: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12: * See the License for the specific language governing permissions and
13: * limitations under the License.
14: */
15:
16: package org.acegisecurity.adapters.cas3;
17:
18: import org.acegisecurity.Authentication;
19: import org.acegisecurity.AuthenticationManager;
20:
21: import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
22:
23: import org.apache.commons.logging.Log;
24: import org.apache.commons.logging.LogFactory;
25:
26: import org.jasig.cas.authentication.handler.AuthenticationException;
27: import org.jasig.cas.authentication.handler.AuthenticationHandler;
28: import org.jasig.cas.authentication.handler.support.AbstractUsernamePasswordAuthenticationHandler;
29: import org.jasig.cas.authentication.principal.UsernamePasswordCredentials;
30:
31: import org.springframework.util.Assert;
32:
33: /**
34: * <p>Provides JA-SIG CAS 3 authentication by delegating to the Acegi <code>AuthenticationManager</code>.</p>
35: * <p>This class would be configured in the <code>webapp/WEB-INF/deployerConfigContext.xml</code> file in the CAS
36: * distribution.</p>
37: *
38: * @author Scott Battaglia
39: * @version $Id: CasAuthenticationHandler.java 1852 2007-05-23 06:59:20Z benalex $
40: *
41: * @see AuthenticationHandler
42: * @see AuthenticationManager
43: */
44: public final class CasAuthenticationHandler extends
45: AbstractUsernamePasswordAuthenticationHandler {
46: //~ Instance fields ================================================================================================
47:
48: private AuthenticationManager authenticationManager;
49: private Log log = LogFactory.getLog(this .getClass());
50:
51: //~ Methods ========================================================================================================
52:
53: protected void afterPropertiesSetInternal() throws Exception {
54: Assert.notNull(this .authenticationManager,
55: "authenticationManager cannot be null.");
56: }
57:
58: protected boolean authenticateUsernamePasswordInternal(
59: final UsernamePasswordCredentials credentials)
60: throws AuthenticationException {
61: final Authentication authenticationRequest = new UsernamePasswordAuthenticationToken(
62: credentials.getUsername(), credentials.getPassword());
63:
64: if (log.isDebugEnabled()) {
65: log.debug("Attempting to authenticate for user: "
66: + credentials.getUsername());
67: }
68:
69: try {
70: this .authenticationManager
71: .authenticate(authenticationRequest);
72: } catch (final org.acegisecurity.AuthenticationException e) {
73: if (log.isDebugEnabled()) {
74: log.debug("Authentication request for "
75: + credentials.getUsername() + " failed: "
76: + e.toString(), e);
77: }
78:
79: return false;
80: }
81:
82: if (log.isDebugEnabled()) {
83: log.debug("Authentication request for "
84: + credentials.getUsername() + " successful.");
85: }
86:
87: return true;
88: }
89:
90: /**
91: * Method to set the Acegi <code>AuthenticationManager</code> to delegate to.
92: *
93: * @param authenticationManager the Acegi AuthenticationManager that knows how to authenticate users.
94: */
95: public void setAuthenticationManager(
96: final AuthenticationManager authenticationManager) {
97: this.authenticationManager = authenticationManager;
98: }
99: }
|