简体   繁体   English

在 Spring Boot 中定义重要凭据的最佳方法

[英]Best way define important credentials in spring boot

When using Spring Boot application we use different application.properties files according to different environments.在使用 Spring Boot 应用程序时,我们根据不同的环境使用不同的application.properties文件。

We put important credentials like: database configurations, server IPs, admin username/password and so on.我们放置了重要的凭据,例如:数据库配置、服务器 IP、管理员用户名/密码等。

I am worrying about what would happen if someone would obtain our application properties and get all important details.我担心如果有人获得我们的应用程序属性并获取所有重要细节会发生什么。

Is there any good approach to put important credentials somewhere and obtain them in our Spring Boot application based on environment?有什么好的方法可以将重要凭据放在某个地方并根据环境在我们的 Spring Boot 应用程序中获取它们吗?

Many techniques许多技术

  • Using tokens replacement (maven replacor)使用令牌替换(maven replacor)

    application.properties spring.datasource.password=#MY_DB_PASSWORD# application.properties spring.datasource.password=#MY_DB_PASSWORD#
    tokens.properties #MY_DB_PASSWORD#=SECRET_PASSWORD token.properties #MY_DB_PASSWORD#=SECRET_PASSWORD

    where tokens.properties has an access protection其中 tokens.properties 具有访问保护

  • Using environment variable使用环境变量
    mvn spring-boot:run -Dspring.datasource.password=SECRET_PASSWORD

    or simply或者干脆
    spring.datasource.password=${myDbPasswordEnv}

  • Using Jaspyt to encrypt your properties使用Jaspyt加密您的属性

One solution is to use Environment variables and property placeholders in the application properties.一种解决方案是在应用程序属性中使用环境变量和属性占位符。 Lets say, you want to store the password of the database.假设您想存储数据库的密码。 Create an environment variable:创建环境变量:

setx DEV_DB_PASS <your_dev_database_password>

Now, in the application properties file, you can access this value as:现在,在应用程序属性文件中,您可以通过以下方式访问此值:

spring.datasource.password = ${DEV_DB_PASS}

You can refer to the official documentation.可以参考官方文档。

You should use spring cloud config.您应该使用 spring 云配置。 As it is best suited for managing configuration in central place using git repository or any similar.因为它最适合使用 git 存储库或任何类似方法在中央位置管理配置。

Please never think environment variables are hidden - The proc entry env has these owned by process owner.请永远不要认为环境变量是隐藏的 - proc 条目 env 由进程所有者拥有这些。 Security by obscurity does not help.默默无闻的安全无济于事。

# ls -asl /proc/6475/environ 
0 -r-------- 1 karl karl 0 Sep 22 13:58 /proc/6475/environ

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM