0

I started a Hashicorp Vault and my secrets can be found at secret/demo-app/keycloak. I have 2 values here: clientId, clientSecret

I setup the bootstrap.properties:

spring.application.name=demo-app
spring.cloud.vault.token=00000000-0000-0000-0000-000000000000
spring.cloud.vault.scheme=http
spring.cloud.vault.kv.enabled=true

But I did not find a way to make some @Data and @Configuration classes that can read these values at startup using @ConfigurableProperties:

@Data
@Configuration
@ConfigurationProperties("keycloak")
public class Client {
    private String clientId;
    private String clientSecret;

    @PostConstruct
    public void init() {
        System.out.println("PostConstruct: " + this.toString());
    }
}

But does not seem to work:

PostConstruct: Client(clientId=null, clientSecret=null)

Any ideas what did I miss? Thanks in advance.

stacktrace2234
  • 159
  • 2
  • 11

1 Answers1

0

This is one of my working solution. I have one secret in vault, which is key=secret, values=. Please notice setters and getters should be defined accordingly.

@ConfigurationProperties("mysecret")
public class MySecret {
    private String secret;
    public String getSecret() {
        return secret;
    }
    public void setSecret(String secret) {
        this.secret = secret;
    }
}

Now to use it in any service class, we can use @EnableConfigurationProperties annotation , and just autowire it.

@Service
@EnableConfigurationProperties(MySecret.class)
class my ClientService {

@autowire private MySecret mySecret;
}

it works for me.