-
Jasypt 암호화 - spring 설정파일 암호화하기Spring/spring boot 및 기타 2021. 1. 28. 16:40
스프링에서 설정파일 값을 외부에 노출하고 싶지 않을떄, Jasypt 를 사용하면 된다.
라이브러리
spring boot starter 용
3.0.3 이 작성기준 2021년 1월 28일 기준 최신버젼이다.
3,0.3 이 출시된 날짜는 2020년 5월 31일 이다.
<dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot-starter</artifactId> <version>3.0.3</version> </dependency>
위와 같이 라이브러리를 추가하면
@SpringBootApplication or @EnableAutoConfiguration 어노테이션을 메인에 추가해 주어야 한다.
이렇게 해주면 환경설정 파일 command line argument, application.properties, yaml properties 들을 암호화 할 수 있다.
@SpringBootApplication or @EnableAutoConfiguration 을 추가해주지 않으면 pom.xml 과 다른 어노테이션을 추가해주어야 한다.
<dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot</artifactId> <version>3.0.3</version> </dependency>
@Configuration @EncryptablePropertySources({@EncryptablePropertySource("classpath:encrypted.properties"), @EncryptablePropertySource("classpath:encrypted2.properties")}) public class MyApplication { ... }
위 작업이 필요하다. 그래서 그냥 @SpringBootApplication or @EnableAutoConfiguration 어노테이션 을 추가하자.
속성값에 대한 정의
Key Required Default Value jasypt.encryptor.password True - jasypt.encryptor.algorithm False PBEWITHHMACSHA512ANDAES_256 jasypt.encryptor.key-obtention-iterations False 1000 jasypt.encryptor.pool-size False 1 jasypt.encryptor.provider-name False SunJCE jasypt.encryptor.provider-class-name False null jasypt.encryptor.salt-generator-classname False org.jasypt.salt.RandomSaltGenerator jasypt.encryptor.iv-generator-classname False org.jasypt.iv.RandomIvGenerator jasypt.encryptor.string-output-type False base64 jasypt.encryptor.proxy-property-sources False false jasypt.encryptor.skip-property-sources False empty list configuration 설정파일
@Configuration public class JasyptConfig { @Bean("jasyptStringEncryptor") public StringEncryptor stringEncryptor() { PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor(); SimpleStringPBEConfig config = new SimpleStringPBEConfig(); config.setPassword("password"); config.setAlgorithm("PBEWITHHMACSHA512ANDAES_256"); config.setKeyObtentionIterations("1000"); config.setPoolSize("1"); config.setProviderName("SunJCE"); config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator"); config.setIvGeneratorClassName("org.jasypt.iv.RandomIvGenerator"); config.setStringOutputType("base64"); encryptor.setConfig(config); return encryptor; } }
password 를 제외하고 기본 값들로 작성되어 있다.
application.yml 설정 추가
jasypt: encryptor: bean: jasyptStringEncrptor
설정으로 등록한 빈의 명을 명시해줘야 한다.
암호화 및 복호화 예시
위에서 암호화를 위한 모든 작업은 끝났다. 아래와 같이 설정파일의 값을 암호화 복호화 해가면서 사용하면 된다.
@SpringBootApplication public class Application implements CommandLineRunner { public static void main(String [] args) { SpringApplication.run(Application.class, args); System.out.println("=========== Server Start ==========="); } @Override public void run(String... args) throws Exception { StandardPBEStringEncryptor pbeEnc = new StandardPBEStringEncryptor(); pbeEnc.setAlgorithm("PBEWithMD5AndDES"); pbeEnc.setPassword("test"); //2번 설정의 암호화 키를 입력 String enc = pbeEnc.encrypt("1234"); //암호화 할 내용 System.out.println("enc = " + enc); //암호화 한 내용을 출력 //테스트용 복호화 String des = pbeEnc.decrypt(enc); System.out.println("des = " + des); } }
application.yml 사용예시
datasource: url: ENC(인크립트된dburl) username: ENC(인크립트된유저명) password: ENC(인크립트된패스워드)
더 많은 사용방법 및 기능들이 아래 참고 주소에 있어서 제대로 사용할거면 아래 문서를 참고하자
https://github.com/ulisesbocchio/jasypt-spring-boot [깃허브 jasypt-spring-boot 업데이트 문서]
반응형'Spring > spring boot 및 기타' 카테고리의 다른 글
[spring] 스프링에서 사용하는 RestTemplate - http 라이브러리 (0) 2021.02.26 [spring boot] 스프링부트 에서 elasticsearch 시작하기 (1) 2021.02.24 spring boot 1.X 와 2.X 차이점 (0) 2021.01.08 [SPRING] kafka Error registering AppInfo mbean - 스프링부트 카프카 (0) 2020.11.03 [SPRING] 몽고 DB @Field 없을때 오류 org.springframework.data.mapping.PropertyReferenceException: No property log found for type (0) 2020.10.26