Jwo*_*Ker 9 spring spring-security
有什么区别HttpSecurity和antMatcher()功能?
任何人都可以解释何时使用它们?
Dim*_*San 41
正如官方文档中明确说明的那样,该方法的签名也有说明-
antMatcher(String antPattern)- 允许配置HttpSecurity仅在匹配提供的蚂蚁模式时调用。
mvcMatcher(String mvcPattern)- 允许配置HttpSecurity仅在匹配提供的 Spring MVC 模式时调用。
通常mvcMatcher比antMatcher. 举个例子:
antMatchers("/secured")只匹配确切的 /securedURLmvcMatchers("/secured")匹配/secured以及/secured/, /secured.html,/secured.xyz因此更通用,也可以处理一些可能的配置错误。
mvcMatcher使用 Spring MVC 用于匹配的相同规则(使用@RequestMapping注解时)。
如果当前请求不会被 Spring MVC 处理,则将使用将该模式用作蚂蚁模式的合理默认值。来源
它可以添加mvcMatchersAPI(因为4.1.1)是较新的比antMatchersAPI(自3.1)。
AntMatcher()是 Ant 风格路径模式的实现。这个映射代码的一部分是从 Apache Ant 借来的。
MvcMatcher()使用 Spring MVCHandlerMappingIntrospector来匹配路径并提取变量。
所以它们都实现了RequestMatcher接口,但在引擎盖下使用了不同的表达语言。
antMatcher("/users/**") matches any path starting with /users
antMatchers("/users") matches only the exact /users URL
mvcMatchers("/users") matches /users, /users/, /users.html
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/users/movie/**") // matches any path starting with /users/movie
.hasRole("ADMIN") ...
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1914 次 |
| 最近记录: |