整合Filter其实和整合Servlet是一样的,都是这两种方式
前面的构建过程就不截图了,可以参考:https://blog.csdn.net/qq_38238041/article/details/84678619
1.通过注解
编写一个Servlet作为入口
@WebServlet(name="firstServlet",value="/first")
public class FirstServlet extends HttpServlet{
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("doget");
}
}
编写Filter类
package cn.bl.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;
//@WebFilter(filterName="filterTest",urlPatterns= {"",""})
@WebFilter(filterName="filterTest1",urlPatterns="/*")
public class FilterTest implements Filter{
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
System.out.println("来了1");
chain.doFilter(request, response);
System.out.println("走了1");
}
@Override
public void destroy() {
}
}
最后是启动类
package cn.bl;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
@SpringBootApplication
@ServletComponentScan
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
同样,也是执行这个main,然后浏览器访问:http://localhost:8080/first
2.通过方法完成Filter组件的注册
Servlet还用原来的
Filter如下
package cn.bl.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;
public class FilterTest2 implements Filter{
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
System.out.println("来了2");
chain.doFilter(request, response);
System.out.println("走了2");
}
@Override
public void destroy() {
}
}
启动类
package cn.bl;
import javax.servlet.http.HttpServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import cn.bl.filter.FilterTest2;
import cn.bl.servlet.FirstServlet;
@SpringBootApplication
public class App2 {
public static void main(String[] args) {
SpringApplication.run(App2.class, args);
}
@Bean
public FilterRegistrationBean<FilterTest2> getFilterRegistrationBean(){
FilterRegistrationBean<FilterTest2> bean = new FilterRegistrationBean<>(new FilterTest2());
bean.addUrlPatterns("/*");
return bean;
}
@Bean
public ServletRegistrationBean<HttpServlet>getServletRegistrationBean(){
ServletRegistrationBean<HttpServlet>bean = new ServletRegistrationBean<>(new FirstServlet());
bean.addUrlMappings("/second");
return bean;
}
}