欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

springboot学习之拦截器配置

程序员文章站 2022-07-09 18:03:00
...
拦截器(Interceptor)实现对每一个请求处理前后进行相关的业务处理,类似Servlet的Filter。
Spring boot中快速实现拦截器的方式:
1、实现HandlerInterceptor接口。
2、继承HandlerInterceptorAdaptor类来实现。
下面具体演示过程,该过程是基于搭建好的maven spring boot环境实现:
方式一:通过实现HandlerInterceptor接口。
a、创建DemoInterceptor,然后implements类HandlerInterceptor
package com.wanghp.demo.configurer;

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

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

/**
 * description:
 * created by wanghaiping
 * created date 2017/12/10
 */
public class DemoInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        System.out.println("----------使用接口实现的方式实现(在请求发生之前执行)---------");

        return true;// 只有返回true才会继续向下执行,返回false取消当前请求
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
        System.out.println("----------使用接口实现的方式实现(在请求发生之后执行)---------");
    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
        System.out.println("----------使用接口实现的方式实现(在请求发生之后执行),但是在DispatcherServlet 渲染了对应的视图之后执行(主要是用于进行资源清理工作)---------");
    }


}
b、创建配置类,并注册拦截器
package com.wanghp.demo.configurer;

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

/**
 * description:
 * created by wanghaiping
 * created date 2017/12/10
 */
@Configuration
public class DemoWebMvcConfigurer extends WebMvcConfigurerAdapter {


     public void addInterceptors(InterceptorRegistry registry){
         // 多个拦截器组成一个拦截器链
         // addPathPatterns 用于添加拦截规则
         // excludePathPatterns 用户排除拦截
         registry.addInterceptor(new DemoInterceptor()).addPathPatterns("/**");
         //registry.addInterceptor(new DemoInterceptor1()).addPathPatterns("/**");
         super.addInterceptors(registry);
     }

}

c、创建测试Controller,并在浏览器进行访问测试。
package com.wanghp.demo.spring.controller;

import com.wanghp.demo.spring.commontest.readconfig.GirlProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * description:
 * created by wanghaiping
 * created date 2017/12/8
 */
@RestController
@RequestMapping("/testController")
public class TestController {
    @GetMapping("/testController")
    public String test(){
        return "test";
    }

}
d、测试结果如下
访问:http://localhost:8080/demo/testController/testController
结果:
springboot学习之拦截器配置

方式二:通过继承HandlerInterceptorAdaptor类来实现。
a、创建DemoInterceptor1,然后继承HandlerInterceptorAdaptor
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

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

/**
 * description: 使用继承的方式实现拦截器,选择性的重写相关的类即可
 * created by wanghaiping
 * created date 2017/12/10
 */
public class DemoInterceptor1 extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        System.out.println("----------使用继承类的方式实现(在请求发生之前执行)---------");
        return true;// 只有返回true才会继续向下执行,返回false取消当前请求
    }




}


b、创建配置类,并注册拦截器
package com.wanghp.demo.configurer;

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

/**
 * description:
 * created by wanghaiping
 * created date 2017/12/10
 */
@Configuration
public class DemoWebMvcConfigurer extends WebMvcConfigurerAdapter {


     public void addInterceptors(InterceptorRegistry registry){
         // 多个拦截器组成一个拦截器链
         // addPathPatterns 用于添加拦截规则
         // excludePathPatterns 用户排除拦截
         //registry.addInterceptor(new DemoInterceptor()).addPathPatterns("/**");
         registry.addInterceptor(new DemoInterceptor1()).addPathPatterns("/**");
         super.addInterceptors(registry);
     }

}

c、创建测试Controller,并在浏览器进行访问测试。
package com.wanghp.demo.spring.controller;

import com.wanghp.demo.spring.commontest.readconfig.GirlProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * description:
 * created by wanghaiping
 * created date 2017/12/8
 */
@RestController
@RequestMapping("/testController")
public class TestController {
    @GetMapping("/testController")
    public String test(){
        return "test";
    }

}
d、测试结果如下
访问:http://localhost:8080/demo/testController/testController
结果:
springboot学习之拦截器配置




springboot学习之拦截器配置