【问题标题】:Spring boot security, API-key protection OR oauth2 resourceServer for same resourcesSpring boot 安全性、API 密钥保护或 oauth2 resourceServer 用于相同资源
【发布时间】:2021-04-27 09:50:42
【问题描述】:

我有一个 Spring Boot 2.4 应用程序,我想用 API 密钥或资源服务器来保护它。我在想我可以在这里使用过滤器首先检查是否提供了 api 密钥,如果是,则授予对资源的访问权限,否则应该给予“第二次机会”以使用不透明的 oauth2-token 进行身份验证(机器的 api 密钥到机器,前端令牌 -> 后端)

我卡住的地方是我的安全配置今天看起来像这样(激活了资源服务器)

    @Bean
    fun springSecurityFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain? =
        http.authorizeExchange()
            .anyExchange().authenticated()
            .and()
            .oauth2ResourceServer {
                it.authenticationManagerResolver(myMultiTenantResolver)
            }
            .build()

我将如何在这里添加一个 API 保护,它应该授予访问权限(如果它成功)而不在这里调用 resourceServer-sn-p(如果它不成功,resourceServer-sn-p 应该被调用)?

【问题讨论】:

    标签: spring-boot spring-security oauth-2.0 api-key


    【解决方案1】:

    一种可能的解决方案如下:-

    1. 创建您的过滤器,即 api-key 过滤器和 auth-token 过滤器。
    2. 在 ApplicationSecurityConfiguration 的 configure(HttpSecurity http) 方法中,在 auth-token 过滤器之前添加 api-key 过滤器。
    3. 如果您传递 api-key,请将您的身份验证详细信息放在 securityContextHolder 中。在下一个过滤器(auth-token filter) 中覆盖doFilter,这里需要检查前一个过滤器是否已经通过认证,通过调用chain.doFilter(request, response) 不运行当前过滤器(auth-token filter) )。

    如果您需要完整的实现,请告诉我。

    【讨论】:

      猜你喜欢
      • 2020-11-30
      • 2017-02-09
      • 2018-07-04
      • 2017-08-18
      • 1970-01-01
      • 2019-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多