Bibi's DevLog ๐ค๐
[Spring] Jasypt - ์๋ฐ ์ฝ๋ ์ํธํ (์ํฌ๋ฆฟ ํค ์ํธํํ๊ธฐ) ๋ณธ๋ฌธ
๐ฅ BE ๋ฐฑ์๋/Spring ์คํ๋ง
[Spring] Jasypt - ์๋ฐ ์ฝ๋ ์ํธํ (์ํฌ๋ฆฟ ํค ์ํธํํ๊ธฐ)
๋น๋น bibi 2021. 6. 11. 00:15jasypt (์๋ฐ ํค ์ํธํ ๋ฐฉ์)
์ถ์ฒ : ์ฐ๋๊ฐ ๊ณต์ ํด์ฃผ์ ํ๊ธฐ๋ ธํธ๐โโ๏ธ
Jasypt
- Java Simplified Encryption
- ์๋ฐ ์ฝ๋ ์ํธํ ์๋น์ค ์ ๊ณต
- DB ํจ์ค์๋, OAuth Client Secret ๋ฑ ๋ฏผ๊ฐ์ ๋ณด๋ฅผ ์ํธํํ๊ธฐ ์ํด ์ฌ์ฉํ๋ค.
Jasypt ์ฌ์ฉํ๊ธฐ
1. ์์กด์ฑ ์ถ๊ฐ
build.gradle
์ ์๋ ์ฝ๋ ์ถ๊ฐ
compile 'com.github.ulisesbocchio:jasypt-spring-boot-starter:3.0.3'
2. ๋น๋ฐ๋ฒํธ ์ํธํ
https://www.devglan.com/online-tools/jasypt-online-encryption-decryption ์์ ์ํธํํ๊ณ ์ถ์ ์ฝ๋์ ์ํธํ๋ฅผ ์งํ
[Jasypt Online Encryption] ์์ญ์ผ๋ก ์คํฌ๋กค ํ
- Enter plain text to Encrypt : ์ํธํํ ์ฝ๋ ์ ๋ ฅ
- Select Type of Encryption : ์ํธํํ ๋ฐฉ์ ์ ํ - Two way๋ก ์ ํ. ๋ํดํธ ์ํธํ ์๊ณ ๋ฆฌ์ฆ์
PBEWithMD5AndDES
์ด๋ค. - Secret Key to Be Used While ... : ์ํธํ์ ์ฌ์ฉํ ์ํฌ๋ฆฟ ํค ์ ๋ ฅ. (์์์ ๋ฌธ์์ด ๋๋ ์๋ฏธ์๋ ๋จ์ด. ๋น๋ฐ๋ฒํธ ๊ฐ์ ๊ฐ๋ )
- [Encrypt]๋ฅผ ๋๋ฅด๋ฉด ์ํธํ๊ฐ ์๋ฃ๋ ๋ฌธ์์ด์ด ์๋ Encrypted String์ ์ถ๋ ฅ๋๋ค.
3. ๋ฏผ๊ฐ์ ๋ณด๋ฅผ ์ํธํ๋ ์ฝ๋๋ก ๋ณ๊ฒฝ
์ํธํํ๊ธฐ ์ ์ฝ๋๋ฅผ ENC(์ํธํํ ์ฝ๋)
๋ก ๋ก ์นํํ ๋ค, 2.์์ ์
๋ ฅํ ์ํฌ๋ฆฟ ํค๋ฅผ ์ค์ ํด ์ค๋ค.
application.properties
github.client.secrets=ENC(OQLcoaTQsAdvyxldhkju7xBUa2c8cpTgIKSgM8SipRRMsr7cLHnrwu36AKEZTgTSoE014OG84eo=)
jasypt.encryptor.password=${JASYPT_PASSWORD}
${JASYPT_PASSWORD}
: ์ํธํ์ ์ฌ์ฉํ ์ํฌ๋ฆฟํค๋ฅผ ์ธํ ๋ฆฌ์ ์ด ํ๊ฒฝ๋ณ์๋ก ๋ฑ๋ก (์ฐธ๊ณ )
4. Jasypt Configuration ํด๋์ค ์ถ๊ฐ ๋ฐ Bean ์ค์
import org.jasypt.encryption.StringEncryptor;
import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@PropertySource("application.properties")
public class JasyptConfig {
@Value("${jasypt.encryptor.password}") // ์ํฌ๋ฆฟ ํค
private String encryptKey;
@Bean("jasyptStringEncryptor")
public StringEncryptor stringEncryptor() {
PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
SimpleStringPBEConfig config = new SimpleStringPBEConfig();
config.setPassword(encryptKey);
config.setAlgorithm("PBEWithMD5AndDES");
config.setKeyObtentionIterations("1000");
config.setPoolSize("1");
config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
config.setStringOutputType("base64");
encryptor.setConfig(config);
return encryptor;
}
}
Two Way Encryption(PBEWithMD5AndDES)์ผ๋ก ์ํธํ๋ฅผ ์งํํ๋ ํด๋์ค.
${jasypt.encryptor.password}
๋ก ๊ฐ์ ธ์จenctryptKey
๋ณ์(=์ํฌ๋ฆฟํค)๋ฅผ ๊ฐ์ ธ์ดsetPassword()
์ ์ธ์๋ก ์ํฌ๋ฆฟํค๋ฅผ ์ค์ setAlgorithm("PBEWithMD5AndDES")
์ผ๋ก ์ํธํ ํ์ ์ค์
์ ํด๋์ค๋ฅผ ์ถ๊ฐ ํ, ์๋ ์ฝ๋๋ฅผ application.properties
์ ์ถ๊ฐํด JasyptStringEncryptor
๋ฅผ Bean์ผ๋ก ๋ฑ๋กํ๋ค.
application.properties
#jaspyt config jasypt.encryptor.bean=jasyptStringEncryptor
5. @EnableEncryptableProperties ์ถ๊ฐ
ํ๋ก์ ํธ mainํด๋์ค์ @EnableEncryptableProperties
์ด๋
ธํ
์ด์
์ถ๊ฐ
@SpringBootApplication
@EnableEncryptableProperties
public class AirbnbApplication {
public static void main(String[] args) {
SpringApplication.run(AirbnbApplication.class, args);
}
}
'๐ฅ BE ๋ฐฑ์๋ > Spring ์คํ๋ง' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JPA] QueryDSL ์ฌ์ฉํ๊ธฐ (0) | 2021.06.26 |
---|---|
[Spring] application.properties ์ค์ ๋ถ๋ฆฌํ๊ธฐ (0) | 2021.06.11 |
[Spring] ๊ตฌ๊ธ OAuth ๊ตฌํํ๊ธฐ (+JWT) (0) | 2021.06.10 |
[Spring] JWT๋ก ํ ํฐ ๊ธฐ๋ฐ ๋ก๊ทธ์ธ ๊ธฐ๋ฅ ๋ง๋ค๊ธฐ (0) | 2021.06.04 |
[Spring boot] ์คํฌ๋ฆฝํธ์ S3 bucket์ ์ด์ฉํ ์คํ๋ง ์ฑ ๋ฐฐํฌ ์๋ํ(+ ์ฌ๋ ๋ด, crontab ํ์ฉ) (0) | 2021.06.02 |