Spring boot Spring预授权工作,但HTTPSecurity可能被忽略?

Spring boot Spring预授权工作,但HTTPSecurity可能被忽略?,spring-boot,spring-security,Spring Boot,Spring Security,我在我的Spring Boot应用程序中使用OpenId和Spring Boot security进行了安全设置 由于意外,我忘记将角色类型添加到我的@PreAuthorize(“hasAnyRole(“…”)”标记中,并试图以用户的身份进行调用,但被拒绝(403),但我的securityConfig文件中确实声明了hasAnyRole。一旦我将角色添加到preAuth标记中,它就工作了,但我想知道这是否是预期的行为?还是我在安全配置文件中做错了什么 我正在使用以下Spring Boot安全设置

我在我的Spring Boot应用程序中使用OpenId和Spring Boot security进行了安全设置

由于意外,我忘记将角色类型添加到我的
@PreAuthorize(“hasAnyRole(“…”)”
标记中,并试图以
用户的身份进行调用,但被拒绝(403),但我的securityConfig文件中确实声明了hasAnyRole。一旦我将角色添加到preAuth标记中,它就工作了,但我想知道这是否是预期的行为?还是我在安全配置文件中做错了什么

我正在使用以下Spring Boot安全设置

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.security.oauth.boot</groupId>
            <artifactId>spring-security-oauth2-autoconfigure</artifactId>
            <version>2.2.13.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-openid</artifactId>
        </dependency>
带有预授权标记的控制器 在这里,我一开始忘了将“USER”添加到标记中,因此被拒绝访问,但HttpSecurity方法中的上述设置不应该处理它吗

@PreAuthorize("hasAnyRole('ADMIN','DEVELOPER','USER')")
@RestController
@RequestMapping("/api/enforcementactions")
public class EnforcementActionsController {
    
    @Autowired
    private EnforcementActionsService service;

    @GetMapping("/getallactions")
    public ResponseEntity<?> getAllEnforcementActions() {
        ... do stuff here and return data
    }
@PreAuthorize(“hasAnyRole('ADMIN','DEVELOPER','USER'))
@RestController
@请求映射(“/api/enforcementactions”)
公共类强制操作控制器{
@自动连线
私人执法行动服务;
@GetMapping(“/getallactions”)
公共响应getAllEnforcementActions(){
…在此处执行操作并返回数据
}

未忽略
HttpSecurity
配置中的规则,只是在
@PreAuthorize
中的规则之前对其进行评估

角色为
user
的用户对
/api/enforcementactions
的调用将首先通过Spring安全过滤器链。
这是检查
HttpSecurity
规则的地方。
它指出,如果用户具有以下任一角色
“ADMIN”
“DEVELOPER”
“user”
,则他们可以继续操作。
有问题的用户具有角色
“user”
,因此请求将沿着筛选链继续

一旦请求通过了筛选链,则在调用控制器方法之前,将检查
@PreAuthorize
中的规则。
此规则规定,只有具有角色
“ADMIN”
“DEVELOPER”
的用户才能访问此方法,并且我们的用户只有角色
“user”
,因此他们的请求此时被拒绝

似乎只有
@PreAuthorize
规则被考虑,但这是因为它更具体。

如果
HttpSecurity
中的规则更为具体,那么请求将在到达
@PreAuthorize

之前在筛选链中被拒绝。谢谢您的明确解释
@PreAuthorize("hasAnyRole('ADMIN','DEVELOPER','USER')")
@RestController
@RequestMapping("/api/enforcementactions")
public class EnforcementActionsController {
    
    @Autowired
    private EnforcementActionsService service;

    @GetMapping("/getallactions")
    public ResponseEntity<?> getAllEnforcementActions() {
        ... do stuff here and return data
    }