[58] | 1 | /* Copyright 2006-2009 the original author or authors. |
---|
| 2 | * |
---|
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
---|
| 4 | * you may not use this file except in compliance with the License. |
---|
| 5 | * You may obtain a copy of the License at |
---|
| 6 | * |
---|
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
| 8 | * |
---|
| 9 | * 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 | package org.codehaus.groovy.grails.plugins.springsecurity |
---|
| 16 | |
---|
| 17 | import org.grails.plugins.springsecurity.service.AuthenticateService |
---|
| 18 | |
---|
| 19 | import org.springframework.context.ApplicationContext |
---|
| 20 | import org.springframework.context.ApplicationContextAware |
---|
| 21 | import org.springframework.context.ApplicationEvent |
---|
| 22 | import org.springframework.context.ApplicationListener |
---|
| 23 | import org.springframework.security.event.authentication.AbstractAuthenticationEvent |
---|
| 24 | import org.springframework.security.event.authentication.AbstractAuthenticationFailureEvent |
---|
| 25 | import org.springframework.security.event.authentication.AuthenticationSuccessEvent |
---|
| 26 | import org.springframework.security.event.authentication.AuthenticationSwitchUserEvent |
---|
| 27 | import org.springframework.security.event.authentication.InteractiveAuthenticationSuccessEvent |
---|
| 28 | import org.springframework.security.event.authorization.AbstractAuthorizationEvent |
---|
| 29 | |
---|
| 30 | /** |
---|
| 31 | * Registers as an event listener and delegates handling of security-related events |
---|
| 32 | * to optional closures defined in SecurityConfig.groovy. |
---|
| 33 | * <p/> |
---|
| 34 | * The following callbacks are supported:<br/> |
---|
| 35 | * <ul> |
---|
| 36 | * <li>onInteractiveAuthenticationSuccessEvent</li> |
---|
| 37 | * <li>onAbstractAuthenticationFailureEvent</li> |
---|
| 38 | * <li>onAuthenticationSuccessEvent</li> |
---|
| 39 | * <li>onAuthenticationSwitchUserEvent</li> |
---|
| 40 | * <li>onAuthorizationEvent</li> |
---|
| 41 | * </ul> |
---|
| 42 | * All callbacks are optional; you can implement just the ones you're interested in, e.g. |
---|
| 43 | * <pre> |
---|
| 44 | * security { |
---|
| 45 | * active = true |
---|
| 46 | * |
---|
| 47 | * onAuthenticationSuccessEvent = { e, appCtx -> |
---|
| 48 | * ... |
---|
| 49 | * } |
---|
| 50 | * } |
---|
| 51 | * </pre> |
---|
| 52 | * The event and the Spring context are provided in case you need to look up a Spring bean, |
---|
| 53 | * e.g. the Hibernate SessionFactory. |
---|
| 54 | * |
---|
| 55 | * @author <a href='mailto:beckwithb@studentsonly.com'>Burt Beckwith</a> |
---|
| 56 | */ |
---|
| 57 | class SecurityEventListener implements ApplicationListener, ApplicationContextAware { |
---|
| 58 | |
---|
| 59 | private ApplicationContext _applicationContext; |
---|
| 60 | |
---|
| 61 | /** |
---|
| 62 | * Dependency injection for AuthenticateService. |
---|
| 63 | */ |
---|
| 64 | AuthenticateService authenticateService |
---|
| 65 | |
---|
| 66 | /** |
---|
| 67 | * {@inheritDoc} |
---|
| 68 | * @see org.springframework.context.ApplicationListener#onApplicationEvent( |
---|
| 69 | * org.springframework.context.ApplicationEvent) |
---|
| 70 | */ |
---|
| 71 | void onApplicationEvent(ApplicationEvent e) { |
---|
| 72 | if (e instanceof AbstractAuthenticationEvent) { |
---|
| 73 | if (e instanceof InteractiveAuthenticationSuccessEvent) { |
---|
| 74 | if (config.onInteractiveAuthenticationSuccessEvent) { |
---|
| 75 | config.onInteractiveAuthenticationSuccessEvent.call( |
---|
| 76 | (InteractiveAuthenticationSuccessEvent)e, _applicationContext) |
---|
| 77 | } |
---|
| 78 | } |
---|
| 79 | else if (e instanceof AbstractAuthenticationFailureEvent) { |
---|
| 80 | if (config.onAbstractAuthenticationFailureEvent) { |
---|
| 81 | config.onAbstractAuthenticationFailureEvent.call( |
---|
| 82 | (AbstractAuthenticationFailureEvent)e, _applicationContext) |
---|
| 83 | } |
---|
| 84 | } |
---|
| 85 | else if (e instanceof AuthenticationSuccessEvent) { |
---|
| 86 | if (config.onAuthenticationSuccessEvent) { |
---|
| 87 | config.onAuthenticationSuccessEvent.call( |
---|
| 88 | (AuthenticationSuccessEvent)e, _applicationContext) |
---|
| 89 | } |
---|
| 90 | } |
---|
| 91 | else if (e instanceof AuthenticationSwitchUserEvent) { |
---|
| 92 | if (config.onAuthenticationSwitchUserEvent) { |
---|
| 93 | // GrailsUser userInfo = (GrailsUser)event.getAuthentication().getPrincipal() |
---|
| 94 | // UserDetails userDetails = event.getTargetUser() |
---|
| 95 | config.onAuthenticationSwitchUserEvent.call( |
---|
| 96 | (AuthenticationSwitchUserEvent)e, _applicationContext) |
---|
| 97 | } |
---|
| 98 | } |
---|
| 99 | } |
---|
| 100 | else if (e instanceof AbstractAuthorizationEvent) { |
---|
| 101 | if (config.onAuthorizationEvent) { |
---|
| 102 | config.onAuthorizationEvent.call( |
---|
| 103 | (AbstractAuthorizationEvent)e, _applicationContext) |
---|
| 104 | } |
---|
| 105 | } |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | /** |
---|
| 109 | * {@inheritDoc} |
---|
| 110 | * @see org.springframework.context.ApplicationContextAware#setApplicationContext( |
---|
| 111 | * org.springframework.context.ApplicationContext) |
---|
| 112 | */ |
---|
| 113 | void setApplicationContext(ApplicationContext applicationContext) { |
---|
| 114 | _applicationContext = applicationContext; |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | private def getConfig() { |
---|
| 118 | return authenticateService.securityConfig.security |
---|
| 119 | } |
---|
| 120 | } |
---|