SpringBoot - 整合Filter的两种方式

整合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;
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值