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.event.authentication;
17:
18: import org.apache.commons.logging.Log;
19: import org.apache.commons.logging.LogFactory;
20: import org.springframework.context.ApplicationEvent;
21: import org.springframework.context.ApplicationListener;
22: import org.springframework.util.ClassUtils;
23:
24: /**
25: * Outputs authentication-related application events to Commons Logging.<P>All authentication events are logged at
26: * the warning level.</p>
27: *
28: * @author Ben Alex
29: * @version $Id: LoggerListener.java 1845 2007-05-23 06:31:32Z benalex $
30: */
31: public class LoggerListener implements ApplicationListener {
32: //~ Static fields/initializers =====================================================================================
33:
34: private static final Log logger = LogFactory
35: .getLog(LoggerListener.class);
36:
37: /** If set to true, {@link InteractiveAuthenticationSuccessEvent} will be logged (defaults to true) */
38: private boolean logInteractiveAuthenticationSuccessEvents = true;
39:
40: //~ Methods ========================================================================================================
41:
42: public void onApplicationEvent(ApplicationEvent event) {
43: if (event instanceof AbstractAuthenticationEvent) {
44: AbstractAuthenticationEvent authEvent = (AbstractAuthenticationEvent) event;
45:
46: if (!logInteractiveAuthenticationSuccessEvents
47: && authEvent instanceof InteractiveAuthenticationSuccessEvent) {
48: return;
49: }
50:
51: if (logger.isWarnEnabled()) {
52: String message = "Authentication event "
53: + ClassUtils.getShortName(authEvent.getClass())
54: + ": "
55: + authEvent.getAuthentication().getName()
56: + "; details: "
57: + authEvent.getAuthentication().getDetails();
58:
59: if (event instanceof AbstractAuthenticationFailureEvent) {
60: message = message
61: + "; exception: "
62: + ((AbstractAuthenticationFailureEvent) event)
63: .getException().getMessage();
64: }
65:
66: logger.warn(message);
67: }
68: }
69: }
70:
71: public boolean isLogInteractiveAuthenticationSuccessEvents() {
72: return logInteractiveAuthenticationSuccessEvents;
73: }
74:
75: public void setLogInteractiveAuthenticationSuccessEvents(
76: boolean logInteractiveAuthenticationSuccessEvents) {
77: this.logInteractiveAuthenticationSuccessEvents = logInteractiveAuthenticationSuccessEvents;
78: }
79: }
|