antMatchers 如果具有相同的前缀,则会相互覆盖

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

在 Spring Boot 中,我通过扩展 WebSecurityConfigurerAdapter 类来保护端点。所以我可以通过添加来保护整个控制器

ExpressionUrlAuthorizationConfigurer<HttpSecurity>.ExpressionInterceptUrlRegistry request = http.csrf().disable().authorizeRequests();

request.antMatchers("contoller-path/**").hasAnyRole("READ_WRITE", "READ");
request.and().httpBasic();

因此,无论谁拥有角色 READ_WRITEREAD 都可以访问整个控制器。

现在,当我添加类似的内容(下面的代码片段)并尝试由仅具有 WRITE 角色的用户进行身份验证并尝试调用端点 contoller-path/update-something 时,就会出现问题。不知何故,Spring Boot 发现这些角色是在以 contoller-path 开头的端点上分配的(在该前缀上分配的角色是“READ_WRITE”、“READ”),并且它不允许我像用户那样访问该端点只有“WRITE”角色。

request.antMatchers("contoller-path/**").hasAnyRole("READ_WRITE", "READ");
request.antMatchers("contoller-path/update-something").hasAnyRole("WRITE");

换句话说,第一个 antMatcher 会覆盖第二个 antMatcher。

我通过实现 ApplicationListener 并侦听 AuthorizationFailureEvent 观察到此行为。

我希望我解释得很好。关于我做错了什么有什么想法吗?

spring-boot security
© www.soinside.com 2019 - 2024. All rights reserved.