我创建了一个Spring Boot过滤器- GenericFilterBean带@Component注释的实现。
GenericFilterBean
@Component
@Component public class MyAuthenticationFilter extends GenericFilterBean { ... @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { ... } }
该过滤器由Spring Boot Framework自动识别,并且对所有REST API都适用。我希望此过滤器仅应用于特定的URL路径,例如,/api/secure/*但找不到正确的方法。我试过了,@WebFilter但是没有用。我没有使用XML配置或servlet初始化程序- 只是注释。
/api/secure/*
@WebFilter
使它正常工作的正确方法是什么?
您可以添加如下过滤器:
@Bean public FilterRegistrationBean someFilterRegistration() { FilterRegistrationBean registration = new FilterRegistrationBean(); registration.setFilter(someFilter()); registration.addUrlPatterns("/url/*"); registration.addInitParameter("paramName", "paramValue"); registration.setName("someFilter"); registration.setOrder(1); return registration; } @Bean(name = "someFilter") public Filter someFilter() { return new SomeFilter(); }