Usage of springMVC's mvc:interceptors interceptor

1. Configure the interceptor

In the springMVC.xml configuration file add:

    <mvc:interceptors>
   <!-- Log Interceptor -->
   <mvc:interceptor>
    <mvc:mapping path="/**" />
    <mvc:exclude-mapping path="/static/**" />
    <bean class="Interceptor java code path" />
   </mvc:interceptor>
 </mvc:interceptors>

illustrate:

1) mvc:mapping interceptor path configuration

2) mvc:exclude-mapping interceptor does not need to intercept the path

2. Reference code

public class LogsInterceptor extends HandlerInterceptorAdapter {

    private static final Logger logger = LoggerFactory.getLogger(LogsInterceptor.class);
    
    private  NamedThreadLocal<String> logContext = new NamedThreadLocal<String>("log-id");

    @Autowired
    private TLogDao logDao;

    /**
     * The preHandle method is used for processor interception. As the name suggests, this method will be called before Controller processing.
     * The Interceptor interceptors in SpringMVC are chained, and multiple Interceptors can exist at the same time.
     * Then SpringMVC will execute one by one according to the order of declarations,
     * And all the preHandle methods in the Interceptor will be called before the Controller method is called.
     * This Interceptor chain structure of SpringMVC can also be interrupted,
     * This interruption method is to make the return value of preHandle false, and the entire request ends when the return value of preHandle is false.
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String host = request.getRemoteHost();
        String url = request.getRequestURI();
        TLogEntity entity = new TLogEntity();
        entity.setCreateTime(new Timestamp(System.currentTimeMillis()));
        entity.setCreateUser("admin");
        entity.setIpAddress(host);
        entity.setLogUrl(url);
        entity.setIsSuccess("N");
        logDao.save(entity);
        logContext.set(entity.getLogId());

        logger.debug("IP is ---->>> " + host + " <<<-----accessed the system");
        return true;
    }

    /**
     * This method will only be executed when the current Interceptor's preHandle method returns true.
     * postHandle is used for processor interception. Its execution time is after the processor is processed, that is, after the Controller method is called.
     * But it will be executed before the DispatcherServlet renders the view, which means you can operate on ModelAndView in this method.
     * The chain structure of this method is opposite to the direction of normal access, that is to say, the Interceptor interceptor declared first will be called later instead.
     * This is a bit similar to the execution process of the interceptor in Struts2,
     * Just call the invoke method of ActionInvocation manually in the intercept method in Struts2,
     * The invoke method of calling ActionInvocation in Struts2 is to call the next Interceptor or call the action,
     * Then the content to be called before the Interceptor is written before the invoke method is called, and the content to be called after the Interceptor is written after the invoke method is called.
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
    }

    /**
     * This method is also executed only when the return value of the current corresponding Interceptor's preHandle method is true.
     * This method will be executed after the entire request is completed, that is, the DispatcherServlet renders the view. The main function of this method is to clean up resources.
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        String host = request.getRemoteHost();
        String logId = logContext.get();
        TLogEntity entity = logDao.findOne(logId);
        entity.setIsSuccess("Y");
        logDao.save(entity);

        logger.debug("IP is ---->>> " + host + " <<<-----Access successful");
    }

}


Original https://www.cnblogs.com/lcngu/p/7096597.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326242431&siteId=291194637