【问题标题】:Connecting to MySQL using application.yml使用 application.yml 连接到 MySQL
【发布时间】:2021-03-12 16:27:00
【问题描述】:

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

【问题讨论】:

标签: mysql spring spring-boot spring-mvc spring-data-jpa


【解决方案1】:

您可以在此处使用 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

J为 spring-boot 提供 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

【讨论】:

    猜你喜欢
    • 2015-09-05
    • 2018-03-29
    • 2015-02-15
    • 1970-01-01
    • 2017-10-21
    • 2018-04-24
    • 2017-09-10
    • 2013-02-03
    • 2012-09-18
    相关资源
    最近更新 更多