javaspringmysql-connector

springboot mysql Connection security


I used Springboot to build a Java web project, and the MySQL database connection username and password were configured in the application.yml file, but this is not secure enough. Is there any other secure way to configure user passwords for MySQL databases?

I hope to obtain a secure way to configure MySQL database user passwords, which cannot use plaintext in the code


Solution

  • you can leverage Jasypt library which provides a convenient mechanism for encrypting properties. Here's an example of how to configure Jasypt with Spring Boot:

    1.Add the Jasypt dependency to your project's pom.xml (Maven):

    <dependency>
    <groupId>com.github.ulisesbocchio</groupId>
    <artifactId>jasypt-spring-boot-starter</artifactId>
    <version>3.0.3</version>
    </dependency>
    

    2.Configure the encryption password in your application.yml:

    jasypt:
      encryptor:
       password: your_encryption_password # This should be kept secret elsewhere
    

    3.Encrypt your MySQL database password using Jasypt CLI tool or any other means supported by Jasypt. For example, if your original password is plaintextPassword, after encryption, it might look something like this:

    spring:
      datasource:
        url: jdbc:mysql://localhost:3306/your_db
        username: your_user
        password: ENC(encryptedPassword) # Replace 'encryptedPassword' with the actual encrypted value
    

    4.Now, when Spring Boot starts, Jasypt will automatically decrypt the encrypted properties.