01: package org.acegisecurity.config;
02:
03: import java.util.Collections;
04:
05: import org.acegisecurity.AuthenticationManager;
06: import org.acegisecurity.providers.AuthenticationProvider;
07: import org.springframework.beans.BeansException;
08: import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
09: import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
10: import org.springframework.beans.factory.support.ManagedList;
11: import org.springframework.beans.factory.support.RootBeanDefinition;
12: import org.springframework.core.OrderComparator;
13:
14: public class AuthenticationProviderOrderResolver implements
15: BeanFactoryPostProcessor {
16:
17: /**
18: *
19: */
20: public void postProcessBeanFactory(
21: ConfigurableListableBeanFactory beanFactory)
22: throws BeansException {
23: // retrieve all the AuthenticationProvider instances
24: ManagedList providers = retrieveAllAuthenticationProviders(beanFactory);
25: String[] names = beanFactory
26: .getBeanNamesForType(AuthenticationManager.class);
27: RootBeanDefinition definition = (RootBeanDefinition) beanFactory
28: .getBeanDefinition(names[0]);
29: definition.getPropertyValues().addPropertyValue("providers",
30: providers);
31: }
32:
33: /**
34: *
35: * @param beanFactory
36: * @return
37: */
38: private ManagedList retrieveAllAuthenticationProviders(
39: ConfigurableListableBeanFactory beanFactory) {
40: String[] m = beanFactory
41: .getBeanNamesForType(AuthenticationProvider.class);
42: ManagedList l = new ManagedList();
43: for (int i = 0; i < m.length; i++) {
44: RootBeanDefinition def = (RootBeanDefinition) beanFactory
45: .getBeanDefinition(m[i]);
46: l.add(def);
47: }
48: Collections.sort(l, new OrderComparator());
49: return l;
50: }
51:
52: }
|