springboot 資料庫連結賬號密碼加密
阿新 • • 發佈:2020-12-23
技術標籤:java
一.引入依賴
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
二.進行對賬號密碼加密
package com.qike.utils; import org.jasypt.encryption.pbe.StandardPBEStringEncryptor; /** * @description: 加密 * @author: liuqiyu * @date: 2020/4/12 * @version: 1.0 */ public class EncryptUtils { public static void main(String[] arg){ StandardPBEStringEncryptor standardPBEStringEncryptor =new StandardPBEStringEncryptor(); /*配置檔案中配置如下的演算法*/ standardPBEStringEncryptor.setAlgorithm("PBEWithMD5AndDES"); /*配置檔案中配置的password*/ standardPBEStringEncryptor.setPassword("EWRREWRERWECCCXC"); /*要加密的文字*/ // 賬號 String name = standardPBEStringEncryptor.encrypt("root"); // 密碼 String password =standardPBEStringEncryptor.encrypt("123456"); /*將加密的文字寫到配置檔案中*/ System.out.println("name="+name); System.out.println("password="+password); } }
三.配置檔案中設定加密賬號密碼
application.yml
#對應著加密Util中設定的加密演算法 jasypt: encryptor: password: EWRREWRERWECCCXC algorithm: PBEWithMD5AndDES spring: datasource: name: test url: jdbc:mysql://*********:3306/table?useUnicode=true&characterEncoding=utf-8 username: ENC(加密後的賬號) password: ENC(加密後的密碼)