문제 상황
jpa의 ddl-auto가 create-drop
인 상황에서 security 전용 테이블을 등록하는 schema.sql이 작동하지 않는 상황
가장 좋은 해결책(?)
- 저는 해당 security 전용 테이블도
jpa에서 관리하게 하면
같은 시점에 create-drop되기 때문에 문제되지 않을 것 같습니다.
- 저희가 당분간은 create-drop을 사용하기 때문에 schema.sql을 혼용해서 사용하는 것은 혼란을 주는 것이라 생각되긴 해요.
임시 해결책
WebSecurityConfigurerAdapter
의 public API인 getApplicationoContext()
를 활용
- 해당 빈이 생성된 시점에는 applicationContext의 bean들을 전부 사용할 수 있을 것이라 추론
- postConstruct에서 DataSource를 불러와서 대상 테이블 생성 sql을 실행시킨다.
- 시스템이 종료되기 직전에도 datasource를 불러와서 대상 테이블 drop sql을 실행시킨다.
@PostConstruct
public void setSchema() {
var datasource = getApplicationContext().getBean(DataSource.class);
new JdbcTemplate(datasource).update("CREATE TABLE oauth2_authorized_client\n"
+ "(\n"
+ " client_registration_id varchar(100) NOT NULL,\n"
+ " principal_name varchar(200) NOT NULL,\n"
+ " access_token_type varchar(100) NOT NULL,\n"
+ " access_token_value blob NOT NULL,\n"
+ " access_token_issued_at timestamp NOT NULL,\n"
+ " access_token_expires_at timestamp NOT NULL,\n"
+ " access_token_scopes varchar(1000) DEFAULT NULL,\n"
+ " refresh_token_value blob DEFAULT NULL,\n"
+ " refresh_token_issued_at timestamp DEFAULT NULL,\n"
+ " created_at timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL,\n"
+ " PRIMARY KEY (client_registration_id, principal_name)\n"
+ ");");
}
@PreDestroy
public void preDestroy() {
var datasource = getApplicationContext().getBean(DataSource.class);
new JdbcTemplate(datasource).update("DROP TABLE oauth2_authorized_client");
}