zoukankan      html  css  js  c++  java
  • SpringBoot添加拦截器

    SpringBoot中添加拦截器。

    1.继承HandlerInterceptor接口:

    public interface HandlerInterceptor {
        
        default boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
                throws Exception {
    
            return true;
        }
        
        default void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
                @Nullable ModelAndView modelAndView) throws Exception {
        }
        
        default void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
                @Nullable Exception ex) throws Exception {
        }
    
    }

    实现类代码:

    public class ApiHandlerInterceptor implements HandlerInterceptor {
        private Logger logger = LoggerFactory.getLogger(this.getClass());
        
        @Override
        public void afterCompletion(HttpServletRequest request,
                HttpServletResponse response, Object handler, Exception ex)
                throws Exception {
            HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
        }
        
        @Override
        public void postHandle(HttpServletRequest request,
                HttpServletResponse response, Object handler,
                ModelAndView modelAndView) throws Exception {
            HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
        }
        
        @Override
        public boolean preHandle(HttpServletRequest request,
                HttpServletResponse response, Object handler) throws Exception {
            if(logger.isDebugEnabled()) {
                logger.debug("api用户请求拦截[" + request.getRequestURI() + "]");
            }
            String token = request.getParameter("token");
            if(!A.isLogin(token)) {
                if(logger.isDebugEnabled()) {
                    logger.debug("api用户未登录");
                }
                response.setCharacterEncoding("UTF-8");
                response.setContentType("application/json; charset=utf-8");
                PrintWriter out = null;
                try {
                    out = response.getWriter();
                    Map<String, Object> resultMap = A.NoLogin();
                    out.append(JSONObject.toJSONString(resultMap));
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if(null != out) {
                        out.close();
                    }
                }
                return false;
            }
            return HandlerInterceptor.super.preHandle(request, response, handler);
        }
    
    }

    2.实现WebMvcConfigurer接口:

    @Configuration
    public class GlobalWebMvcConfigurer implements WebMvcConfigurer {
        private Logger logger = LoggerFactory.getLogger(this.getClass());
            
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            /**
             * api 端首页和产品详情不用拦截
             */
            registry.addInterceptor(new ApiHandlerInterceptor())
                            .addPathPatterns("/api/**")
                            .excludePathPatterns("/api/open/**");
            
            }
    }
    addPathPatterns是拦截指定接口,/api/**是拦截所有/api/开头的接口。
    excludePathPatterns是不拦截的接口。


  • 相关阅读:
    chrome浏览器,阻止回调方法中的window.open()方法
    安装1.4.2版本的Django
    noindex与nofllow标签的作用和用法
    mybatis中写sql语句时需要转义的字符
    GridView 导出到Excel
    (转)利用Cache防止同一帐号重复登录 (c#) .
    利用NPOI 导出文件到Excel (导出合同)
    TreeView控件的属性和事件【转】
    C# 数字货币转换大写方法
    Server 2003 IIS 发布网站
  • 原文地址:https://www.cnblogs.com/se7end/p/9598539.html
Copyright © 2011-2022 走看看