gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 16:49:32 24 4
gpt4 key购买 nike

我想在 webfilter 配置 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使用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/*"); 可能是您的解决方案

关于java - 我可以在 spring webfilter 中使用通配符作为路径变量吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61720663/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com