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.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.*;
import org.springframework.core.io.Resource;
@ -140,20 +137,20 @@ import javax.security.auth.login.LoginException;
* @author Ray Krueger
* @version $Id$
*/
public class JaasAuthenticationProvider implements AuthenticationProvider, InitializingBean, ApplicationContextAware,
ApplicationListener {
public class JaasAuthenticationProvider implements AuthenticationProvider, ApplicationEventPublisherAware,
InitializingBean, ApplicationListener {
//~ Static fields/initializers =====================================================================================
protected static final Log log = LogFactory.getLog(JaasAuthenticationProvider.class);
//~ Instance fields ================================================================================================
private ApplicationContext context;
private LoginExceptionResolver loginExceptionResolver = new DefaultLoginExceptionResolver();
private Resource loginConfig;
private String loginContextName = "ACEGI";
private AuthorityGranter[] authorityGranters;
private JaasAuthenticationCallbackHandler[] callbackHandlers;
private ApplicationEventPublisher applicationEventPublisher;
//~ Methods ========================================================================================================
@ -246,10 +243,9 @@ public class JaasAuthenticationProvider implements AuthenticationProvider, Initi
*
* @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)
throws IOException {
protected void configureJaas(Resource loginConfig) throws IOException {
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.
* 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 {
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
* #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
*/
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
*/
protected void publishSuccessEvent(UsernamePasswordAuthenticationToken token) {
getApplicationContext().publishEvent(new JaasAuthenticationSuccessEvent(token));
}
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
this.context = applicationContext;
applicationEventPublisher.publishEvent(new JaasAuthenticationSuccessEvent(token));
}
/**
@ -445,6 +431,14 @@ public class JaasAuthenticationProvider implements AuthenticationProvider, Initi
return UsernamePasswordAuthenticationToken.class.isAssignableFrom(aClass);
}
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
this.applicationEventPublisher = applicationEventPublisher;
}
protected ApplicationEventPublisher getApplicationEventPublisher() {
return applicationEventPublisher;
}
//~ Inner Classes ==================================================================================================
/**

View File

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