개발자가 암호화 작동 방식에 대한 깊은 지식 없이도 최소한의 노력으로 자신의 프로젝트에 기본 암호화 기능을 추가할 수 있도록 하는 Java 라이브러리
package studio.thinkground.aroundhub.mvc.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.jasypt.encryption.StringEncryptor;
import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;
/** GitHub : https://github.com/ulisesbocchio/jasypt-spring-boot */
@Configuration
public class JasyptConfig {
@Bean(name = "jasyptStringEncryptor")
public StringEncryptor stringEncryptor() {
String password = "around_hub_studio";
PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
SimpleStringPBEConfig config = new SimpleStringPBEConfig();
config.setPassword(password); // 암호화할 때 사용하는 키
config.setAlgorithm("PBEWithMD5AndDES"); // 암호화 알고리즘
config.setKeyObtentionIterations("1000"); // 반복할 해싱 회수
config.setPoolSize("1"); // 인스턴스 pool
config.setProviderName("SunJCE");
config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator"); // salt 생성 클래스
config.setStringOutputType("base64"); // 인코딩 방식
encryptor.setConfig(config);
return encryptor;
}
}
password는 필수지만 나머지는 아니다.
https://www.devglan.com/online-tools/jasypt-online-encryption-decryption#google_vignette
void encryptTest() {
String id = "root";
String password = "djfkdnsemgjqm";
System.out.printin(jasyptEncoding(id));
System.out.printin(jasyptEncoding(password));
}
public String jasyptEncoding(String value) {
String key = "around_hub_studio";
StandardPBEStringEncryptor pbeEnc = new StandardPBEStringEncryptor();
pbeEnc.setAlgorithm("PBEWithMD5AndDES");
pbeEnc.setPassword(key);
return pbeEnc.encrypt(value);
}
## Maria DB 데이터베이스 설정
spring.datasource.driverClassName=org.mariadb.jdbc.Driver
spring.datasource.ur=jdbc:mariadb://localhost:3306/around_hub_shop
spring.datasource.username=ENC(2YwFz|vr1iAB8SspqVEB4Q==)
spring.datasource.password=ENC(192dibxsmjFBcayyQpls6td0H7xOwjeQ)
## 프로퍼티 암호화 설정
jasypt.encryptor.bean=jasyptStringEncryptor
jasypt.encryptor.bean= {@Bean(name = "jasyptStringEncryptor")의 name 값 삽입}
public class DefaultPropertyDetector implements EncryptablePropertyDetector {
private String prefix = "ENCC";
private String suffix = ")";
public DefaultPropertyDetector() {
public DefaultPropertyDetector (String prefix, String suffix) {
Assert.notNull (prefix,
message: "Prefix can't be null");
Assert. notNull(suffix,
message: "Suffix can't be null");
this. prefix = prefix;
this. suffix = suffix;
@Override
public boolean isEncrypted(String property) {
if (property == null) { // null이 들어오면 false
return false;
}
final String trimmedValue = property. trim(); // 공백 제거
return (trimmedValue.startsWith(prefix) &&
trimmedValue. endsWith (suffix)); // bool 값으로 한번 걸침.
@Override
public String unwrapEncryptedValue(String property) {
return property. substring(
prefix. length),
(property. Length) - suffix. Lengthe));
}