以前编写配置 Filter
<filter> <filter-name>FirstFilter</filter-name> <filter-class>com.neuedu.filter.FirstFilter</filter-class> </filter> <filter-mapping> <filter-name>FirstFilter</filter-name> <url-pattern>/first</url-pattern> </filter-mapping>
SpringBoot 整合Filter方式一
通过注解扫描完成Filter组件的注册
- 编写Filter
package com.neuedu.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; /** * SpringBoot 整合Filter 方式一 * @author 清水三千尺 * */ //@WebFilter(filterName="",urlPatterns= {"*.do","*.jsp"}) 这是拦截多个URL的写法 @WebFilter(filterName="",urlPatterns="/first") public class FirstFilter implements Filter { @Override public void destroy() { // TODO Auto-generated method stub } @Override public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException { System.out.println("进入Filter。。。。"); //放行操作 arg2.doFilter(arg0, arg1); System.out.println("离开Filter。。。。"); } @Override public void init(FilterConfig filterConfig) throws ServletException { // TODO Auto-generated method stub } }
- 编写启动类
package com.neuedu; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.ServletComponentScan; /** * SpringBoot 启动类 * @author 清水三千尺 * */ @SpringBootApplication @ServletComponentScan //在SpringBoot启动时会扫描@WebFileter,并将该类实例化 public class App { public static void main(String[] args) throws Exception { SpringApplication.run(App.class, args); } }
- 启动测试
浏览器访问:http://localhost:8080/frist
控制台结果:
SpringBoot 整合Filter方式二
通过方法完成Filter组件的注册
- 编写Filter
package com.neuedu.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; /** * SpringBoot 整合Filter 方式二 * @author 清水三千尺 * */ public class SecondFilter implements Filter { @Override public void destroy() { // TODO Auto-generated method stub } @Override public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException { System.out.println("进入SecondFilter。。。。"); //放行操作 arg2.doFilter(arg0, arg1); System.out.println("离开SecondFilter。。。。"); } @Override public void init(FilterConfig filterConfig) throws ServletException { // TODO Auto-generated method stub } }
- 编写启动类
package com.neuedu; 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 com.neuedu.filter.SecondFilter; import com.neuedu.servlet.SecondServlet; /** * SpringBoot 启动类 * @author 清水三千尺 * */ @SpringBootApplication public class App2 { public static void main(String[] args) throws Exception { SpringApplication.run(App2.class, args); } /** * 注册Servlet */ @Bean public ServletRegistrationBean<SecondServlet> getServletRegistrationBean() { //完成SecondServlet的注册 ServletRegistrationBean<SecondServlet> bean = new ServletRegistrationBean<SecondServlet>(new SecondServlet()); //完成url的指定 bean.addUrlMappings("/second"); return bean; } /** * 注册Filter */ @Bean public FilterRegistrationBean<SecondFilter> getFilterRegistrationBean() { //完成SecondFilter的注册 FilterRegistrationBean<SecondFilter> bean = new FilterRegistrationBean<SecondFilter>(new SecondFilter()); //完成url的指定(需要拦截的URL) //bean.addUrlPatterns(new String[] {"*.do","*.jsp"}); 指定多个URL bean.addUrlPatterns("/second"); return bean; } }
- 启动测试
浏览器访问:http://localhost:8080/second
控制台结果: