• 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);
        }
    }
  • 相关阅读:
    向SDE加入图形的时候出现Update fail,5019
    在两台机器上安装分别安装oracle和arcsde
    Maxthon与IE的Cookie冲突
    数据迁移如何不需要中止业务
    ArcSde注册版本后的表
    小心注释掉的js代码也会引起错误
    vb.net对循环内的变量分配和初始化的处理
    一个webservice的初级例子
    事务处理
    ado.net 相对于ado主要有哪些改进
  • 原文地址:https://www.cnblogs.com/dsh2018/p/11263509.html
Copyright © 2020-2023  润新知