Shiro learns the filter class provided by 18-shiro - AdviceFilter

This class is the same level as the previous AbstractShiroFilter, which is a subclass of OnecPerRequestFilter. We can think of his intention from his name Advice-AOP. There are many advices in spring, that is, notifications, such as the former Pre- notification, post-notification, final notification, etc. This class allows to implement the characteristics of aop in filter through many methods , such as preHandle (pre-notification) , postHandle (post-notification, but may not be executed in the case of throwing an exception, ) , afterCompletion (final notification, will be executed).

 

First look at his doFilterInternal method:

public void doFilterInternal(ServletRequest request, ServletResponse response, FilterChain chain)
            throws ServletException, IOException {
        Exception exception = null;
        try {
            boolean continueChain = preHandle(request, response);//Call preHandle first to determine whether it can be executed
            if (log.isTraceEnabled()) {
                log.trace("Invoked preHandle method.  Continuing chain?: [" + continueChain + "]");
            }
            if (continueChain) {//If preHandle returns true, continue to execute the following filter and servlet.
                executeChain(request, response, chain);
            }
            postHandle(request, response);//Execute the operation after completion.
            if (log.isTraceEnabled()) {
                log.trace("Successfully invoked postHandle method");
            }
        } catch (Exception e) {
            exception = e;
        } finally {
            cleanup(request, response, exception);
        }
}

 

The method of ExecuteChain is particularly simple, that is to execute the remaining chain . Because we may configure multiple filters when configuring a path, it does not mean configuring a filter .

 

protected void executeChain(ServletRequest request, ServletResponse response, FilterChain chain) throws Exception {
        chain.doFilter(request, response);
}

 

 

If preHandle returns false , it means that the direct call to postHandle is no longer executed , and the remaining filter servlets are no longer called, so nothing is returned to the page, and the page displays a blank. There is no return in postHandle , indicating a pure operation.

 

Finally , the cleanup method is called in finally , and the source code is as follows:

protected void cleanup(ServletRequest request, ServletResponse response, Exception existing)
            throws ServletException, IOException {
        Exception exception = existing;
        try {
            afterCompletion(request, response, exception);//This method is actually called,
            if (log.isTraceEnabled()) {
                log.trace("Successfully invoked afterCompletion method.");
            }
        } catch (Exception e) {
            if (exception == null) {
                exception = e;
            } else {
                log.debug("afterCompletion implementation threw an exception.  This will be ignored to " +
                        "allow the original source exception to be propagated.", e);
            }
        }
        if (exception != null) {
            if (exception instanceof ServletException) {
                throw (ServletException) exception;
            } else if (exception instanceof IOException) {
                throw (IOException) exception;
            } else {
                if (log.isDebugEnabled()) {
                    String msg = "Filter execution resulted in an unexpected Exception " +
                            "(not IOException or ServletException as the Filter API recommends).  " +
                            "Wrapping in ServletException and propagating.";
                    log.debug(msg);
                }
                throw new ServletException(exception);
            }
        }
}

 

In this method, the afterCompetion is called , so it is equivalent to finally calling the aftercompletion method, so if we have to perform some operations, we must do it in the overridden afterCompetion method, because then it will be executed.

 

We are looking at preHandle postHandle afterCompletion in this class

protected boolean preHandle(ServletRequest request, ServletResponse response) throws Exception {
        return true;
}
@SuppressWarnings({"UnusedDeclaration"})
protected void postHandle(ServletRequest request, ServletResponse response) throws Exception {}
@SuppressWarnings({"UnusedDeclaration"})
public void afterCompletion(ServletRequest request, ServletResponse response, Exception exception) throws Exception {
}

 

Although these three methods are not abstract methods, they have almost no operations, and the above javadoc also shows that these methods need to be overwritten according to specific business logic. So we can perform aop -like operations based on business logic . Of course, if our business logic does not need to use these, we can keep the default.

 

Advice has two subclasses, one is LogoutFilter and the other is PathMatchingFilter .

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327085905&siteId=291194637