Spring Boot uses .env files to implement [privacy information configuration]

background

You do not want to upload private information such as IDs and passwords of various cloud services to open source places such as GitHub.

.envwhat is

.envFiles are generally used to store project environment variables. Generally, information such as cloud service passwords and database IPs can be .envstored in files instead of being placed in clear text in the project's configuration file (for example application.yml).

Usually, software or frameworks have methods to support .envfile loading configuration (instead of asking engineers to hand-write the program for loading environment variables during the project loading phase).

.envThe following describes how to save, load and use private information through the Spring Boot framework and files.

process

Step 1: .gitignoreAdd to the file.env

.envPrivate information can generally be stored in files. If we do not want to upload this private information to open source repositories such as GitHub, we must configure .envthe file accordingly, that is, configure it to be ignored by git:

.vscode/
.idea/

...

.env

Step 2: Create .envthe file

.envThe file stores data in KV format. The file format is the same *.properties. It is recommended to capitalize the Key:

USERNAME=username
PASSWORD=password

Step 3: application.yamlConfigure and use in Spring Boot

application.ymlJust configure it in spring.config.import:

spring:
  config:
    import: optional:file:.env[.properties]

app:
	username: ${
    
    USERNAME}
	password: ${
    
    PASSWORD}

Supongo que te gusta

Origin blog.csdn.net/m0_46261993/article/details/126224829
Recomendado
Clasificación