본문 바로가기
IT 자료

PostgreSQL ssl 연결(java spring)

by 성곤 2023. 5. 21.
반응형

 

자바, 스프링(application.yml)환경에서 postgresql에 ssl 인증서를 사용하여 연결하는 방법

 

sslmode는 postgresql서버 설정에 따라  [disable, allow, prefer, require, verify-ca, verify-full] 중 선택

 

ssl 경로 유의할 것

jdbc-url: jdbc:postgresql://${dburl}:${dbport}/${dbschema}?charSet=UTF-8&defaultResultSetType=Cursor&ssl=true&sslmode=require&sslrootcert=src/main/resources/root.crt&sslcert=src/main/resources/postgresql.crt&sslkey=src/main/resources/postgresql.pk8



풀어서본다면 아래와 같다.
jdbc-url: jdbc:postgresql://${dburl}:${dbport}/${dbschema}
?charSet=UTF-8
&defaultResultSetType=Cursor
&ssl=true
&sslmode=require
&sslrootcert=src/main/resources/root.crt
&sslcert=src/main/resources/postgresql.crt
&sslkey=src/main/resources/postgresql.pk8



만약 aws batch라면 경로가 로컬과 다르다.(절대경로 /app)

jdbc-url: jdbc:postgresql://${dburl}:${dbport}/${dbschema}
?charSet=UTF-8
&defaultResultSetType=Cursor
&ssl=true
&sslmode=require
&sslrootcert=/app/resources/root.crt
&sslcert=/app/resources/postgresql.crt
&sslkey=/app/resources/postgresql.pk8

 

 

만약 pk8 인증서가 없는 경우 : postgresql.pk8 생성하기 (tistory.com)

 

 

 

 

 

 

이건 EntityManagerFactory 설정 부분

@Bean
public EntityManagerFactory entityManagerFactoryFox() {

	JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();

	LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
 
	factory.setDataSource(this.dataSourceFox());
	factory.setJpaVendorAdapter(vendorAdapter);

	ImmutableMap.Builder<String, Object> propertiesBuilder = ImmutableMap.builder();
	propertiesBuilder.put("hibernate.hbm2ddl.auto", "none");
	propertiesBuilder.put("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
	propertiesBuilder.put("hibernate.show_sql", "true");
	propertiesBuilder.put("hibernate.jdbc.lob.non_contextual_creation", "true");

	factory.setJpaPropertyMap(propertiesBuilder.build());
	factory.setPackagesToScan(new String[]{"com.fox.fox2.fox3.model"});
	factory.setPersistenceUnitName("foxname");
	factory.afterPropertiesSet();

	return factory.getObject();
}

 

참조 : java - Spring Boot connection to Postgresql with SSL - Stack Overflow

반응형