springboot 에서 jasypt 를 사용 하여 설정 정 보 를 암호 화 합 니 다.

jasypt 은 자바 에서 유행 하 는 오픈 소스 복호화 도구 패키지 입 니 다.
 
1.springboot 프로젝트 에 의존 도입

    com.github.ulisesbocchio
    jasypt-spring-boot-starter
    2.1.1

2.application.yml 에 jasypt.encryptor.password=yoursalt 를 설정 합 니 다.예 를 들 어
jasypt:
  encryptor:
    password: turing

 
jasypt 의 암호 화 염 값 을 설정 파일 에 직접 설정 하지 않 으 려 면 jar 명령 행 에서 프로그램 매개 변수 나 jvm 매개 변 수 를 지정 합 니 다.  -Djasypt.encryptor.password=turing -jar xxx.jar 
3.명령 행 복호화
\#명문 을 암호 화 하 는 명령 을 직접 사용 합 니 다 java-cp jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEBStringEncryption CLI input="123456"password=turing algorithm=PBEWith MD5AndDES\#직접 명령 을 사용 하여 밀 문 을 복호화 합 니 다 java-cp jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEBStringDecryption CLI input="KCQCruXa2BN+StckVPlkAg="password=turing algorithm=PBEWithMD5AndDES
 
4、 프로젝트 업무 코드 에 jasypt 사용 하기
@autowired StringEncryptor 주입  bean
package com.tingcream.springmybatis2;

import org.jasypt.encryption.StringEncryptor;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;


@RunWith(SpringRunner.class)
@SpringBootTest
public class JasyptTest {

@Autowired
private StringEncryptor stringEncryptor;


@Test
public void encrypt() {
String text ="123456";
String s1= stringEncryptor.encrypt(text);
System.out.println("        :"+s1);
String s2= stringEncryptor.decrypt(s1);
System.out.println("        :"+s2);

/*
 *   :     stringEncryptor       ,            
 *         :LTsP+Ixe26vAZYnVd28Lag==
        :123456

        :EeTv7ggGS3SVEICVd1TVdA==
        :123456

        :L0gcXucNVewXZSsFNrmVhw==
        :123456
 * 
 * */

}  
}


5.application.yml 에서 민감 한 정보 설정
spring: 
  datasource: 
    druid:
      driver-class-name: com.mysql.cj.jdbc.Driver
      username: root
      password: ENC(KCQCruXa2BN+StcKVPlkAg==)  #  jasypt       ENC   

좋은 웹페이지 즐겨찾기