SpringBoot集成之过滤器

在开发过程中,我们通常需要过滤一些请求,如访问某个接口判断登陆是否验证等。springboot下过滤器有两种实现方式

    1.注解方式

使用该过滤器的时候需添加@WebFilter注解,另外还需要@Component注解,将该类作为组件,注入spring容器中。

package com.example.demo.filter;


import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;


/**
 * Created by linjiaming
 */
@Slf4j
@Component
@WebFilter(value = "/hello")
public class HelloFilter2 implements Filter {


    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
            FilterChain filterChain) throws IOException, ServletException {
        log.info("进入到过滤器2啦");
        filterChain.doFilter(servletRequest,servletResponse);
    }

    @Override
    public void destroy() {

    }
}

值得一提的是,该过滤器的日志打印方式是lombok插件自带的注解@Slf4j。该插件的功能很强大,可自动为实体类生成get,set方法等。使用时需先下载对应插件,以及在pom文件里引入对应的依赖

     2.bean注入方式

首先自定义filter,然后在springboot启动类下配置一个过滤的bean,若不在springboot的启动类下配置,在其他的类上使用时,需在类上写上@Configuration,标志这个类是一个配置类。springboot启动类注解@springbootApplication内部含有该注解,所以无需配置。

package com.example.demo.filter;


import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import lombok.extern.slf4j.Slf4j;


/**
 * Created by linjiaming
 */
@Slf4j
public class HelloFilter1 implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
            FilterChain filterChain) throws IOException, ServletException {
        log.info("进入到过滤器1啦");
        //放行至下一个过滤器
        filterChain.doFilter(servletRequest,servletResponse);
    }

    @Override
    public void destroy() {

    }
}
package com.example.demo;

import com.example.demo.filter.HelloFilter1;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class DemoApplication {

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

    @Bean
    public FilterRegistrationBean registrationBean(){
        FilterRegistrationBean filter = new FilterRegistrationBean(new HelloFilter1());
        filter.addUrlPatterns("/hello");
        //多个过滤器时执行顺序
        //filter.setOrder(1);
        return filter;
    }
}

 

你可能感兴趣的:(springboot整合系列)