【问题标题】:Spring Security, OAUTH2, dynamic client-secretSpring Security,OAUTH2,动态客户端秘密
【发布时间】:2021-03-07 11:31:32
【问题描述】:

Spring Security 5.4.0 版

通常 client-id 和 client-secret 是 Oauth2 提供者提供的值,它们对于每个客户端都是永久的,可以像这样在配置文件中指定

spring:
  security:
    oauth2:
      client:
        registration:
          google:
            client-id: google-client-id
            client-secret: google-client-secret

但在我的情况下,我已经修复了客户端 ID,但客户端秘密是基于某些参数生成的,每次尝试获取身份验证代码时都会根据这些参数生成。 在最后一个类ClientRegistration 中,client-secret 被定义为一个字符串值,所以在我的情况下不可能采用这个类

我的问题是否可以在这种情况下使用 Spring Security,如果可以,可以采用/配置什么?

【问题讨论】:

    标签: spring-security oauth


    【解决方案1】:

    听起来你可能在做PKCE

    在这种情况下,you can leave the secret empty 和 Spring Security 将生成代码质询和验证器作为 /authorize 请求的一部分。

    来自文档:

    使用代码交换证明密钥 (PKCE) 支持公共客户端。如果客户端在不受信任的环境中运行(例如本机应用程序或基于 Web 浏览器的应用程序),因此无法维护其凭据的机密性,则在满足以下条件时将自动使用 PKCE:

    client-secret 被省略(或为空)

    client-authentication-method 设置为 "none" (ClientAuthenticationMethod.NONE)

    如果您正在做一些定制的事情,那么您可能会考虑标准化为 PKCE。

    但是,如果您不能这样做,那么 Spring Security 会为 adding custom parameters to the /authorize/token requests 提供各种挂钩。我建议看看DefaultOAuth2AuthorizationRequestResolver#setAuthorizationRequestCustomizerDefaultAuthorizationCodeTokenResponseClient#setRequestEntityConverter

    您可以像这样在 DSL 中注册您的自定义授权请求和令牌请求:

    http
        .oauth2Login((oauth2) -> oauth2
            .authorizeEndpoint((authorize) -> authorize
                .authorizeRequestResolver(...)
            )
            .tokenEndpoint((token) -> token
                .accessTokenResponseClient(...)
            )
        );
    

    【讨论】:

      猜你喜欢
      • 2012-10-22
      • 2013-06-20
      • 2019-04-06
      • 2015-11-05
      • 1970-01-01
      • 2017-09-17
      • 2020-02-11
      • 2015-06-25
      • 2022-11-08
      相关资源
      最近更新 更多