Spring
API 키 숨기기 ( Spring Legacy 에서 properties 이용하기 )
hojomu
2023. 5. 14. 22:23
API key나 아이디 , 비밀번호 같이 프로젝트를 하다보면 숨겨야할 정보들이 있다.
이번에는 spring legacy project에서 해당 정보들을 .properties 파일에 저장해서 사용하고,
깃허브에는 .properties 파일을 gitignore 파일에 추가해서 개인정보가 유출되지 않도록 할 것이다.
먼저, src/main/resources 경로에 .properties 파일을 생성하자
src/main/resources 경로는 classpath 라고도 한다.
root-context , servlet-context 설정
root-context와 servlet-context에 PropertyPlaceholderConfigurer 를 사용할 수 있도록 Bean을 설정한다
사용하기
원하는 properties를 @Value 어노테이션으로 불러와서 Controller / Service 등의 java 파일에서 사용할 수 있다.
jsp 파일에서는 ${kakaoMapApiKey} 형태로 사용할 수 있다.
Spring legacy의 경우, Spring boot나 react 보다 훨씬 불편하게 되어있다;