SpringBoot——SpringBoot使用过滤器Filter

1 SpringBoot使用过滤器Filter

SpringBoot中使用过滤器Filter有两种方式

方式一: 通过注解方式实现

方式二:通过 Spring Boot 的配置类实现

2  方式一: 通过注解方式实现

首先先写一个过滤器Filter,在类的上方使用 @WebFilter 注解来创建Filter即可

package com.liuhaiyang.springboot.filter;


import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

//过滤器
@WebFilter(urlPatterns = "/myfilter")
public class MyFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("---------您已进入过滤器----------");
        filterChain.doFilter(servletRequest,servletResponse);
    }
}

在SpringBoot项目的入口类上方使用注解 @ServletComponentScan 扫描filter包中的注解

package com.liuhaiyang.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;

@SpringBootApplication
@ServletComponentScan(basePackages = "com.liuhaiyang.springboot.filter")  //第一种方式
public class SpringbootTest14Application {

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

}

启动测试

SpringBoot——SpringBoot使用过滤器Filter_第1张图片

SpringBoot——SpringBoot使用过滤器Filter_第2张图片

 2 方式二:通过 Spring Boot 的配置类实现

首先先写一个过滤器Filter,不使用注解

package com.liuhaiyang.springboot.filter2;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

//过滤器
public class MyFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("---------您已进入过滤器222----------");
        filterChain.doFilter(servletRequest,servletResponse);
    }
}

再写一个配置类

package com.liuhaiyang.springboot.config;

import com.liuhaiyang.springboot.filter2.MyFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration  //定义此类为配置类
public class FilterConfig {
    @Bean
    public FilterRegistrationBean myFilterRegistrationBean(){
        FilterRegistrationBean filterRegistrationBean=new FilterRegistrationBean(new MyFilter());

        //添加过滤路径
        filterRegistrationBean.addUrlPatterns("/user/*");
        return filterRegistrationBean;
    }

}

最后写一个controller类

package com.liuhaiyang.springboot.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class MyController {
    @RequestMapping("/user/detail")
    public @ResponseBody String userDetail(){
        return "/user/detail";
    }

    @RequestMapping("/center")
    public @ResponseBody String center(){
        return "/center";
    }
}

启动测试

SpringBoot——SpringBoot使用过滤器Filter_第3张图片

SpringBoot——SpringBoot使用过滤器Filter_第4张图片

 SpringBoot——SpringBoot使用过滤器Filter_第5张图片

 

你可能感兴趣的:(Spring,Boot,spring,boot,java,spring)