简体   繁体   English

我可以在 spring webfilter 中使用通配符作为路径变量吗?

[英]Can i use wildcard for path variable in spring webfilter?

I want to add filter for this path with wildcards /{login}/cart/* in webfilter config Spring application, but wildcard for {login} is not working我想在 webfilter config Spring 应用程序中使用通配符 /{login}/cart/* 为这个路径添加过滤器,但是 {login} 的通配符不起作用

@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在@TongChen 的评论中添加更多细节

Did you try UserFilterBean.addUrlPatterns("/\\blogin\\b/cart/*");你试过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 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这是附加文档,说明这种方法肯定适用于@RequestMapping,但它是否适用于过滤器需要尝试 - 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:如果您想使用正则表达式处理“/spring-web/spring-web-3.0.5.jar”,那么这就是解决方案:

@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/*");基于所有这些,我希望UserFilterBean.addUrlPatterns("/\\blogin\\b/cart/*"); might be a solution for you可能是你的解决方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM