繁体   English   中英

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

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

我想在 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;
}

在@TongChen 的评论中添加更多细节

你试过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

我正在从同一个官方文档的示例部分复制文本:

例子

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

直到现在我还没有尝试过这种方法,我纯粹基于文档,

这是附加文档,说明这种方法肯定适用于@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

要处理的文档示例:

如果您想使用正则表达式处理“/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) {    
    // ...
  }
}

基于所有这些,我希望UserFilterBean.addUrlPatterns("/\\blogin\\b/cart/*"); 可能是你的解决方案

暂无
暂无

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

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