mysql - 使用 application.yml 连接到 MySQL
问题描述
我是 Spring Boot 和 mysql 的新手。我正在尝试在 application.yml 文件中使用 mysql 配置创建一个 REST API。我的 yml 文件如下所示。
datasource:
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/test
username:
password:
jpa:
hibernate.ddl-auto: update
generate-ddl: true
show-sql: true
问题是我不想将密码放在 yaml 文件中。我想使用密钥文件或不记名令牌。我不确定如何从这个开始。请帮忙。提前致谢 :)
解决方案
你可以在这里使用 Jasypt 来加密数据库密码。您可以使用ENC()键:--
spring:
datasource:
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/test
username:ENC(<incrypted username>) // if you want
password:ENC(<encrypted password>)
jpa:
hibernate.ddl-auto: update
generate-ddl: true
show-sql: true
用于 spring-boot 的J asypt POM 依赖项:-
<!-- https://mvnrepository.com/artifact/com.github.ulisesbocchio/jasypt-spring-boot-starter -->
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
(对于不使用@SpringBootApplication或@EnableAutoConfiguration的项目,则可以直接使用jasypt-spring-boot依赖) 其他:--
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot</artifactId>
<version>2.0.0</version>
</dependency>
想了解如何加密/解密 Jasypt 密钥点击:Jasypt
更多关于 Jasypt jasypt github的详细信息
推荐阅读
- javascript - 应用程序在开发中有效,但在部署到 heroku 时无效
- python - 当扩展这个类时,它的一个方法现在需要一个额外的参数。为什么?
- latex - 从 LaTeX+BibLaTeX 到 Markdown 的 Pandoc 转换
- mysql - 选择将其所有关联项目匹配到列表的行组
- cadence-workflow - 调用子工作流时,如何防止循环依赖/无限循环
- javascript - 比较两个巨大的对象数组的最有效方法
- mongoose - Mongoose >= 5.2.0 abortTransaction() 不起作用
- google-apps-script - 打开电子表格时,如何使用 Google Sheets 应用程序上的应用程序脚本复制工作表?
- reactjs - SVG 图像视图框调整大小
- reactjs - 如何在react native的return语句中编写for循环