【发布时间】: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