• SpringBoot如何自定义拦截器呢?


    转自:

    SpringBoot如何自定义拦截器呢?

    下文笔者讲述springboot自定义拦截器的方法分享,如下所示

    HandlerInterceptor的功能

    在request被响应之前(请求到达Controller之前)
    在request被响应后视图渲染之前
    整个request全部结束之后(一般用于请求结束释放资源)
    
    注意事项:
       不能使用拦截器修改request里的内容
        但可以抛异常或return false来结束Request请求的后续执行
    

    实现自定义拦截器方法

    自定义拦截器类:实现HandlerInterceptor接口

    package com.java265.myInterceptor;
    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;
    
    @Component
    public class MyInterceptor implements HandlerInterceptor {
    
        //请求处理之前Controller之前调用
        @Override
        public boolean preHandle(HttpServletRequest httpServletRequest,
    	   HttpServletResponse httpServletResponse, Object o) throws Exception {
    
            System.out.println("MyInterceptor the requestHandler  before");
    
            //必须return:true否则请求将不会向下执行
            return true;
        }
    
        //请求处理之后但视图渲染之前调用
        @Override
        public void postHandle(HttpServletRequest httpServletRequest, 
    	   HttpServletResponse httpServletResponse, 
    	   Object o, ModelAndView modelAndView) throws Exception {
            System.out.println("MyInterceptor the requestHandler  after");
        }
    
        //整个请求结束,视图完成渲染之后调用,一般用于释放资源
        @Override
        public void afterCompletion(HttpServletRequest httpServletRequest, 
    	HttpServletResponse httpServletResponse, 
    	Object o, Exception e) throws Exception {
            System.out.println("MyInterceptor the view after");
        }
    }
     
    配置自定义拦截器类:创建一个Java类继承WebMvcConfigurerAdapter,并重写 addInterceptors 方法
    将自定义拦截器放入拦截器链中:addInterceptors 方法中添加自己的拦截器类,并指定拦截的URL;
    package com.java265.myInterceptor;
    
    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 MyInterceptorConfig extends WebMvcConfigurerAdapter {
    
        @Autowired
        private MyInterceptor myInterceptor;
    
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            //将自定义拦截器加入拦截器链中,并指定拦截的URl
            registry.addInterceptor(myInterceptor).addPathPatterns("/swagger/*");
        }
    }
  • 相关阅读:
    一文带你秒懂 字节序(byte order),比特序(bit order),位域(bit field)
    python基本操作的作业
    系统编程07-线程的互斥锁、读写锁、条件变量
    Ubuntu18.04安装docker-io
    Kafka(四)消费者消费消息
    网络安全(黑客)自学
    Selenium入门(一)Java 搭建 Selenium 环境
    SpringBoot整合Swagger2+Knife4j,注解使用
    swift 【block】
    源码解析HDFS文件上传之write上传过程-尚硅谷Java培训
  • 原文地址:https://blog.csdn.net/qq_25073223/article/details/127993594