Day 16
์ธ๋ถ Security ํ์ผ ๋ฑ๋กํ๊ธฐ

ClientRegistrationRepository๋ฅผ ์์ฑํ๋ฉด clientId์ clientSecret๊ฐ ํ์์ธ๋ฐ ๋ก์ปฌ์์๋ application-oauth.properties๊ฐ ์์ด์ ๋ฌธ์ ๊ฐ ์์์ง๋ง
ํ์ฌ๋ EC2์์ ์งํ์ค์ด๋ฉด์ github์์ ๋ค์ด๋ก๋ ๋ฐ์ ํ๋ก์ ํธ๋ก ํ๋๋ฐ ๋ก์ปฌ์์ git์ ์ฌ๋ฆด ๋ application-oauth.properties๋ .gitignore๋ก ์ ์ธ๋์์ผ๋ก ๋นผ๋์์
์ ํ๋ฆฌ์ผ์ด์ ์ ์คํํ๊ธฐ ์ํด ๊ณต๊ฐ๋ ์ ์ฅ์์ clientId์ ClientSecret์ ์ฌ๋ฆด์๋ ์์ผ๋ ์๋ฒ์์ ์ง์ ์ด ์ค์ ๋ค์ ๊ฐ์ง๊ณ ์๋๋ก ํด์ผํจ
app๋๋ ํ ๋ฆฌ์ properties ํ์ผ์ ์์ฑ
vim /home/ec2-user/app/application-aouth.properties
๋ก ํ์ผ์ ์์ฑ, ์ฐ๋ค์์ ๋ก์ปฌ์์์ application-aouth.properties ํ์ผ์ ๊ทธ๋๋ก ๋ณต์ฌ ๋ถํ๋ฃ๊ธฐ๋ฅผ ํ๊ณ ์ ์ฅ์ข ๋ฃ๋ฅผ ํ๋ค์
๋ฐฉ๊ธ ์๋ฒ์์ ์์ฑํ application-aouth.properties์ ์ฐ๋๋ก deploy.sh๋ฅผ ์์
์ด๊ฒ์ ์ถ๊ฐ
nohup java -jar \
-Dspring.config.location=classpath:/application.
properties,/home/ec2-user/app/application-oauth.properties \
$REPOSITORY/$JAR_NAME 2>&1 &
-Dspring.config.location
์คํ๋ง ์ค์ ํ์ผ ์์น๋ฅผ ์ง์
๊ธฐ๋ณธ ์ต์ ๋ค์ ๋ด๊ณ ์๋ application.properties์ OAuth ์ค์ ๋ค์ ๋ด๊ณ ์๋ application-oauth.properties์ ์์น๋ฅผ ์ง์
classpath๊ฐ ๋ถ์ผ๋ฉด jar ์์ ์๋ resources ๋๋ ํ ๋ฆฌ๋ฅผ ๊ธฐ์ค์ผ๋ก ๊ฒฝ๋ก๊ฐ ์์ฑ๋จ
application-oauth.properties์ ์ ๋ ๊ฒฝ๋ก๋ฅผ ์ฌ์ฉ, ์ธ๋ถ์ ํ์ผ์ด ์๊ธฐ ๋๋ฌธ
์คํ๋ง ๋ถํธ ํ๋ก์ ํธ๋ก RDS ์ ๊ทผํ๊ธฐ
RDS๋ MariaDB๋ฅผ ์ใ ์ค์ธ๋ฐ ์ฌ๊ธฐ์ ์คํ๋ง๋ถํธ ํ๋ก์ ํธ๋ฅผ ์คํํ๊ธฐ ์ํด์๋ ๋ช ๊ฐ์ง ์์ ์ด ํ์
ํ ์ด๋ธ ์์ฑ : H2์์ ์๋์ผ๋ก ์์ฑํด์ฃผ๋ ํ ์ด๋ธ๋ค์ MariaDB์์ ์ง์ ์ฟผ๋ฆฌ๋ฅผ ์ด์ฉํด ์์ฑ
ํ๋ก์ ํธ ์ค์ : ์๋ฐ ํ๋ก์ ํธ๊ฐ MariaDB์ ์ ๊ทผํ๋ ค๋ฉด ๋ฐ์ดํฐ๋ฒ ์ด์ค ๋๋ผ์ด๋ฒ๊ฐ ํ์ํ๊ธฐ ๋๋ฌธ์ MariaDB์์ ์ฌ์ฉ ๊ฐ๋ฅํ ๋๋ผ์ด๋ฒ๋ฅผ ํ๋ก์ ํธ์ ์ถ๊ฐ
EC2(๋ฆฌ๋ ์ค) ์ค์ : ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ ์ ์ ๋ณด๋ ์ค์ํ๊ฒ ๋ณดํธํด์ผํ ์ ๋ณด์ด๋ค. ๋ง์ฝ ๊ณต๊ฐ๋๋ฉด ์ธ๋ถ์์ ๋ฐ์ดํฐ๋ฅผ ๋ชจ๋ ๊ฐ์ ธ๊ฐ ์ ์๊ธฐ ๋๋ฌธ์ด๋ค. ํ๋ก์ ํธ์์ ์ ์ ์ ๋ณด๋ฅผ ๊ฐ์ง๊ณ ์๋ค๋ฉด ๊นํ๋ธ์ ๊ฐ์ด ์คํ๋ ๊ณต๊ฐ์์ ๋๊ตฌ๋ ํดํนํ ์ํ์ด ์๋ค. ๊ทธ๋์ EC2 ์๋ฒ ๋ด๋ถ์์ ์ ์ ์ ๋ณด๋ฅผ ๊ด๋ฆฌํ๋๋ก ์ค์
RDSํ ์ด๋ธ์ ์์ฑ : ์ฌ๊ธฐ์๋ JPA๊ฐ ์ฌ์ฉ๋ ์ํฐํฐ ํ ์ด๋ธ๊ณผ ์คํ๋ง ์ธ์ ์ด ์ฌ์ฉ๋ ํ ์ด๋ธ 2๊ฐ์ง ์ข ๋ฅ๋ฅผ ์์ฑ
JPA๊ฐ ์ฌ์ฉํ ํ ์ด๋ธ์ ํ ์คํธ ์ฝ๋ ์ํ์, ๋ก๊ทธ๋ก ์์ฑ๋๋ ์ฟผ๋ฆฌ, schema-mysql.sqlํ์ผ์์์ ์ธ์ ํ ์ด๋ธ ์ฟผ๋ฆฌ๋ฅผ ๋ณต์ฌํด์ RDS์ ๋ฐ์
create table posts (id bigint not null auto_increment, created_date datetime, modified_date datetime, author varchar(255), content TEXT not null, title varchar(500) not null, primary key (id)) engine=InnoDB;
create table user (id bigint not null auto_increment, created_date datetime, modified_date datetime, email varchar(255) not null, name varchar(255) not null, picture varchar(255), role varchar(255) not null, primary key (id)) engine=InnoDB;
CREATE TABLE SPRING_SESSION (
PRIMARY_ID CHAR(36) NOT NULL,
SESSION_ID CHAR(36) NOT NULL,
CREATION_TIME BIGINT NOT NULL,
LAST_ACCESS_TIME BIGINT NOT NULL,
MAX_INACTIVE_INTERVAL INT NOT NULL,
EXPIRY_TIME BIGINT NOT NULL,
PRINCIPAL_NAME VARCHAR(100),
CONSTRAINT SPRING_SESSION_PK PRIMARY KEY (PRIMARY_ID)
) ENGINE=InnoDB ROW_FORMAT=DYNAMIC;
CREATE UNIQUE INDEX SPRING_SESSION_IX1 ON SPRING_SESSION (SESSION_ID);
CREATE INDEX SPRING_SESSION_IX2 ON SPRING_SESSION (EXPIRY_TIME);
CREATE INDEX SPRING_SESSION_IX3 ON SPRING_SESSION (PRINCIPAL_NAME);
CREATE TABLE SPRING_SESSION_ATTRIBUTES (
SESSION_PRIMARY_ID CHAR(36) NOT NULL,
ATTRIBUTE_NAME VARCHAR(200) NOT NULL,
ATTRIBUTE_BYTES BLOB NOT NULL,
CONSTRAINT SPRING_SESSION_ATTRIBUTES_PK PRIMARY KEY (SESSION_PRIMARY_ID, ATTRIBUTE_NAME),
CONSTRAINT SPRING_SESSION_ATTRIBUTES_FK FOREIGN KEY (SESSION_PRIMARY_ID) REFERENCES SPRING_SESSION(PRIMARY_ID) ON DELETE CASCADE
) ENGINE=InnoDB ROW_FORMAT=DYNAMIC;
ํ๋ก์ ํธ ์ค์
๋จผ์ MariaDB๋๋ผ์ด๋ฒ๋ฅผ build.gradle์ ๋ฑ๋ก
compile('org.mariadb.jdbc:mariadb-java-client')
๊ทธ๋ฆฌ๊ณ ์๋ฒ์ ๊ตฌ๋๋ ํ๊ฒฝ์ ํ๋ ๊ตฌ์ฑํ๋ค.
src/main/resources์ application-real.propertiesํ์ผ์ ์ถ๊ฐ
application-real.properties๋ก ํ์ผ์ ๋ง๋ค๋ฉด profile=real์ธ ํ๊ฒฝ์ด ๊ตฌ์ฑ๋๋ค
์ค์ ๋ก ์ด์๋ ํ๊ฒฝ์ด๊ธฐ ๋๋ฌธ์ ๋ณด์/๋ก๊ทธ์ ์ด์๊ฐ ๋ ๋งํ ์ค์ ๋ค์ ๋ชจ๋ ์ ๊ฑฐํ๋ฉฐ RDS ํ๊ฒฝ profile ์ค์ ์ด ์ถ๊ฐ
spring.profiles.include=oauth,real-db
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
spring.session.store-type=jdbc
์ด๋ ๊ฒ ํ๊ณ ํธ์ฌ
EC2 ์ค์
OAuth์ ๋ง์ฐฌ๊ฐ์ง๋ก RDS ์ ์ ์ ๋ณด๋ ๋ณดํธํด์ผํ ์ ๋ณด์ด๊ธฐ ๋๋ฌธ์ EC2 ์๋ฒ์ ์ง์ ์ค์ ํ์ผ์ ๋๋ค
app ๋๋ ํ ๋ฆฌ์ application-real-db.properties ํ์ผ์ ์์ฑ
spring.jpa.hibernate.ddl-auto=none
spring.datasource.url=jdbc:mariadb://springboot-book.cjkbpw0rm3r3.ap-northeast-2.rds.amazonaws.com:3306/springboot-bo
ok
spring.datasource.username=kyu9
spring.datasource.password=shim0329
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
spring.jpa.hibernate.ddl-auto=none
JPA๋ก ํ ์ด๋ธ์ด ์๋ ์์ฑ๋๋ ์ต์ ์ None(์์ฑํ์ง ์์)์ผ๋ก ์ง์
RDS์์๋ ์ค์ ๋ก ์ด์์ผ๋ก ์ฌ์ฉ๋ ํ ์ด๋ธ์ด๊ธฐ ๋๋ฌธ์ ์ ๋ ์คํ๋ง ๋ถํธ์์ ์๋ก ๋ง๋ค์ง ์๋๋ก ํด์ผํจ
์ด ์ต์ ์ ํ์ง ์์ผ๋ฉด ํ ์ด๋ธ์ด ๋ชจ๋ ์๋ก ์์ฑ๋ ์ ์์
์ฃผ์ํด์ผํจ
๋ง์ง๋ง์ผ๋ก deploy.sh๊ฐ real profile์ ์ฌ์ฉํ ์ ์๋๋ก ์ฝ๋์ถ๊ฐ
nohup java -jar \
-Dspring.config.location=classpath:/application.properties,/home/ec2-user/application-oauth.properties,
/home/ec2-user/app/application-real-db.properties,classpath:/application-real.properties \
-Dspring.profiles.active=real \
$REPOSITORY/$JAR_NAME 2>&1 &
Dspring.profiles.active=real
application-real.properties๋ฅผ ํ์ฑํ
application-real.properties์ spring.profiles.include=oauth,real-db ์ต์ ๋๋ฌธ์ real-db ์ญ์ ํจ๊ป ํ์ฑํ ๋์์ ํฌํจ
์์๊พธ
์ด๋ ๊ฒ ๋จ๋ฉด์ ์๋ฌ๊ฐ ๋๋๊ฑธ๊น...
์ด๋ ๊ฒ ์ค์ ํ๊ณ ./deploy.sh ๋ก ์คํํ๊ณ ๋์ nohup.out ํ์ผ์ ์ด์ด
Last updated
Was this helpful?