1.Springboot中实现Filter实现的途径
- 实现javax.servlet.Filter接口,注解方式配置Filter
- 实现javax.servlet.Filter接口, 通过FilterRegistrationBean类控制配置
- 通过org.springframework.boot.context.embedded.ServletContextInitializer接口实现类配置
** 2.注解的方式配置Filter**
创建实现Filter接口的实现类AnnoantationFilter
/** * @author gschao
* @date 2016/11/17 15:19.
* 注释方式新建Filter
*/
@WebFilter(filterName = "annoantationFilter" ,urlPatterns = {"/home"})
public class AnnoantationFilter implements Filter{
private Logger logger = LoggerFactory.getLogger(AnnoantationFilter.class);
/**
* 初始化
* @param filterConfig
* @throws ServletException
*/
@Override
public void init(FilterConfig filterConfig) throws ServletException {
logger.info("####AnnoantationFilter init……"+filterConfig.getFilterName());
}
/**
* @author gschao
* @date 2016-11-17
* 执行Filter
* @param servletRequest
* @param servletResponse
* @param filterChain
* @throws IOException
* @throws ServletException
*/
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
logger.info("####AnnoantationFilter doFilter");
filterChain.doFilter(servletRequest,servletResponse);
}
/**
* @author gschao
* @date 2016-11-17
* 销毁
*/
@Override
public void destroy() {
logger.info("####Annoantation destory-------");
}
}
@WebFilter可以设置的属性
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documentedpublic @interface WebFilter {
String description() default ""; //描述
String displayName() default "";//展示名称
WebInitParam[] initParams() default {};//初始化参数
String filterName() default "";//过滤器名称
String smallIcon() default "";
String largeIcon() default "";
String[] servletNames() default {};
String[] value() default {};
String[] urlPatterns() default {};//设置匹配URL
DispatcherType[] dispatcherTypes() default {DispatcherType.REQUEST};
boolean asyncSupported() default false;
}
3. 通过FilterRegistrationBean类配置Filter
适用创建Filter实现类或者使用第三方实现类
创建LogTestFilter 类
/**
* @author gschao
* @date 2016/11/17 15:15.
* 日志文件测试Filter
*/
public class LogTestFilter implements Filter {
private Logger logger = LoggerFactory.getLogger(LogTestFilter.class);
@Override
public void init(FilterConfig filterConfig) throws ServletException {
logger.info("filter init:"+filterConfig.getFilterName());
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
logger.info("doFilter----");
filterChain.doFilter(servletRequest,servletResponse);//直接转发
}
@Override
public void destroy() {
logger.info("LoggerFilter destory---");
}
}
创建配置类FilterConfiguration
/**
* @author gschao
* @date 2016/11/17 15:09.
* 过滤器配置
*
*/
@Configuration
public class FilterConfiguration {
/**
* @author gschao
* @date 2016-11-17
* 装饰框架SiteMeshFilter配置
* @return
*/
@Bean(name = "configurableSiteMeshFilter")
public FilterRegistrationBean filterRegistrationBean(){
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
ConfigurableSiteMeshFilter configurableSiteMeshFilter = new ConfigurableSiteMeshFilter();// 配置拦截请求
filterRegistrationBean.addUrlPatterns("/*");// 配置Filter
filterRegistrationBean.setFilter(configurableSiteMeshFilter);
return filterRegistrationBean;
}
/**
* 注册Filter
* @return
*/
@Bean
public FilterRegistrationBean logTestFilter(){
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
LogTestFilter logTestFilter = new LogTestFilter();
filterRegistrationBean.setFilter(logTestFilter);
return filterRegistrationBean;
}
4.ServletContextInitializer接口实现类配置
实现ServletContextInitializer接口
实现onStartup(ServletContext var1)方法
ServletContext 源码
public interface ServletContext {
……
javax.servlet.FilterRegistration.Dynamic addFilter(String var1, String var2);
javax.servlet.FilterRegistration.Dynamic addFilter(String var1, Filter var2);
javax.servlet.FilterRegistration.Dynamic addFilter(String var1, Class<? extends Filter> var2);
<T extends Filter> T createFilter(Class<T> var1) throws ServletException;
……
}
可以通过以上方法进行设置。