zoukankan      html  css  js  c++  java
  • springboot拦截器

    先创建一个Interceptor类实现HandlerInterceptor接口

    package ax.tst.interceptor;
    
    import ax.f4j.controller.BaseController;
    import ax.tst.common.AuthTools;
    import ax.tst.model.RequestLog;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.mongodb.core.MongoOperations;
    import org.springframework.stereotype.Component;
    import org.springframework.web.servlet.HandlerInterceptor;
    import org.springframework.web.servlet.ModelAndView;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.util.Date;
    
    /**
     * 请求拦截器
     *
     * @author QQQ
     * @since 2018年5月15日14:51:06
     */
    @Component
    public class RequestInterecptor extends BaseController implements HandlerInterceptor {
    
        @Autowired
        private MongoOperations mongoOperations;
    
        @Autowired
        private AuthTools authTools;
    
        @Override
        public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
            // 记录接口请求日志
            RequestLog requestLog = new RequestLog(authTools.getUserInfo().getId(), getRemoteAddress(), httpServletRequest.getServletPath(), new Date());
            mongoOperations.save(requestLog);
            return true;
        }
    
        @Override
        public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
        }
    
        @Override
        public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
        }
    }

    注册

    package ax.tst;
    
    import ax.tst.interceptor.RequestInterecptor;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
    
    @Configuration
    public class TstApiConfig extends WebMvcConfigurerAdapter {
    
        @Autowired
        private RequestInterecptor requestInterecptor;
    
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            registry.addInterceptor(requestInterecptor).addPathPatterns("/**");
            super.addInterceptors(registry);
        }
    }
  • 相关阅读:
    MyEclipse 自带的TomCat 新增部署的时候不显示 Deploy Location
    No prohects are avaliable for deployment to this server
    Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
    Dom4j 对XMl解析 新手学习,欢迎高手指正
    电脑的技巧
    Browserify的基本使用
    bower的基本使用
    前端工程化--前端工程化技术栈
    前端工程化--架构说明
    前端工程化-前端工程化说明
  • 原文地址:https://www.cnblogs.com/dsh2018/p/11263509.html
Copyright © 2011-2022 走看看