springboot使用拦截器

转载自: https://blog.csdn.net/qq_27905183/article/details/79079762

之前的系列篇中介绍了在SpringBoot中使用过滤器,其实拦截器和过滤器从功能上来说,拦截器/过滤器所能实现的功能过滤器/拦截器都能够实现。但是过滤器是java实现的,它需要依赖于Servlet容器,而拦截器是SprignMVC实现的一个机制,独立于Servlet容器,而且能实现IOC容器中的各个bean。简单的说:过滤器的urlPattern针对的是所有的请求,而拦截器的urlPattern针对的SpringMVC中的Controller控制器处理的请求,并不会拦截Servlet容器


使用Interceptor实例
使用Interceptor很简单,分为三个步骤:
1.创建自定义的拦截器并实现HandlerInterceptor接口;
package org.springframework.demo.section;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 一个简单的Interceptor拦截器类
 * @author chengxi
 */
public class MyInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {

        //只有返回true才会继续向下执行,返回false取消当前请求
        System.out.println("myinterc prehandler");
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

        System.out.println("myinterc posthandler");
    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

        System.out.println("myinterc aftercompletion");
    }
}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
2.创建一个java类继承WebMvcConfiguraeAdapter并重写addInterceptor方法,该类用来添加配置拦截器:
package org.springframework.demo.section;

import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * 继承WebMvcConfigureAdapter继承并重写addInterceptor方法用于添加配置拦截器
 * @author chengxi
 */
public class MyAdapter extends WebMvcConfigurerAdapter {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        //添加拦截器
        registry.addInterceptor(new MyInterceptor())
                .addPathPatterns("/**");
        super.addInterceptors(registry);
    }
}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
3.然后我们需要编写一个Controller控制类用于处理url请求,因为拦截器只能拦截SpringMVC的Controller控制器处理的请求:
package org.springframework.demo.section;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 一个简单的控制器测试类,主要用于测试Interceptor拦截器的使用
 * @author chengxi
 */
@RestController
@RequestMapping("/inter")
public class InterController {

    @RequestMapping("/test")
    public void test(HttpServletResponse response) throws IOException {
        response.getWriter().print("<h1>Hello interceptor test</h1>");
    }
}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
最后创建一个启动类,直接运行即可,然后打开浏览器:localhost:8080/inter/test将会看到页面输出Hello interceptor test,即表示拦截器使用成功




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值