import javax.sql.DataSource;

import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

@Configuration
@PropertySource("classpath:/application.properties")
public class DatasourceConfiguration {
	@Autowired
	private ApplicationContext applicationContext;
	
	@Bean
	@ConfigurationProperties(prefix="spring.datasource.hikari")
	public HikariConfig hikariConfig() {
		return new HikariConfig();
	}
	
	@Bean
	public DataSource dataSource() throws Exception {
		DataSource dataSource = new HikariDataSource(hikariConfig());
		return dataSource;
	}
	
	@Bean
	public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
		SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
		sqlSessionFactoryBean.setDataSource(dataSource);
		
		sqlSessionFactoryBean.setConfigLocation(applicationContext.getResource("classpath:mybatis-config.xml"));
		sqlSessionFactoryBean.setMapperLocations(applicationContext.getResources("classpath:/mapper/**/*Mapper.xml"));
		return sqlSessionFactoryBean.getObject();
	}

	
	@Bean(name="batchSqlSessionTemplate") 
	public SqlSessionTemplate batchSqlSessionTemplate(SqlSessionFactory sqlSessionFactory) { 
		 return new SqlSessionTemplate(sqlSessionFactory, ExecutorType.BATCH); 
	}
	
	@Bean(name="sqlSessionTemplate") 
	public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
		return new SqlSessionTemplate(sqlSessionFactory);
	}
	
	
	/**
	 * 트렌젝션 관리
	 * @param dataSource
	 * @return
	 */
	@Bean
	public PlatformTransactionManager transactionManager(DataSource dataSource) {
		return new DataSourceTransactionManager(dataSource);
	}
	

	
}

 

 

 

 

아래와같이 적용

	/**
	 * DB컨넥션 후 Connection객체를 반환한다.
	 * @return
	 * @throws SQLException
	 */
	private Connection getConnection() throws Exception{

		ApplicationContext ctx = ApplicationContextAwareExtends.getApplicationContext();
		DataSource dataSource =  ctx.getBean(DatasourceConfiguration.class).dataSource();
		
		return dataSource.getConnection();
	}
1. 연결 유지
tools -> Preferences -> Oracle -> Connection -> Check connection 체크

2. 연결 정보 저장
tools -> Preferences -> Oracle -> Logon History -> Definition -> Store with password 체크

3. 자동 선택 하여 쿼리 실행
tools -> Preferences -> Window Types -> SQL Window -> AutoSelect statement 체크
체크 하지 않는 경우 실행 하고자 하는 쿼리를 블럭 지정 후, F8 키를 눌러 실행 하여야 한다.체크 한 경우 F8 키로 쿼리 실행시 세미콜론(;) 단위로 실행 한다.

4. Null 값의 셀 색상 설정
tools -> Preferences -> Window Types -> SQL Window -> Null value cell color 에서 선택

5. 글꼴 설정
tools -> Preferences -> User Interface -> Fonts 에서 설정

 

환경 구성

ALB / tomcat9 / spring boot 2.5.7

 

application.properties에 아래와같이 수정하여도 실제 적용이 안되는 경우

server.servlet.session.cookie.domain = [도메인]
server.servlet.session.cookie.path = /
server.servlet.session.cookie.http-only= true
server.servlet.session.cookie.same-site=None
server.servlet.session.cookie.secure=true

 

해당 설정이 적용되지 않는 이유는  AWS의 ALB를 사용하는 경우에는  AWS ALB가 기본적으로 HttpOnly 속성은 적용하고, SameSite, Secure 속성은 적용하지 않기 때문에 타겟 그룹 설정에서 쿠키 설정을 변경해야 한다고 한다.

 

하지만 ALB에서 설정을 안해주는 경우가 있다. (다른분들은 다 그냥 하시던데요..??)

 

그럼 남은건 Tomcat에 설정하는 방법이다.

 

Cookie secure 설정 : tomcat폴더/conf/sever.xml 파일을 수정한다.

<Connector connectionTimeout="20000" port="88" protocol="HTTP/1.1" redirectPort="8443" secure="true"
/>

위의 내용과 같이 https 설정이 아니어도 그냥 secure 설정 한다. (https/SSL인증서 설정은 ALB에 되어있다.)

 

sameSite 설정 : 

 

    <!-- Default set of monitored resources. If one of these changes, the    -->
    <!-- web application will be reloaded.                                   -->
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
    <WatchedResource>WEB-INF/tomcat-web.xml</WatchedResource>
    <WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>
    
    <!-- sameSite -->
    <CookieProcessor className="org.apache.tomcat.util.http.Rfc6265CookieProcessor" sameSiteCookies="Strict"/>

    <!-- Uncomment this to disable session persistence across Tomcat restarts -->
    <!--
    <Manager pathname="" />

 

 

 

 

 

+ Recent posts