SEC-534: Refactored JaasAuthenticationProvider to use ApplicationPublisherAware rather than ApplicationContextAware.

This commit is contained in:
Luke Taylor 2007-08-29 11:51:02 +00:00
parent 1467527c0a
commit 5f993e5627
2 changed files with 20 additions and 26 deletions

View File

@ -36,10 +36,7 @@ import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException; import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext; import org.springframework.context.*;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
@ -140,20 +137,20 @@ import javax.security.auth.login.LoginException;
* @author Ray Krueger * @author Ray Krueger
* @version $Id$ * @version $Id$
*/ */
public class JaasAuthenticationProvider implements AuthenticationProvider, InitializingBean, ApplicationContextAware, public class JaasAuthenticationProvider implements AuthenticationProvider, ApplicationEventPublisherAware,
ApplicationListener { InitializingBean, ApplicationListener {
//~ Static fields/initializers ===================================================================================== //~ Static fields/initializers =====================================================================================
protected static final Log log = LogFactory.getLog(JaasAuthenticationProvider.class); protected static final Log log = LogFactory.getLog(JaasAuthenticationProvider.class);
//~ Instance fields ================================================================================================ //~ Instance fields ================================================================================================
private ApplicationContext context;
private LoginExceptionResolver loginExceptionResolver = new DefaultLoginExceptionResolver(); private LoginExceptionResolver loginExceptionResolver = new DefaultLoginExceptionResolver();
private Resource loginConfig; private Resource loginConfig;
private String loginContextName = "ACEGI"; private String loginContextName = "ACEGI";
private AuthorityGranter[] authorityGranters; private AuthorityGranter[] authorityGranters;
private JaasAuthenticationCallbackHandler[] callbackHandlers; private JaasAuthenticationCallbackHandler[] callbackHandlers;
private ApplicationEventPublisher applicationEventPublisher;
//~ Methods ======================================================================================================== //~ Methods ========================================================================================================
@ -246,10 +243,9 @@ public class JaasAuthenticationProvider implements AuthenticationProvider, Initi
* *
* @param loginConfig URL to Jaas login configuration * @param loginConfig URL to Jaas login configuration
* *
* @throws IOException DOCUMENT ME! * @throws IOException if there is a problem reading the config resource.
*/ */
protected void configureJaas(Resource loginConfig) protected void configureJaas(Resource loginConfig) throws IOException {
throws IOException {
configureJaasUsingLoop(); configureJaasUsingLoop();
} }
@ -257,7 +253,6 @@ public class JaasAuthenticationProvider implements AuthenticationProvider, Initi
* Loops through the login.config.url.1,login.config.url.2 properties looking for the login configuration. * Loops through the login.config.url.1,login.config.url.2 properties looking for the login configuration.
* If it is not set, it will be set to the last available login.config.url.X property. * If it is not set, it will be set to the last available login.config.url.X property.
* *
* @throws IOException DOCUMENT ME!
*/ */
private void configureJaasUsingLoop() throws IOException { private void configureJaasUsingLoop() throws IOException {
String loginConfigUrl = loginConfig.getURL().toString(); String loginConfigUrl = loginConfig.getURL().toString();
@ -284,10 +279,6 @@ public class JaasAuthenticationProvider implements AuthenticationProvider, Initi
} }
} }
public ApplicationContext getApplicationContext() {
return context;
}
/** /**
* Returns the AuthorityGrannter array that was passed to the {@link * Returns the AuthorityGrannter array that was passed to the {@link
* #setAuthorityGranters(AuthorityGranter[])} method, or null if it none were ever set. * #setAuthorityGranters(AuthorityGranter[])} method, or null if it none were ever set.
@ -375,7 +366,7 @@ public class JaasAuthenticationProvider implements AuthenticationProvider, Initi
* @param ase The {@link AcegiSecurityException} that caused the failure * @param ase The {@link AcegiSecurityException} that caused the failure
*/ */
protected void publishFailureEvent(UsernamePasswordAuthenticationToken token, AcegiSecurityException ase) { protected void publishFailureEvent(UsernamePasswordAuthenticationToken token, AcegiSecurityException ase) {
getApplicationContext().publishEvent(new JaasAuthenticationFailedEvent(token, ase)); applicationEventPublisher.publishEvent(new JaasAuthenticationFailedEvent(token, ase));
} }
/** /**
@ -385,12 +376,7 @@ public class JaasAuthenticationProvider implements AuthenticationProvider, Initi
* @param token The {@link UsernamePasswordAuthenticationToken} being processed * @param token The {@link UsernamePasswordAuthenticationToken} being processed
*/ */
protected void publishSuccessEvent(UsernamePasswordAuthenticationToken token) { protected void publishSuccessEvent(UsernamePasswordAuthenticationToken token) {
getApplicationContext().publishEvent(new JaasAuthenticationSuccessEvent(token)); applicationEventPublisher.publishEvent(new JaasAuthenticationSuccessEvent(token));
}
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
this.context = applicationContext;
} }
/** /**
@ -445,6 +431,14 @@ public class JaasAuthenticationProvider implements AuthenticationProvider, Initi
return UsernamePasswordAuthenticationToken.class.isAssignableFrom(aClass); return UsernamePasswordAuthenticationToken.class.isAssignableFrom(aClass);
} }
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
this.applicationEventPublisher = applicationEventPublisher;
}
protected ApplicationEventPublisher getApplicationEventPublisher() {
return applicationEventPublisher;
}
//~ Inner Classes ================================================================================================== //~ Inner Classes ==================================================================================================
/** /**

View File

@ -99,7 +99,7 @@ public class JaasAuthenticationProviderTests extends TestCase {
public void testDetectsMissingLoginConfig() throws Exception { public void testDetectsMissingLoginConfig() throws Exception {
JaasAuthenticationProvider myJaasProvider = new JaasAuthenticationProvider(); JaasAuthenticationProvider myJaasProvider = new JaasAuthenticationProvider();
myJaasProvider.setApplicationContext(context); myJaasProvider.setApplicationEventPublisher(context);
myJaasProvider.setAuthorityGranters(jaasProvider.getAuthorityGranters()); myJaasProvider.setAuthorityGranters(jaasProvider.getAuthorityGranters());
myJaasProvider.setCallbackHandlers(jaasProvider.getCallbackHandlers()); myJaasProvider.setCallbackHandlers(jaasProvider.getCallbackHandlers());
myJaasProvider.setLoginContextName(jaasProvider.getLoginContextName()); myJaasProvider.setLoginContextName(jaasProvider.getLoginContextName());
@ -114,7 +114,7 @@ public class JaasAuthenticationProviderTests extends TestCase {
public void testDetectsMissingLoginContextName() throws Exception { public void testDetectsMissingLoginContextName() throws Exception {
JaasAuthenticationProvider myJaasProvider = new JaasAuthenticationProvider(); JaasAuthenticationProvider myJaasProvider = new JaasAuthenticationProvider();
myJaasProvider.setApplicationContext(context); myJaasProvider.setApplicationEventPublisher(context);
myJaasProvider.setAuthorityGranters(jaasProvider.getAuthorityGranters()); myJaasProvider.setAuthorityGranters(jaasProvider.getAuthorityGranters());
myJaasProvider.setCallbackHandlers(jaasProvider.getCallbackHandlers()); myJaasProvider.setCallbackHandlers(jaasProvider.getCallbackHandlers());
myJaasProvider.setLoginConfig(jaasProvider.getLoginConfig()); myJaasProvider.setLoginConfig(jaasProvider.getLoginConfig());
@ -185,8 +185,8 @@ public class JaasAuthenticationProviderTests extends TestCase {
assertNull("Failure event was fired", eventCheck.failedEvent); assertNull("Failure event was fired", eventCheck.failedEvent);
} }
public void testGetApplicationContext() throws Exception { public void testGetApplicationEventPublisher() throws Exception {
assertNotNull(jaasProvider.getApplicationContext()); assertNotNull(jaasProvider.getApplicationEventPublisher());
} }
public void testLoginExceptionResolver() { public void testLoginExceptionResolver() {