我创建了一个
spring boot过滤器 – 使用@Component注释实现GenericFilterBean.
@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初始化程序 – 只是注释.
让它运作的正确方法是什么?
解决方法
您可以添加如下过滤器:
@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(); }