欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot項目配置數(shù)據(jù)庫密碼加密相關(guān)代碼

 更新時間:2024年11月11日 09:38:48   作者:程序員潘子  
這篇文章主要介紹了SpringBoot項目配置數(shù)據(jù)庫密碼加密的相關(guān)資料,本文介紹了在Springboot項目中配置數(shù)據(jù)庫連接時存在的安全問題,即用戶名和密碼以明文形式存儲,容易泄露,提出了一種簡單的加密方案,需要的朋友可以參考下

一、說明

我們在寫Springboot項目時候,配置文件中需要配置數(shù)據(jù)庫連接,用戶名和密碼都是明文配置的。這樣做很不安全,容易密碼泄露。

二、加密方案

1、加密方案有好多種,下來介紹一種本人用的,比較簡單的加密方法。

2、使用說明:

使用密碼加密工具類,生成加密后的字符串,配置到你的項目配置文件中,項目啟動后,springboot項目會根據(jù)你寫的解密方法去自行解密,從而鏈接到你的數(shù)據(jù)庫。

三、相關(guān)代碼

1、application.yml

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、密碼加密工具類

package com.jianqi.HL7Service.config;

import org.jasypt.properties.PropertyValueEncryptionUtils;
import org.jasypt.util.text.BasicTextEncryptor;

public final class JasyptEncryptorUtils {

    private static final String salt = "test666";

    private static BasicTextEncryptor basicTextEncryptor = new BasicTextEncryptor();

    static {
        basicTextEncryptor.setPassword(salt);
    }

    private JasyptEncryptorUtils(){}

    /**
     * 明文加密
     * @param plaintext
     * @return
     */
    public static String encode(String plaintext){
        System.out.println("明文字符串:" + plaintext);
        String ciphertext = basicTextEncryptor.encrypt(plaintext);
        return ciphertext;
    }

    /**
     * 解密
     * @param ciphertext
     * @return
     */
    public static String decode(String ciphertext){
        ciphertext = "ENC(" + ciphertext + ")";
        if (PropertyValueEncryptionUtils.isEncryptedValue(ciphertext)){
            String plaintext = PropertyValueEncryptionUtils.decrypt(ciphertext,basicTextEncryptor);
            return plaintext;
        }
        System.out.println("解密失敗");
        return "";
    }

    public static void main(String[] args) {
        // 需要加密的明文
        String plaintext = "patient113";

        // 加密明文
        String encryptedText = JasyptEncryptorUtils.encode(plaintext);
        System.out.println("加密后字符串:" + encryptedText);

        // 解密密文
        String decryptedText = JasyptEncryptorUtils.decode(encryptedText);
        System.out.println("解密后的字符串:" + decryptedText);
    }
}

3、數(shù)據(jù)庫配置類

package com.jianqi.HL7Service.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.boot.jdbc.DataSourceBuilder;

import javax.sql.DataSource;

@Configuration
@EnableJpaRepositories(basePackages = "com.jianqi.HL7Service.repository")
@EnableTransactionManagement
public class DatabaseConfig {

    @Value("${spring.datasource.url}")
    private String dbUrl;

    @Value("${spring.datasource.username}")
    private String dbUsername;

    @Value("${spring.datasource.password}")
    private String dbEncryptedPassword;

    @Bean
    public DataSource dataSource() {
        // 使用 JasyptEncryptorUtils 解密數(shù)據(jù)庫密碼
        String dbPassword = JasyptEncryptorUtils.decode(dbEncryptedPassword);

        return DataSourceBuilder.create()
                .url(dbUrl)
                .username(dbUsername)
                .password(dbPassword)
                .build();
    }

    @Bean
    public PlatformTransactionManager transactionManager() {
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setDataSource(dataSource());
        return transactionManager;
    }
}

總結(jié) 

到此這篇關(guān)于SpringBoot項目配置數(shù)據(jù)庫密碼加密的文章就介紹到這了,更多相關(guān)SpringBoot配置數(shù)據(jù)庫密碼加密內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論