我创建了一个 Spring Boot 过滤器 - 使用 @Component 注释实现 GenericFilterBean.
I have created a spring boot filter - implements GenericFilterBean with @Component annotation.
@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 初始化程序 - 只是注释.
The filter is automatically identified by the Spring Boot Framework and works fine for all of the REST API. I want this filter to apply only on a certain URL path, such as /api/secure/* but I can't find the right way.
I tried @WebFilter but it didn't work.
I'm not using XML configuration or servlet initializer - just the annotations.
什么是让它工作的正确方法?
What would be the correct way to get it working?
你可以像这样添加过滤器:
You can add a filter like this:
@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();
}
这篇关于如何基于 URL 模式应用 Spring Boot 过滤器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
如何检测 32 位 int 上的整数溢出?How can I detect integer overflow on 32 bits int?(如何检测 32 位 int 上的整数溢出?)
return 语句之前的局部变量,这有关系吗?Local variables before return statements, does it matter?(return 语句之前的局部变量,这有关系吗?)
如何将整数转换为整数?How to convert Integer to int?(如何将整数转换为整数?)
如何在给定范围内创建一个随机打乱数字的 intHow do I create an int array with randomly shuffled numbers in a given range(如何在给定范围内创建一个随机打乱数字的 int 数组)
java的行为不一致==Inconsistent behavior on java#39;s ==(java的行为不一致==)
为什么 Java 能够将 0xff000000 存储为 int?Why is Java able to store 0xff000000 as an int?(为什么 Java 能够将 0xff000000 存储为 int?)