列出spring security的所有SecurityFilterChain

0

本文讲述一下如何在运行时列出所有的SecurityFilterChain。

java

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

ApplicationContextHolder

主要是如何直接使用autowired注入,在启动的时候报错,因此在完全启动之后,从context里头取

@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.java

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_CHAIN

doc


如果觉得我的文章对你有用,请随意赞赏

你可能感兴趣的

载入中...