【问题标题】:pass oauth2 client-id and client-secret from spring boot bean to yaml file将 oauth2 client-id 和 client-secret 从 spring boot bean 传递到 yaml 文件
【发布时间】:2021-03-12 10:46:04
【问题描述】:

我想将 oauth2 client-id 和 client-secret 从 spring boot bean 传递给 yaml,而不是直接硬编码 yaml 文件中的值。我的客户端 ID 和客户端密码是从 Harshicorp Vault 安全检索的,我想将它们安全地传递给 yaml。我怎么做 ?我尝试了类似下面的方法

@Configuration
@ConfigurationProperties(prefix = "spring.security.oauth2.registration.custom-client")
@Slf4j
public class SSOConfig {


    private String client_id;

    private String client_secret;

    public void setClient_secret(String client_secret) {
        this.client_secret = "xxxxxxxxxxxxx";
    }

    public String getClient_secret() {
        return client_secret;
    }

    public void setClient_id(String client_id) {
        this.client_id = "xxxxxxxxxxxx";
    }

    public String getClient_id() {
        return client_id;
    }

yaml

spring:
  thymeleaf:
    cache: false
  security:
    oauth2:
      client:
        registration:
          custom-client:
            client-id: (this line is removed completely)
            client-secret: (this line is removed completely)
            scope: ["openid", "profile", "email", "address", "phone", "groups"]
            provider: custom-provider
            state: xoxoxo
            redirect-uri: http://localhost:8080/login
            client-authentication-method: basic
            authorization-grant-type: authorization_code

添加这段代码后,我从 yaml 中删除了 client-id 和 client-secret,但仍然抛出错误,例如 client-id 不能为空

【问题讨论】:

  • 您不希望 oauth 配置从文件中选择属性,而是希望通过代码硬编码,这是您想要做的吗?如果是,那么您可以覆盖默认配置或定义包含 client_id 和 secret 的 clientbean。
  • 是的,阿米特,我不想从 yaml 文件中选择 client-id 和 client-secret 属性,我希望从 bean(代码)中提取它们。怎么做 ?你能指出我正确的方向吗?
  • 我会在github上发布一个带有repo的代码

标签: java spring spring-boot oauth-2.0 yaml


【解决方案1】:

我很抱歉迟到了。我不得不浏览spring docs,发现使用最新的spring security oauth2,默认实现将自动配置所有内容,但如果您想覆盖默认配置,如果您想自己提供客户端详细信息,您可以通过这种方式创建一个名为ClientRegistrationRepository@Bean 来覆盖它,

    @Bean
    public ClientRegistrationRepository clientRegistrationRepository() {
        return new InMemoryClientRegistrationRepository(this.googleClientRegistration());
    }

    private ClientRegistration googleClientRegistration() {
        return ClientRegistration.withRegistrationId("google")
            .clientId("google-client-id")
            .clientSecret("google-client-secret")
            .clientAuthenticationMethod(ClientAuthenticationMethod.BASIC)
            .authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE)
            .redirectUri("{baseUrl}/login/oauth2/code/{registrationId}")
            .scope("openid", "profile", "email", "address", "phone")
            .authorizationUri("https://accounts.google.com/o/oauth2/v2/auth")
            .tokenUri("https://www.googleapis.com/oauth2/v4/token")
            .userInfoUri("https://www.googleapis.com/oauth2/v3/userinfo")
            .userNameAttributeName(IdTokenClaimNames.SUB)
            .jwkSetUri("https://www.googleapis.com/oauth2/v3/certs")
            .clientName("Google")
            .build();
    }

详细了解覆盖自动配置。 link

【讨论】:

    猜你喜欢
    • 2017-09-08
    • 2012-01-03
    • 1970-01-01
    • 2020-02-18
    • 2021-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多