整合Filter 方式一
3.1通过注解扫描完成Filter 组件注册
3.1.1创建Filter
/**
* 整合Filter 方式一
*/
//@WebFilter(filterName = "FirstFilter",urlPatterns =
{"*.do","*.jsp"})
@WebFilter(filterName = "FirstFilter",urlPatterns =
"/first")
public class FirstFilter implements Filter{
@Override
public void init(FilterConfig filterConfig) throws
ServletException {
} @Override
public void doFilter(ServletRequest servletRequest,
ServletResponse servletResponse, FilterChain filterChain)
throws IOException, ServletException {
System.out.println("进入First Filter");
filterChain.doFilter(servletRequest,servletResponse);
System.out.println("离开First Filter");
} @
Override
public void destroy() {
}
}
3.1.2修改启动类
@SpringBootApplication
@ServletComponentScan//在spring Boot 启动时会扫描
@WebServlet,@WebFilter 注解,并将该类实例化
public class SpringbootwebApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootwebApplication.class,
args);
}
}
* 整合Filter 方式二
*/
public class SecondFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws
ServletException {
} @
Override
public void doFilter(ServletRequest servletRequest,
ServletResponse servletResponse, FilterChain filterChain)
throws IOException, ServletException {
System.out.println("进入Second Filter");
filterChain.doFilter(servletRequest,servletResponse);
System.out.println("离开Second Filter");
}
@Override
public void destroy() {
}
}
4.1.2创建Filter 配置类
/**
* Filter 配置类
*/
@Configuration
public class FilterConfig {
@Bean
public FilterRegistrationBean
getFilterRegistrationBean(){
FilterRegistrationBean bean = new
FilterRegistrationBean(new SecondFilter());
// bean.addUrlPatterns(new String[]{"*.do","*.jsp"});
bean.addUrlPatterns("/second");
return bean;
}
}