spring 使用application.yml连接到MySQL

0md85ypi  于 2023-02-07  发布在  Spring
关注(0)|答案(2)|浏览(180)

我是 Boot 和mysql的新手。我正在尝试在application.yml文件中创建一个带有mysql配置的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文件中。我想使用一个密钥文件或一个不记名令牌。我不知道我该如何开始。请帮助。提前感谢:)

ngynwnxp

ngynwnxp1#

你可以在这里使用Jasypt来加密数据库密码。你可以把它和**ENC()**key放在一起:--

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启动的Jasypt 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>

(For项目不使用@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的更多详情

zf2sa74q

zf2sa74q2#

新的**驱动程序类是-com.mysql.cj.jdbc.驱动程序

相关问题