首页 > 解决方案 > antMatcher 和 mvcMatcher 的区别

问题描述

和函数有什么区别HttpSecurityantMatcher()mvcMatcher()

谁能解释何时使用它们?

标签: springspring-security

解决方案


正如官方文档中明确说明的那样,这种方法的签名也有说明-

antMatcher(String antPattern)- 允许将 配置HttpSecurity为仅在匹配提供的 ant 模式时调用。

mvcMatcher(String mvcPattern)- 允许配置HttpSecurity仅在匹配提供的 Spring MVC 模式时调用。

通常mvcMatcherantMatcher. 举个例子:

  • antMatchers("/secured")仅匹配确切的 /securedURL
  • mvcMatchers("/secured")匹配/secured以及/secured/, /secured.html,/secured.xyz

因此更通用,也可以处理一些可能的配置错误。

mvcMatcher使用 Spring MVC 用于匹配的相同规则(使用@RequestMapping注解时)。

如果 Spring MVC 不会处理当前请求,则将使用使用该模式作为 ant 模式的合理默认值。资源

可以补充一点,mvcMatchersAPI(自 4.1.1 起)比API(自 3.1 起)更新。antMatchers


推荐阅读