Java – how to apply spring boot filters based on URL patterns?

I created a spring boot filter – implementing genericfilterbean with the @ Component annotation

@Component 
public class MyAuthenticationFilter  extends GenericFilterBean {
...
@Override
public void doFilter(ServletRequest servletRequest,ServletResponse servletResponse,FilterChain filterChain) throws IOException,ServletException {
...
}
}

This filter is automatically recognized by the spring boot framework and is applicable to all rest APIs I want this filter to apply only to a URL path, such as / API / secure / * but I can't find the correct method I tried @ webfilter, but it didn't work I didn't initialize the program using XML configuration or servlet - just comments

What is the right way to make it work?

Solution

You can add the following filters:

@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();
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>