I want to add filter for this path with wildcards /{login}/cart/* in webfilter config Spring application, but wildcard for {login} is not working
@Bean
public FilterRegistrationBean<UserFilter> homeFilter() {
FilterRegistrationBean<UserFilter> UserFilterBean = new FilterRegistrationBean<>();
UserFilterBean.setFilter(new UserFilter());
UserFilterBean.addUrlPatterns("/{login}/cart/*");
return UserFilterBean;
}
Adding more details to @TongChen's comment
Did you try UserFilterBean.addUrlPatterns("/\\blogin\\b/cart/*");
?
spring uses AntPathMatcher, please take a look at this complete documentation - https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/util/AntPathMatcher.html
I am copying text from the same official documentation's examples part:
Examples
com/t?st.jsp — matches com/test.jsp but also com/tast.jsp or com/txst.jsp
com/*.jsp — matches all .jsp files in the com directory
com/**/test.jsp — matches all test.jsp files underneath the com path
org/springframework/**/*.jsp — matches all .jsp files underneath the org/springframework path
org/**/servlet/bla.jsp — matches org/springframework/servlet/bla.jsp but also org/springframework/testing/servlet/bla.jsp and org/servlet/bla.jsp
com/{filename:\\w+}.jsp will match com/test.jsp and assign the value test to the filename variable
I have not tried this approach till now, I am going purely based on documentation,
Here is additional documentation that says this approach will work for @RequestMapping for sure, but whether it will be applicable to filter or not is something to be tried - 16.3.2.2 URI Template Patterns with Regular Expressions https://docs.spring.io/spring/docs/3.1.0.RELEASE/spring-framework-reference/html/mvc.html#mvc-ann-requestmapping-uri-templates-regex
Example from documentation to handle:
If you want to handle "/spring-web/spring-web-3.0.5.jar" with a regex then this is the solution:
@RequestMapping("/spring-web/{symbolicName:[a-z-]+}-{version:\d\.\d\.\d}.{extension:\.[a-z]}")
public void handle(@PathVariable String version, @PathVariable String extension) {
// ...
}
}
Based on all these I am hopeful that UserFilterBean.addUrlPatterns("/\\blogin\\b/cart/*");
might be a solution for you
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.