Spring Security的解析处理器的载入

来源:互联网 发布:手绘动画制作软件 编辑:程序博客网 时间:2024/05/29 15:31

Spring Security通过在spring配置文件中引入SpringSecurity的命名空间,应用启动之后会载入SpringSecurity的命名空间对应的命名空间处理器进行初始化载入解析器的操作。

其对应的命名空间处理器是org.springframework.security.config.SecurityNamespaceHandler。

其载入解析器的方法是

    private void loadParsers() {        // Parsers        parsers.put(Elements.LDAP_PROVIDER, new LdapProviderBeanDefinitionParser());        parsers.put(Elements.LDAP_SERVER, new LdapServerBeanDefinitionParser());        parsers.put(Elements.LDAP_USER_SERVICE, new LdapUserServiceBeanDefinitionParser());        parsers.put(Elements.USER_SERVICE, new UserServiceBeanDefinitionParser());        parsers.put(Elements.JDBC_USER_SERVICE, new JdbcUserServiceBeanDefinitionParser());        parsers.put(Elements.AUTHENTICATION_PROVIDER, new AuthenticationProviderBeanDefinitionParser());        parsers.put(Elements.GLOBAL_METHOD_SECURITY, new GlobalMethodSecurityBeanDefinitionParser());        parsers.put(Elements.AUTHENTICATION_MANAGER, new AuthenticationManagerBeanDefinitionParser());        parsers.put(Elements.METHOD_SECURITY_METADATA_SOURCE, new MethodSecurityMetadataSourceBeanDefinitionParser());        // Only load the web-namespace parsers if the web classes are available        if(ClassUtils.isPresent(FILTER_CHAIN_PROXY_CLASSNAME, getClass().getClassLoader())) {            parsers.put(Elements.DEBUG, new DebugBeanDefinitionParser());            parsers.put(Elements.HTTP, new HttpSecurityBeanDefinitionParser());            parsers.put(Elements.HTTP_FIREWALL, new HttpFirewallBeanDefinitionParser());            parsers.put(Elements.FILTER_INVOCATION_DEFINITION_SOURCE, new FilterInvocationSecurityMetadataSourceParser());            parsers.put(Elements.FILTER_SECURITY_METADATA_SOURCE, new FilterInvocationSecurityMetadataSourceParser());            parsers.put(Elements.FILTER_CHAIN, new FilterChainBeanDefinitionParser());            filterChainMapBDD = new FilterChainMapBeanDefinitionDecorator();        }    }
解析器的方法的入口
   public BeanDefinition parse(Element element, ParserContext pc) {        if (!namespaceMatchesVersion(element)) {            pc.getReaderContext().fatal("You cannot use a spring-security-2.0.xsd or spring-security-3.0.xsd schema " +                    "with Spring Security 3.1. Please update your schema declarations to the 3.1 schema.", element);        }        //获得xml元素的名称       String name = pc.getDelegate().getLocalName(element);        //通过名称获得对应的解析器       BeanDefinitionParser parser = parsers.get(name);        if (parser == null) {            // SEC-1455. Load parsers when required, not just on init().            loadParsers();        }        if (parser == null) {            if (Elements.HTTP.equals(name) || Elements.FILTER_SECURITY_METADATA_SOURCE.equals(name) ||                    Elements.FILTER_CHAIN_MAP.equals(name) || Elements.FILTER_CHAIN.equals(name)) {                reportMissingWebClasses(name, pc, element);            } else {                reportUnsupportedNodeType(name, pc, element);            }            return null;        }        //正式解析xml节点元素,一般配置http元素,之后会进行过滤器代理的实例化以及过滤器链的初始化注入到过滤器代理中        return parser.parse(element, pc);    }


原创粉丝点击