1. Explanation
When we write Springboot projects, we need to configure database connections in the configuration file, and the username and password are configured in plain text. This is not safe and makes it easy to leak passwords.
2. Encryption solution
1. There are many types of encryption solutions. Here is a simple encryption method that I use.
2. Instructions for use:
Use the password encryption tool class to generate the encrypted string and configure it into your project configuration file. After the project is started, the springboot project will decrypt itself according to the decryption method you wrote, thus linking to your database.
3. Related codes
1、
spring: datasource: url: jdbc:mysql://127.0.0.1:3306/patient?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC username: test password: oiWRKCcmZH/pQes5KH03kgVSHza7OK/G jpa: hibernate: ddl-auto: update show-sql: true
2. Password encryption tool category
package .; import ; import ; public final class JasyptEncryptorUtils { private static final String salt = "test666"; private static BasicTextEncryptor basicTextEncryptor = new BasicTextEncryptor(); static { (salt); } private JasyptEncryptorUtils(){} /** * plain text encryption * @param plaintext * @return */ public static String encode(String plaintext){ ("Precision string:" + plaintext); String ciphertext = (plaintext); return ciphertext; } /** * Decryption * @param ciphertext * @return */ public static String decode(String ciphertext){ ciphertext = "ENC(" + ciphertext + ")"; if ((ciphertext)){ String plaintext = (ciphertext,basicTextEncryptor); return plaintext; } ("Decryption failed"); return ""; } public static void main(String[] args) { // plain text that needs to be encrypted String plaintext = "patient113"; // Encrypted plaintext String encryptedText = (plaintext); ("Encrypted string:" + encryptedText); // Decrypt the cipher text String decryptedText = (encryptedText); ("Decrypted string:" + decryptedText); } }
3. Database configuration class
package .; import ; import ; import ; import ; import ; import ; import ; import ; import ; @Configuration @EnableJpaRepositories(basePackages = ".") @EnableTransactionManagement public class DatabaseConfig { @Value("${}") private String dbUrl; @Value("${}") private String dbUsername; @Value("${}") private String dbEncryptedPassword; @Bean public DataSource dataSource() { // Use JasyptEncryptorUtils to decrypt the database password String dbPassword = (dbEncryptedPassword); return () .url(dbUrl) .username(dbUsername) .password(dbPassword) .build(); } @Bean public PlatformTransactionManager transactionManager() { JpaTransactionManager transactionManager = new JpaTransactionManager(); (dataSource()); return transactionManager; } }
Summarize
This is the end of this article about SpringBoot project configuring database password encryption. For more related content on SpringBoot configuration database password encryption, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!