首页 > 解决方案 > 使用 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 文件中。我想使用密钥文件或不记名令牌。我不确定如何从这个开始。请帮忙。提前致谢 :)

标签: mysqlspringspring-bootspring-mvcspring-data-jpa

解决方案


你可以在这里使用 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的详细信息


推荐阅读