簡體   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