Shiro源码学习之ShiroFilter

来源:互联网 发布:js连接mysql数据库 编辑:程序博客网 时间:2024/06/07 20:12

一、shiroFilter的继承关系


二、接口说明

1、ServletContextSupport :封装了一个ServletContext

2、Filter 接口

public interface Filter {
 
    void init(FilterConfig filterConfig) throws ServletException;
 
    void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException;
 
    void destroy();
}


3、AbstractFilter 

AbstractFilter  extends ServletContextSupport implements Filter 

它实现了init方法,将ServletContext赋值给了ServletContextSupport ,同时提供了onFilterConfigSet方法让子类实现,可以让子类在过滤器初始化的时候做些事情


4、Nameable接口

public interface Nameable {
    void setName(String name);
}


5、NameableFilter

用于给过滤器设置一个名词,默认为配置文件中的名称

6、OncePerRequestFilter:防止重复使用相同名字(这里的名字从NameableFilter中获取)的过滤器,他实现了Filter的 doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)方法,同时提供了doFilterInternal方法让子类继承,以便在过滤请求的时候能够让子类实现自己的操作

主要方法:

public final void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
            throws ServletException, IOException {
        String alreadyFilteredAttributeName = getAlreadyFilteredAttributeName();
        if ( request.getAttribute(alreadyFilteredAttributeName) != null ) {
            log.trace("Filter '{}' already executed.  Proceeding without invoking this filter.", getName());
            filterChain.doFilter(request, response);
        } else //noinspection deprecation
            if (!isEnabled(request, response) ||
                shouldNotFilter(request) ) {
            log.debug("Filter '{}' is not enabled for the current request.  Proceeding without invoking this filter.",
                    getName());
            filterChain.doFilter(request, response);
        } else {
            log.trace("Filter '{}' not yet executed.  Executing now.", getName());
            request.setAttribute(alreadyFilteredAttributeName, Boolean.TRUE);
            try {
                doFilterInternal(request, response, filterChain);
            } finally {
                // Once the request has finished, we're done and we don't
                // need to mark as 'already filtered' any more.
                request.removeAttribute(alreadyFilteredAttributeName);
            }
        }
    }


7、AbstractShiroFilter

该接口封装了WebSecurityManager securityManager与FilterChainResolver filterChainResolver;,实现了onFilterConfigSet方法,调用了init()方法,该方法子类可覆盖,做些事情,然后确保了securityManager的存在。实现了doFilterInternal方法,在访问请求拦截的时候,将原来的FilterChain代理成立了ProxiedFilterChain,然后进行我们配置的过滤器处理

8、ShiroFilter

覆盖了父类AbstractShiroFilter的init方法,将环境变量中的securityManager与filterChainResolver赋值给AbstractShiroFilter中相应的对象















0 0