我试图在我的spring应用程序中添加WebFilter。但是,我没有使用.xml文件(甚至不是web.xml,因为我的应用程序不需要它。)
所以,我在课堂上添加了AbstractAnnotationConfigDispatcherServletInitializer
:
@Override
protected Filter[] getServletFilters() {
return new Filter[]{new RequestFilter()};
}
而且,我的RequestFilter.java:
@WebFilter("/test/*")
public class RequestFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException { }
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException { }
@Override
public void destroy() { }
我希望只过滤匹配/test/*
模式的请求,但会过滤对任何资源的请求。
如何映射我的过滤器?
感谢。
答案 0 :(得分:1)
@WebFilter
- 不是Spring注释。春天忽略了它。方法getServletFilters
返回一个过滤器数组,而不将它们映射到URL。因此他们在每次请求时触发。如果您不想在web.xml中编写url-mappings,则可以使用HandlerInterceptor而不是Filter
。可以在DispatcherServletInitializer
:
public class SomeInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
// ...
return true;
}
}
@Configuration
@ComponentScan("com.example")
@EnableWebMvc
public class AppConfig extends WebMvcConfigurerAdapter {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry
.addInterceptor(new SomeInterceptor())
.addPathPatterns("/test/*");
}
}
public class WebAppInitializer implements WebApplicationInitializer {
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(AppConfig.class);
ctx.setServletContext(servletContext);
Dynamic dynamic = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
dynamic.addMapping("/");
dynamic.setLoadOnStartup(1);
}
}
或者您可以定义自己的WebFilter注释!
首先,您需要实用程序类来匹配URL模式:
public class GlobMatcher {
public static boolean match(String pattern, String text) {
String rest = null;
int pos = pattern.indexOf('*');
if (pos != -1) {
rest = pattern.substring(pos + 1);
pattern = pattern.substring(0, pos);
}
if (pattern.length() > text.length())
return false;
for (int i = 0; i < pattern.length(); i++)
if (pattern.charAt(i) != '?'
&& !pattern.substring(i, i + 1).equalsIgnoreCase(text.substring(i, i + 1)))
return false;
if (rest == null) {
return pattern.length() == text.length();
} else {
for (int i = pattern.length(); i <= text.length(); i++) {
if (match(rest, text.substring(i)))
return true;
}
return false;
}
}
}
注释本身:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface WebFilter {
String[] urlPatterns();
}
网址格式匹配的传递功能:
@Aspect
public class WebFilterMatcher {
@Pointcut("within(@com.example.WebFilter *)")
public void beanAnnotatedWithWebFilter() {}
@Pointcut("execution(boolean com.example..preHandle(..))")
public void preHandleMethod() {}
@Pointcut("preHandleMethod() && beanAnnotatedWithWebFilter()")
public void preHandleMethodInsideAClassMarkedWithWebFilter() {}
@Around("preHandleMethodInsideAClassMarkedWithWebFilter()")
public Object beforeFilter(ProceedingJoinPoint joinPoint) throws Throwable {
Object[] args = joinPoint.getArgs();
if(args.length > 0) {
HttpServletRequest request = (HttpServletRequest) args[0];
Class target = joinPoint.getTarget().getClass();
if (target.isAnnotationPresent(WebFilter.class)) {
String[] patterns = ((WebFilter) target.getAnnotation(WebFilter.class)).urlPatterns();
for (String pattern : patterns) {
if (GlobMatcher.match(pattern, request.getRequestURI())) {
return joinPoint.proceed();
}
}
}
}
return true;
}
}
拦截器:
@WebFilter(urlPatterns = {"/test/*"})
public class SomeInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
// ...
return true;
}
}
上下文配置稍有变化:
<beans> <!-- Namespaces are omitted for brevity -->
<aop:aspectj-autoproxy />
<bean id="webFilterMatcher" class="com.example.WebFilterMatcher" />
<mvc:interceptors>
<bean class="com.example.SomeInterceptor" />
</mvc:interceptors>
</beans>
答案 1 :(得分:1)
如果使用Spring Boot,可以将@Component注释添加到过滤器实现中,或者将@ServletComponentScan添加到主类中。
答案 2 :(得分:0)
另一个选择是使用FilterRegistrationBean注册自定义过滤器类,而不将过滤器本身添加为Bean。例如:
@Bean
public FilterRegistrationBean<RequestResponseLoggingFilter> loggingFilter(){
FilterRegistrationBean<RequestResponseLoggingFilter> registrationBean
= new FilterRegistrationBean<>();
registrationBean.setFilter(new RequestResponseLoggingFilter());
registrationBean.addUrlPatterns("/users/*");
return registrationBean;
}