3. Client Side Usage

To use these features in an application, just build it as a Spring Boot application that depends on spring-cloud-vault-config (e.g. see the test cases). Example Maven configuration:spring-doc.cn

Example 1. pom.xml
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.4.0.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-vault-config</artifactId>
        <version>3.0.4</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

<!-- repositories also needed for snapshots and milestones -->

Then you can create a standard Spring Boot application, like this simple HTTP server:spring-doc.cn

@SpringBootApplication
@RestController
public class Application {

    @RequestMapping("/")
    public String home() {
        return "Hello World!";
    }

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

When it runs it will pick up the external configuration from the default local Vault server on port 8200 if it is running. To modify the startup behavior you can change the location of the Vault server using application.properties, for examplespring-doc.cn

Example 2. application.yml
spring.cloud.vault:
    host: localhost
    port: 8200
    scheme: https
    uri: https://localhost:8200
    connection-timeout: 5000
    read-timeout: 15000
    config:
spring.config.import: vault://
  • host sets the hostname of the Vault host. The host name will be used for SSL certificate validationspring-doc.cn

  • port sets the Vault portspring-doc.cn

  • scheme setting the scheme to http will use plain HTTP. Supported schemes are http and https.spring-doc.cn

  • uri configure the Vault endpoint with an URI. Takes precedence over host/port/scheme configurationspring-doc.cn

  • connection-timeout sets the connection timeout in millisecondsspring-doc.cn

  • read-timeout sets the read timeout in millisecondsspring-doc.cn

  • spring.config.import mounts Vault as PropertySource using all enabled secret backends (key-value enabled by default)spring-doc.cn

Enabling further integrations requires additional dependencies and configuration. Depending on how you have set up Vault you might need additional configuration like SSL and authentication.spring-doc.cn

If the application imports the spring-boot-starter-actuator project, the status of the vault server will be available via the /health endpoint.spring-doc.cn

The vault health indicator can be enabled or disabled through the property management.health.vault.enabled (default to true).spring-doc.cn

With Spring Cloud Vault 3.0 and Spring Boot 2.4, the bootstrap context initialization (bootstrap.yml, bootstrap.properties) of property sources was deprecated. Instead, Spring Cloud Vault favors Spring Boot’s Config Data API which allows importing configuration from Vault. With Spring Boot Config Data approach, you need to set the spring.config.import property in order to bind to Vault. You can read more about it in the Config Data Locations section. You can enable the bootstrap context either by setting the configuration property spring.cloud.bootstrap.enabled=true or by including the dependency org.springframework.cloud:spring-cloud-starter-bootstrap.

3.1. Authentication

Spring Cloud Vault supports multiple authentication mechanisms to authenticate applications with Vault.spring-doc.cn

For a quickstart, use the root token printed by the Vault initialization.spring-doc.cn

Example 3. application.yml
spring.cloud.vault:
    token: 19aefa97-cccc-bbbb-aaaa-225940e63d76
spring.config.import: vault://
Consider carefully your security requirements. Static token authentication is fine if you want quickly get started with Vault, but a static token is not protected any further. Any disclosure to unintended parties allows Vault use with the associated token roles.