ShiroFilter请求处理

OncePerRequestFilter

public final void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws ServletException, IOException {
    String alreadyFilteredAttributeName = this.getAlreadyFilteredAttributeName();
    if(request.getAttribute(alreadyFilteredAttributeName) != null) {
        log.trace("Filter \'{}\' already executed.  Proceeding without invoking this filter.", this.getName());
        filterChain.doFilter(request, response);
    } else if(this.isEnabled(request, response) && !this.shouldNotFilter(request)) {
        log.trace("Filter \'{}\' not yet executed.  Executing now.", this.getName());
        request.setAttribute(alreadyFilteredAttributeName, Boolean.TRUE);

        try {
            this.doFilterInternal(request, response, filterChain);
        } finally {
            request.removeAttribute(alreadyFilteredAttributeName);
        }
    } else {
        log.debug("Filter \'{}\' is not enabled for the current request.  Proceeding without invoking this filter.", this.getName());
        filterChain.doFilter(request, response);
    }

}

AbstractShiroFilter

protected void doFilterInternal(ServletRequest servletRequest, ServletResponse servletResponse, final FilterChain chain) throws ServletException, IOException {
    Throwable t = null;

    try {
        final ServletRequest msg = this.prepareServletRequest(servletRequest, servletResponse, chain);
        final ServletResponse response = this.prepareServletResponse(msg, servletResponse, chain);
        WebSubject subject = this.createSubject(msg, response);
        subject.execute(new Callable() {
            public Object call() throws Exception {
                AbstractShiroFilter.this.updateSessionLastAccessTime(msg, response);
                AbstractShiroFilter.this.executeChain(msg, response, chain);
                return null;
            }
        });
    } catch (ExecutionException var8) {
        t = var8.getCause();
    } catch (Throwable var9) {
        t = var9;
    }

    if(t != null) {
        if(t instanceof ServletException) {
            throw (ServletException)t;
        } else if(t instanceof IOException) {
            throw (IOException)t;
        } else {
            String msg1 = "Filtered request failed.";
            throw new ServletException(msg1, t);
        }
    }
}

protected void executeChain(ServletRequest request, ServletResponse response, FilterChain origChain) throws IOException, ServletException {
    FilterChain chain = this.getExecutionChain(request, response, origChain);
    chain.doFilter(request, response);
}

protected FilterChain getExecutionChain(ServletRequest request, ServletResponse response, FilterChain origChain) {
    FilterChain chain = origChain;
    FilterChainResolver resolver = this.getFilterChainResolver();
    if(resolver == null) {
        log.debug("No FilterChainResolver configured.  Returning original FilterChain.");
        return origChain;
    } else {
        FilterChain resolved = resolver.getChain(request, response, origChain);
        if(resolved != null) {
            log.trace("Resolved a configured FilterChain for the current request.");
            chain = resolved;
        } else {
            log.trace("No FilterChain configured for the current request.  Using the default.");
        }

        return chain;
    }
}

PathMatchingFilterChainResolver

public FilterChain getChain(ServletRequest request, ServletResponse response, FilterChain originalChain) {
    FilterChainManager filterChainManager = this.getFilterChainManager();
    if(!filterChainManager.hasChains()) {
        return null;
    } else {
        String requestURI = this.getPathWithinApplication(request);
        Iterator i$ = filterChainManager.getChainNames().iterator();

        String pathPattern;
        do {
            if(!i$.hasNext()) {
                return null;
            }

            pathPattern = (String)i$.next();
        } while(!this.pathMatches(pathPattern, requestURI));

        if(log.isTraceEnabled()) {
            log.trace("Matched path pattern [" + pathPattern + "] for requestURI [" + requestURI + "].  " + "Utilizing corresponding filter chain...");
        }
        
        return filterChainManager.proxy(originalChain, pathPattern);
    }
}

DefaultFilterChainManager

public FilterChain proxy(FilterChain original, String chainName) {
    /**判断该请求是否有配置的Filter**/
    NamedFilterList configured = this.getChain(chainName);
    if(configured == null) {
        String msg = "There is no configured chain under the name/key [" + chainName + "].";
        throw new IllegalArgumentException(msg);
    } else {
        return configured.proxy(original);
    }
}

SimpleNamedFilterList

public FilterChain proxy(FilterChain orig) {
    return new ProxiedFilterChain(orig, this);
}

ProxiedFilterChain

public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
    if(this.filters != null && this.filters.size() != this.index) {
        if(log.isTraceEnabled()) {
            log.trace("Invoking wrapped filter at index [" + this.index + "]");
        }

        ((Filter)this.filters.get(this.index++)).doFilter(request, response, this);
    } else {
        if(log.isTraceEnabled()) {
            log.trace("Invoking original filter chain.");
        }

        this.orig.doFilter(request, response);
    }

}

AdviceFilter

public void doFilterInternal(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
    Exception exception = null;

    try {
        boolean e = this.preHandle(request, response);
        if(log.isTraceEnabled()) {
            log.trace("Invoked preHandle method.  Continuing chain?: [" + e + "]");
        }

        if(e) {
            this.executeChain(request, response, chain);
        }

        this.postHandle(request, response);
        if(log.isTraceEnabled()) {
            log.trace("Successfully invoked postHandle method");
        }
    } catch (Exception var9) {
        exception = var9;
    } finally {
        this.cleanup(request, response, exception);
    }

}

AccessControlFilter

public boolean onPreHandle(ServletRequest request, ServletResponse response, Object mappedValue) throws Exception {
    return this.isAccessAllowed(request, response, mappedValue) || this.onAccessDenied(request, response, mappedValue);
}



你可能感兴趣的:(ShiroFilter请求处理)