spring mvc 프로젝트 내 private value 관리

violet·2024년 4월 23일

api key or 서버 경로를
java 파일에 하드 코딩하는것은 보안상 이슈가 생김.
이에 대한 대안책.

  1. DB의 특정 table에 값을 넣어두고 query로 select 해서 사용

  2. 설정파일로 빼서 관리

이 중 2번째 솔루션에 대한 내용이다.

Spring mvc (Legacy project)

  1. src/main/resources 경로에 .properties 파일을 추가함
    api.properties

  1. .properties 파일에 변수 추가
a.b.c.d.e = test String 1
a.b.c = test String2
  1. src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml 파일에 해당 코드 추가
	<beans:bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<beans:property name="locations">
			<beans:list>
				<beans:value>classpath:/api.properties</beans:value>
			</beans:list>
		</beans:property>
	</beans:bean>
  1. src/main/webapp/WEB-INF/spring/root-context.xml 파일에 해당 코드 추가
	<!-- api key 인증정보 파일 bean 등록 -->
	<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>classpath:/api.properties</value>
			</list>
		</property>
	</bean>
  1. 변수를 사용할 java 파일에 어노테이션 및 변수 선언
	@Value("${a.d.c}")
	private String testString;
  1. 변수 출력 확인
System.out.println("testString : "+testString);
profile
기억저장소

0개의 댓글