Spring boot项目如何保存DB用户名和密码

Sad*_*tun 3 java security spring spring-boot application.properties

当我尝试为我的 Java 项目创建安全的生产环境时,我应该在哪里保存数据库用户名、密码和 url,以保持安全,目前我正在使用应用程序属性来保存数据库凭据,请建议一些良好且安全的方法来保存数据库凭据。

应用程序属性

# This should be used only for credentials and other local-only config.
spring.datasource.url = jdbc:postgresql://localhost/database
spring.datasource.username = root
spring.datasource.password = root@123
Run Code Online (Sandbox Code Playgroud)

我应该在哪里保存安全和生产 Spring boot 项目

小智 7

当您使用 Spring Boot 时,您可以在应用程序启动时传递数据库连接参数。

例如java -jar your-spring-boot-app.jar -Dspring.datasource.url=jdbc:postgresql://localhost/database ...

您还可以将它们设置为系统环境变量:

export SPRING_DATASOURCE_URL="jdbc:postgresql://localhost/database" 然后像往常一样启动您的应用程序即可。

这样,您只需将连接参数存储在实际使用它们的服务器上,并防止意外地将它们签入代码版本控制系统。

请参阅https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

为了提高安全性,您可以使用https://projects.spring.io/spring-vault/或外部配置服务器https://spring.io/projects/spring-cloud-config