> Markdown version of [Storing Sensitive Data](https://vaadin.com/docs/v23/security/advanced-topics/external-configuration). Section index: [llms.txt](https://vaadin.com/docs/v23/security/llms.txt)

# Storing Sensitive Data in a Spring Boot Application

It’s often bad practice to put sensitive information, such as database URI, username, or password, in your `application.properties` file. If sensitive information is stored in project files or code, it might leak when you commit your project to source control management systems.

To avoid leaking sensitive information, you should consider storing sensitive information outside of your project files. In no case should you ever `git commit` passwords or other secrets into the repository.

This guide demonstrates two ways to externalize sensitive data:

- Using system environment variables

- Using an external properties file

## <a id="use-system-environment-variables"></a>Use System Environment Variables

One of the easiest ways to externalize sensitive information is to set those values as system environment variables.

For example, a [Vaadin Start](https://start.vaadin.com/) project configured to use PostgreSQL might contain the following sensitive information in the `application.properties` file.

application.properties

```properties
...
spring.datasource.url = jdbc:postgresql://localhost:5432/postgres
spring.datasource.username = postgres
spring.datasource.password = password
...
```

To replace the sensitive information with system environment variables, you should edit your file similarly to the following:

application.properties

```properties
...
spring.datasource.url = ${DB_URL}
spring.datasource.username = ${DB_USER}
spring.datasource.password = ${DB_PASSWORD}
...
```

The `${…​}` token is the special syntax for Spring’s [property placeholder](https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.external-config.files.property-placeholders), while `DB_URL`, `DB_USER`, and `DB_PASSWORD` represent the actual environment variables that you need to set in your own system.

On Linux and macOS, use the following commands in a terminal to temporarily set the environment variables.

```zsh
export DB_URL=jdbc:postgresql://localhost:5432/postgres
export DB_USER=postgres
export DB_PASSWORD=password
```

However, if you want to set the environment variables permanently, you need to add these commands to your `~/.zprofile` (*zsh*) or `~/.bashrc` (*bash*) files, depending on your system’s default shell.

On Windows, you can set the environment variable by:

1. Navigating to **Control Panel** > **System**.

2. In **System Properties**, select **Advanced** > **Environment Variables**.

3. Add new user or system environment variables by using the **New** buttons.

[Image: Add environment variable on Windows.]

## <a id="import-configuration-from-external-file"></a>Import Configuration from External File

Another simple solution to externalize sensitive information would be to import external property files using Spring Boot’s `spring.config.import` properties.

Assuming that you have an external file called `db.properties` to hold your database secrets,

db.properties

```properties
spring.datasource.url = jdbc:postgresql://localhost:5432/postgres
spring.datasource.username = postgres
spring.datasource.password = password
```

then you can import this file into your Vaadin app’s default `application.properties` instead.

application.properties

```properties
...
spring.config.import = file:/Users/MyUserName/secret/db.properties
...
```

`FCC4C231-5DB9-4950-9559-C89630042A43`
