【问题标题】:Why is the Auth0 not throwing 403 with spring-boot-starter-oauth2-resource-server when filtering by scope?为什么 Auth0 在按范围过滤时不使用 spring-boot-starter-oauth2-resource-server 抛出 403?
【发布时间】:2023-01-12 04:58:58
【问题描述】:

我有以下...

@RestController
@RequestMapping(path="/person", produces = MediaType.APPLICATION_JSON_VALUE)
public class AuthController {
  @GetMapping("")
    @ResponseBody
    public String getPersonFromEmail(@RequestParam(name = "email") String email){ ... }
}
@EnableWebSecurity
public class SecurityConfig {
    ...
    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http.authorizeHttpRequests()
                .requestMatchers("/person").hasAuthority("SCOPE_blahablah")
                .anyRequest().authenticated()
                .and().cors()
                .and().oauth2ResourceServer().jwt();
        return http.build();
    }
}

当我运行但没有传递令牌时,我得到一个 401。但是,当我传递一个没有适当范围的令牌时,我得到一个 200。我希望也得到一个 403。我错过了什么?

【问题讨论】:

    标签: spring-boot oauth auth0


    【解决方案1】:

    您应该期待的实际上是 403(禁止,这意味着身份验证有效但访问被拒绝)而不是 401(未授权,这意味着身份验证丢失或无效)。

    您是否检查过您的SecurityConfig 是否正确加载了断点或日志行?它接缝 @Configuration 不见了,我敢打赌你的资源服务器实际上是由 spring-boot 默认 SecurityFilterChain 保护的,它只适用于 anyRequest().authenticated()

    旁注,您还可以禁用会话(和 CSRF 保护),这将简化扩展和容错。您可以查看 my tutorials 以获取资源服务器配置和访问控制测试。

    【讨论】:

    • 这将更多是评论然后是答案。你是对的,它应该是 403 而不是 401,那是一个错误的输入,但它仍然没有抛出那个。它正在让请求通过。我认为至少所有内容都已加载,因为如果我删除所有内容,那么在没有令牌时我不会得到 401。
    • 此外,我在示例中没有看到您正在检查不工作的部分的范围。
    猜你喜欢
    • 2021-05-07
    • 2021-10-26
    • 2018-09-07
    • 2023-04-04
    • 2020-05-23
    • 2021-09-12
    • 2021-07-03
    • 2016-08-20
    • 2017-10-03
    相关资源
    最近更新 更多