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

问题描述 投票:0回答:1

我想在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;
}
java spring
1个回答
0
投票

在@TongChen的评论中添加更多详细信息

您尝试过UserFilterBean.addUrlPatterns("/\\blogin\\b/cart/*");吗?

spring使用AntPathMatcher,请看一下完整的文档-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模板模式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/*");为您提供解决方案

© www.soinside.com 2019 - 2024. All rights reserved.