Spring boot的拦截器

眉间皱痕 提交于 2020-02-05 04:59:44

定义拦截器和配置拦截器.
实现 WebMvcConfigurer 接口,然后重写 addInterceptors 方法,将自定义的拦截器添加进去.这样不会拦截静态资源

这里用到的maven工程

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.cs</groupId>
    <artifactId>springBoot_filter</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath/>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>  
</project>

配置端口号
application.yml

server:
  port: 8333

编写启动类
SpringBootFilterApplication .java

package com.cs;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
public class SpringBootFilterApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringBootFilterApplication.class,args);
    }
}

编写自定义过滤器

package com.cs.demo.filter;

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 //注入到Spring容器当中
public class MyFilter implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        System.out.println("请求处理之前");

        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

        System.out.println("请求处理后");
    }

    //请求结果结果已经渲染好了,response没有进行返回但也无法修改reponse了(一般用于清理数据)
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

        System.out.println("清理数据");
    }
}

编写配置类 把只定义过滤器加载到spring过滤器当中
MyConfig .java

package com.cs.demo.config;

import com.cs.demo.filter.MyFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration //告知spring这是一个配制类
public class MyConfig implements WebMvcConfigurer {

    @Autowired
    private MyFilter myFilter;
    
    //重写 addInterceptors方法
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
    	//    addPathPatterns("/**")指的是过滤那些请求
        registry.addInterceptor(myFilter).addPathPatterns("/**");  
    }
}

写一个 Restfull风格的Controller
HelloController .java

package com.cs.demo.controller;

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

@RestController
public class HelloController 
    @GetMapping("hello")
    public String hello(){
        return "hello";
    }
}

执行启动类 访问端口号:
在这里插入图片描述
回看控制台
在这里插入图片描述显然 依次执行

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!