列出spring security的全部SecurityFilterChain

本文講述一下如何在運行時列出全部的SecurityFilterChain。html

java

FilterChainProxy chain = (FilterChainProxy) ApplicationContextHolder.getContext().getBean("springSecurityFilterChain");
 List<SecurityFilterChain> filterChains = chain.getFilterChains();
 System.out.println(filterChains);

ApplicationContextHolder

主要是如何直接使用autowired注入,在啓動的時候報錯,所以在徹底啓動以後,從context裏頭取java

@Component
public class ApplicationContextHolder implements ApplicationContextAware {

    private static ApplicationContext ctx;

    public static ApplicationContext getContext() {
        return ctx;
    }


    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        ctx = applicationContext;
    }
}

springSecurityFilterChain

spring-security-config-4.1.4.RELEASE-sources.jar!/org/springframework/security/config/http/HttpSecurityBeanDefinitionParser.javaspring

static void registerFilterChainProxyIfNecessary(ParserContext pc, Object source) {
        if (pc.getRegistry().containsBeanDefinition(BeanIds.FILTER_CHAIN_PROXY)) {
            return;
        }
        // Not already registered, so register the list of filter chains and the
        // FilterChainProxy
        BeanDefinition listFactoryBean = new RootBeanDefinition(ListFactoryBean.class);
        listFactoryBean.getPropertyValues().add("sourceList", new ManagedList());
        pc.registerBeanComponent(new BeanComponentDefinition(listFactoryBean,
                BeanIds.FILTER_CHAINS));

        BeanDefinitionBuilder fcpBldr = BeanDefinitionBuilder
                .rootBeanDefinition(FilterChainProxy.class);
        fcpBldr.getRawBeanDefinition().setSource(source);
        fcpBldr.addConstructorArgReference(BeanIds.FILTER_CHAINS);
        fcpBldr.addPropertyValue("filterChainValidator", new RootBeanDefinition(
                DefaultFilterChainValidator.class));
        BeanDefinition fcpBean = fcpBldr.getBeanDefinition();
        pc.registerBeanComponent(new BeanComponentDefinition(fcpBean,
                BeanIds.FILTER_CHAIN_PROXY));
        pc.getRegistry().registerAlias(BeanIds.FILTER_CHAIN_PROXY,
                BeanIds.SPRING_SECURITY_FILTER_CHAIN);
    }

最後一行,註冊了別名SPRING_SECURITY_FILTER_CHAINapp

doc

相關文章
相關標籤/搜索