shiro框架源码解析与改造(二)---ShiroFilterFactoryBean

ShiroFilterFactoryBean是shiro框架的核心起始类,是shiro框架一切流程的源头。

上文已经知道,DelegatingFilterProxy会从springmvc容器中查找这个类,并代理执行。
那么ShiroFilterFactoryBean类具体是干什么的呢?

查看源码可以看到ShiroFilterFactoryBean实现了 FactoryBean接口,所以真正注册到springmvc容器中的类并不是ShiroFilterFactoryBean本身,而是另外一个类。

@Override
    public Object getObject() throws Exception {
        if(this.instance == null) {
            this.instance = this.createInstance();
        }
        return this.instance;
    }

这个类实例就是intstance,下面看看createInstance()方法具体干了什么:

protected AbstractShiroFilter createInstance() throws Exception {
        log.debug("Creating Shiro Filter instance.");
        SecurityManager securityManager = this.getSecurityManager();
        String msg;
        if(securityManager == null) {
            msg = "SecurityManager property must be set.";
            throw new BeanInitializationException(msg);
        } else if(!(securityManager instanceof WebSecurityManager)) {
            msg = "The security manager does not implement the WebSecurityManager interface.";
            throw new BeanInitializationException(msg);
        } else {
            FilterChainManager manager = this.createFilterChainManager();
            PathMatchingFilterChainResolver chainResolver = new PathMatchingFilterChainResolver();
            chainResolver.setFilterChainManager(manager);
            return new SpringShiroFilter((WebSecurityManager)securityManager, chainResolver,this.notFilters);
        }
    }

这个方法有点复杂,做的事情比较多。

首先是获取安全管理器SecurityManager ,这是也是需要预先配置的
 SecurityManager securityManager = this.getSecurityManager();
 然后创建FilterChainManager, 
 PathMatchingFilterChainResolver,
 SpringShiroFilter

可以看到最后真正注册到springmvc容器中的bean是springShiroFilter。所以实际实行的filter也是它。
关于FilterChainManager,PathMatchingFilterChainResolver,SpringShiroFilter这三个类将在后文一 一解析。

猜你喜欢

转载自blog.csdn.net/ljz2016/article/details/81213659