在 Spring Boot 中使用 Filter 与前面的使用 Servlet 相似,根据 Filter 注册方式的不同,有两种使用方式。若使用的是 Servlet3.0+版本,则两种方式均可使用;若使用的是 Servlet2.5版本,则只能使用配置类方式。
1.注解方式
若使用的是 Servlet3.0+版本,可以直接使用 Filter 的注解对 Filter 进行注册。其总步骤有
两步:
- 在定义好的 Filter 上使用@WebFilter 注解
- 在入口类上添加@ServletComponentScan 注解 --->需要写扫描的包
1.1创建springboot
1.2创建filter
自定义filter实现Filter接口
package com.example.demo.filter;
import lombok.extern.slf4j.Slf4j;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @author Created by niugang on 2019/7/20/15:46
*/
@WebFilter("/*")
@Slf4j
public class AccesLogFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
log.info("AccesLogFilter init");
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
String requesturi = request.getRequestURI();
log.info("Request URI:{}",requesturi);
filterChain.doFilter(request,response);
}
@Override
public void destroy() {
log.info("AccesLogFilter destroy");
}
}
1.3修改springboot启动入口
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
@SpringBootApplication
@ServletComponentScan(basePackages="com.example.demo.filter")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
2.配置方式
若使用的是 Servlet2.5 版本,没有 Filter 注解,此时只能使用配置类方式。其总步骤有
两步,与@ServletComponentScan 注解无关。
- 定义 Filter
- 定义配置类
2.1创建工程
2.2创建filter
package com.example.demo.filter;
import lombok.extern.slf4j.Slf4j;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @author Created by niugang on 2019/7/20/15:46
*/
@Slf4j
public class AccesLogFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
log.info("AccesLogFilter init");
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
String requesturi = request.getRequestURI();
log.info("Request URI:{}",requesturi);
filterChain.doFilter(request,response);
}
@Override
public void destroy() {
log.info("AccesLogFilter destroy");
}
}
2.3修改配置类
package com.example.demo;
import com.example.demo.filter.AccesLogFilter;
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<AccesLogFilter> registration() {
//创建filter
AccesLogFilter accesLogFilter = new AccesLogFilter();
//注册过滤器
FilterRegistrationBean<AccesLogFilter> registration = new FilterRegistrationBean<>(accesLogFilter);
//添加条件
registration.addUrlPatterns("/*");
return registration;
}
}
微信公众号