springboot filter注册(@WebFilter)
*************************
相关注解
@WebFilter
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface WebFilter {
String filterName() default "";
String[] urlPatterns() default {};
String description() default "";
String displayName() default "";
WebInitParam[] initParams() default {};
String smallIcon() default "";
String largeIcon() default "";
String[] servletNames() default {};
String[] value() default {};
DispatcherType[] dispatcherTypes() default {DispatcherType.REQUEST};
boolean asyncSupported() default false;
}
*************************
示例
*****************
filter 层
MyFilter:自定义filter
public class MyFilter implements Filter {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
servletResponse.getOutputStream().println("hello world");
}
}
*****************
config 层
WebConfig:扫描创建filter bean
@Configuration
@ServletComponentScan(basePackages = {"com.example.demo.filter"})
public class WebConfig {
}
*************************
测试输出
localhost:8080/hello2