From c4fb1a4d33571a3b7e627a0842cdbbaed93fa565 Mon Sep 17 00:00:00 2001 From: ohhamma Date: Tue, 8 Oct 2024 15:08:31 +0900 Subject: [PATCH 01/46] KL-184/feat: add config for prod --- src/main/resources/application-prod.yaml | 28 ++++++++++++++++++++++++ src/main/resources/application.yaml | 6 ++--- 2 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 src/main/resources/application-prod.yaml diff --git a/src/main/resources/application-prod.yaml b/src/main/resources/application-prod.yaml new file mode 100644 index 00000000..fbfd0ca6 --- /dev/null +++ b/src/main/resources/application-prod.yaml @@ -0,0 +1,28 @@ +spring: + config: + activate: + on-profile: "prod" + jpa: + hibernate: + ddl-auto: validate + properties: + hibernate: + show_sql: false + format_sql: false + jdbc: + time_zone: ${TZ} + sql: + init: + mode: never + +logging: + level: + root: WARN + taco.klkl: INFO + org.springframework: WARN + org.hibernate.SQL: INFO + org.hibernate.type.descriptor.sql: INFO + + pattern: + console: "%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}" + file: "%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n" diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml index 698d6ded..eee097ca 100644 --- a/src/main/resources/application.yaml +++ b/src/main/resources/application.yaml @@ -4,10 +4,10 @@ spring: profiles: group: test: "test, h2" - local: "local, h2" - dev: "dev, mysql" + local: "local, h2, swagger" + dev: "dev, mysql, swagger" + prod: "prod, mysql" include: - - swagger - storage - oauth - jwt From c9db9885277f50599c44322ff3204e56a1ae8d34 Mon Sep 17 00:00:00 2001 From: ohhamma Date: Tue, 8 Oct 2024 15:09:21 +0900 Subject: [PATCH 02/46] KL-184/refactor: apply options for each db and environment --- src/main/resources/application-dev.yaml | 27 +++++++++++++++++++-- src/main/resources/application-h2.yaml | 19 ++------------- src/main/resources/application-local.yaml | 18 ++++++++++---- src/main/resources/application-mysql.yaml | 11 --------- src/main/resources/application-swagger.yaml | 4 +++ 5 files changed, 44 insertions(+), 35 deletions(-) diff --git a/src/main/resources/application-dev.yaml b/src/main/resources/application-dev.yaml index 8ac68c0a..8788adf2 100644 --- a/src/main/resources/application-dev.yaml +++ b/src/main/resources/application-dev.yaml @@ -2,6 +2,29 @@ spring: config: activate: on-profile: "dev" + jpa: + hibernate: + ddl-auto: update + properties: + hibernate: + show_sql: true + format_sql: true + jdbc: + time_zone: ${TZ} + defer-datasource-initialization: true + sql: + init: + data-locations: classpath:database/data-mysql.sql + mode: always -api: - main-url: ${DEV_URL} +logging: + level: + root: INFO + taco.klkl: DEBUG + org.springframework: INFO + org.hibernate.SQL: DEBUG + org.hibernate.type.descriptor.sql: DEBUG + + pattern: + console: "%d{yyyy-MM-dd HH:mm:ss} - %msg%n" + file: "%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n" diff --git a/src/main/resources/application-h2.yaml b/src/main/resources/application-h2.yaml index 35ce5ba8..f5af1bfc 100644 --- a/src/main/resources/application-h2.yaml +++ b/src/main/resources/application-h2.yaml @@ -2,31 +2,16 @@ spring: config: activate: on-profile: "h2" - jpa: - show-sql: true - database-platform: org.hibernate.dialect.H2Dialect - hibernate: - ddl-auto: create - defer-datasource-initialization: true - properties: - hibernate: - show_sql: true - format_sql: true - jdbc: - time_zone: ${TZ} datasource: driver-class-name: org.h2.Driver url: jdbc:h2:mem:klkldb;MODE=MySQL username: sa password: + jpa: + database-platform: org.hibernate.dialect.H2Dialect h2: console: enabled: true path: /h2-console settings: web-allow-others: true - sql: - init: - data-locations: classpath:database/data-h2.sql - mode: always - platform: h2 diff --git a/src/main/resources/application-local.yaml b/src/main/resources/application-local.yaml index c0fa8c52..87b492a7 100644 --- a/src/main/resources/application-local.yaml +++ b/src/main/resources/application-local.yaml @@ -3,20 +3,28 @@ spring: activate: on-profile: "local" jpa: + hibernate: + ddl-auto: create properties: hibernate: show_sql: true format_sql: true - -api: - main-url: ${LOCAL_URL} + jdbc: + time_zone: ${TZ} + defer-datasource-initialization: true + sql: + init: + data-locations: classpath:database/data-h2.sql + mode: always + platform: h2 logging: level: root: INFO - org.springframework.web: DEBUG + taco.klkl: DEBUG + org.springframework: DEBUG org.hibernate.SQL: DEBUG - org.hibernate.type.descriptor.sql.BasicBinder: TRACE + org.hibernate.type.descriptor.sql: TRACE pattern: console: "%d{yyyy-MM-dd HH:mm:ss} - %msg%n" diff --git a/src/main/resources/application-mysql.yaml b/src/main/resources/application-mysql.yaml index 8fada97c..aa89e7b4 100644 --- a/src/main/resources/application-mysql.yaml +++ b/src/main/resources/application-mysql.yaml @@ -8,15 +8,4 @@ spring: username: ${MYSQL_USER} password: ${MYSQL_PASSWORD} jpa: - hibernate: - ddl-auto: update - defer-datasource-initialization: true - properties: - hibernate: - show_sql: true - format_sql: true database-platform: org.hibernate.dialect.MySQLDialect - sql: - init: - data-locations: classpath:database/data-mysql.sql - mode: always diff --git a/src/main/resources/application-swagger.yaml b/src/main/resources/application-swagger.yaml index d73397e7..b8fb5600 100644 --- a/src/main/resources/application-swagger.yaml +++ b/src/main/resources/application-swagger.yaml @@ -1,3 +1,7 @@ +spring: + config: + activate: + on-profile: "swagger" springdoc: swagger-ui: tags-sorter: alpha From 6ad37ef257ae82e71dfdb2ae88cc8c8c896aa1f1 Mon Sep 17 00:00:00 2001 From: ohhamma Date: Tue, 8 Oct 2024 15:14:52 +0900 Subject: [PATCH 03/46] KL-184/refactor: apply test config --- src/test/resources/application-test.yaml | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/test/resources/application-test.yaml b/src/test/resources/application-test.yaml index 6ff4108c..e3077fac 100644 --- a/src/test/resources/application-test.yaml +++ b/src/test/resources/application-test.yaml @@ -3,20 +3,28 @@ spring: activate: on-profile: "test" jpa: + hibernate: + ddl-auto: create-drop properties: hibernate: show_sql: true format_sql: true -api: - main-url: ${LOCAL_URL} + jdbc: + time_zone: ${TZ} + defer-datasource-initialization: true + sql: + init: + data-locations: classpath:database/data-h2.sql + mode: always + platform: h2 logging: level: root: INFO - org.springframework.web: DEBUG + taco.klkl: DEBUG + org.springframework: DEBUG org.hibernate.SQL: DEBUG - org.hibernate.type.descriptor.sql.BasicBinder: TRACE - + org.hibernate.type.descriptor.sql: TRACE pattern: - console: "%d{yyyy-MM-dd HH:mm:ss} - %msg%n" + console: "%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n" file: "%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n" \ No newline at end of file From de6c89a413acb9e563a38cb1da41328931c2e088 Mon Sep 17 00:00:00 2001 From: ohhamma Date: Tue, 8 Oct 2024 16:18:56 +0900 Subject: [PATCH 04/46] KL-184/feat: add data for prod --- src/main/resources/sql/data-prod.sql | 113 +++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 src/main/resources/sql/data-prod.sql diff --git a/src/main/resources/sql/data-prod.sql b/src/main/resources/sql/data-prod.sql new file mode 100644 index 00000000..f1175f50 --- /dev/null +++ b/src/main/resources/sql/data-prod.sql @@ -0,0 +1,113 @@ +/* Region */ +INSERT INTO region(region_id, name) +VALUES (1, '동북아시아'), + (2, '동남아시아'), + (3, '기타'); + +/* Currency */ +INSERT INTO currency (currency_id, code, unit) +VALUES (1, 'JPY', '엔'), + (2, 'CNH', '위안'), + (3, 'TWD', '달러'), + (4, 'THB', '바트'), + (5, 'VND', '동'), + (6, 'PHP', '페소'), + (7, 'SGD', '달러'), + (8, 'IDR', '루피아'), + (9, 'MYR', '링깃'), + (10, 'USD', '달러'); + +/* Country */ +INSERT INTO country(country_id, region_id, name, code, wallpaper, currency_id) +VALUES (1, 1, '일본', 'JP', 'image/sample', 1), + (2, 1, '중국', 'CN', 'image/sample', 2), + (3, 1, '대만', 'TW', 'image/sample', 3), + (4, 2, '태국', 'TH', 'image/sample', 4), + (5, 2, '베트남', 'VN', 'image/sample', 5), + (6, 2, '필리핀', 'PH', 'image/sample', 6), + (7, 2, '싱가포르', 'SG', 'image/sample', 7), + (8, 2, '인도네시아', 'ID', 'image/sample', 8), + (9, 2, '말레이시아', 'MY', 'image/sample', 9), + (10, 3, '괌', 'GU', 'image/sample', 10), + (11, 3, '미국', 'US', 'image/sample', 10); + +/* City */ +INSERT INTO city (city_id, country_id, name) +VALUES (1, 1, '오사카'), + (2, 1, '교토'), + (3, 1, '도쿄'), + (4, 1, '후쿠오카'), + (5, 1, '오키나와'), + (6, 1, '삿포로'), + (7, 1, '나고야'), + (8, 2, '홍콩'), + (9, 2, '상하이'), + (10, 2, '베이징'), + (11, 3, '타이베이'), + (12, 4, '방콕'), + (13, 4, '치앙마이'), + (14, 4, '푸켓'), + (15, 5, '다낭'), + (16, 5, '나트랑'), + (17, 5, '호치민'), + (18, 5, '하노이'), + (19, 6, '세부'), + (20, 6, '보라카이'), + (21, 7, '싱가포르'), + (22, 8, '발리'), + (23, 9, '코타키나발루'), + (24, 9, '쿠알라룸푸르'), + (25, 10, '투몬'), + (26, 11, '뉴욕'), + (27, 11, '로스엔젤레스'), + (28, 11, '하와이'); + +/* Category */ +INSERT INTO category(category_id, name) +VALUES (1, '식품'), + (2, '의류'), + (3, '잡화'), + (4, '화장품'); + +/* Subcategory */ +INSERT INTO subcategory (subcategory_id, name, category_id) +VALUES + (1, '라면 및 즉석식품', 1), + (2, '스낵 및 과자', 1), + (3, '조미료 및 소스', 1), + (4, '보충제 및 건강식품', 1), + (5, '음료 및 차', 1), + (6, '주류', 1), + (7, '상의', 2), + (8, '하의', 2), + (9, '아우터', 2), + (10, '원피스', 2), + (11, '신발', 2), + (12, '액세사리', 2), + (13, '쥬얼리', 2), + (14, '일반의약품', 3), + (15, '주방잡화', 3), + (16, '욕실잡화', 3), + (17, '문구 및 완구', 3), + (18, '스킨케어', 4), + (19, '메이크업', 4), + (20, '헤어케어', 4), + (21, '바디케어', 4), + (22, '위생용품', 4); + +/* Tag */ +INSERT INTO tag (tag_id, name) +VALUES (1, '편의점'), + (2, '고수'); + +INSERT INTO subcategory_tag (subcategory_tag_id, subcategory_id, tag_id) +VALUES + (1, 1, 1), + (2, 1, 2), + (3, 2, 1), + (4, 2, 2), + (5, 3, 1), + (6, 3, 2), + (7, 4, 1), + (8, 5, 1), + (9, 6, 1); \ No newline at end of file From 596b10b1783c090e9209056a9cb3119000e40a24 Mon Sep 17 00:00:00 2001 From: ohhamma Date: Tue, 8 Oct 2024 16:19:15 +0900 Subject: [PATCH 05/46] KL-184/refactor: apply data for each profile --- src/main/resources/application-dev.yaml | 2 +- src/main/resources/application-local.yaml | 2 +- src/main/resources/application-prod.yaml | 6 ++++-- .../resources/{database/data-mysql.sql => sql/data-dev.sql} | 0 .../resources/{database/data-h2.sql => sql/data-local.sql} | 0 src/test/resources/application-test.yaml | 2 +- 6 files changed, 7 insertions(+), 5 deletions(-) rename src/main/resources/{database/data-mysql.sql => sql/data-dev.sql} (100%) rename src/main/resources/{database/data-h2.sql => sql/data-local.sql} (100%) diff --git a/src/main/resources/application-dev.yaml b/src/main/resources/application-dev.yaml index 8788adf2..a95d55c3 100644 --- a/src/main/resources/application-dev.yaml +++ b/src/main/resources/application-dev.yaml @@ -14,8 +14,8 @@ spring: defer-datasource-initialization: true sql: init: - data-locations: classpath:database/data-mysql.sql mode: always + data-locations: classpath:sql/data-dev.sql logging: level: diff --git a/src/main/resources/application-local.yaml b/src/main/resources/application-local.yaml index 87b492a7..e6a8ea43 100644 --- a/src/main/resources/application-local.yaml +++ b/src/main/resources/application-local.yaml @@ -14,8 +14,8 @@ spring: defer-datasource-initialization: true sql: init: - data-locations: classpath:database/data-h2.sql mode: always + data-locations: classpath:sql/data-local.sql platform: h2 logging: diff --git a/src/main/resources/application-prod.yaml b/src/main/resources/application-prod.yaml index fbfd0ca6..dd380484 100644 --- a/src/main/resources/application-prod.yaml +++ b/src/main/resources/application-prod.yaml @@ -4,16 +4,18 @@ spring: on-profile: "prod" jpa: hibernate: - ddl-auto: validate + ddl-auto: update properties: hibernate: show_sql: false format_sql: false jdbc: time_zone: ${TZ} + defer-datasource-initialization: true sql: init: - mode: never + mode: always + data-locations: classpath:sql/data-prod.sql logging: level: diff --git a/src/main/resources/database/data-mysql.sql b/src/main/resources/sql/data-dev.sql similarity index 100% rename from src/main/resources/database/data-mysql.sql rename to src/main/resources/sql/data-dev.sql diff --git a/src/main/resources/database/data-h2.sql b/src/main/resources/sql/data-local.sql similarity index 100% rename from src/main/resources/database/data-h2.sql rename to src/main/resources/sql/data-local.sql diff --git a/src/test/resources/application-test.yaml b/src/test/resources/application-test.yaml index e3077fac..3326af96 100644 --- a/src/test/resources/application-test.yaml +++ b/src/test/resources/application-test.yaml @@ -14,8 +14,8 @@ spring: defer-datasource-initialization: true sql: init: - data-locations: classpath:database/data-h2.sql mode: always + data-locations: classpath:sql/data-local.sql platform: h2 logging: From 6920713e26c932a8b445787888c58b04c686ca3d Mon Sep 17 00:00:00 2001 From: ohhamma Date: Tue, 8 Oct 2024 17:20:28 +0900 Subject: [PATCH 06/46] KL-184/refactor: add https in domain env --- .../java/taco/klkl/infra/cloudfront/CloudFrontUrlGenerator.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/taco/klkl/infra/cloudfront/CloudFrontUrlGenerator.java b/src/main/java/taco/klkl/infra/cloudfront/CloudFrontUrlGenerator.java index a1674a5d..b3e5d74d 100644 --- a/src/main/java/taco/klkl/infra/cloudfront/CloudFrontUrlGenerator.java +++ b/src/main/java/taco/klkl/infra/cloudfront/CloudFrontUrlGenerator.java @@ -18,6 +18,6 @@ private void init() { } public static String generateUrlByFileName(final String fileName) { - return "https://" + cloudFrontDomain + "/" + fileName; + return cloudFrontDomain + "/" + fileName; } } From 5da1d119d2ecf8e92f078a19e221123fbc8a6a57 Mon Sep 17 00:00:00 2001 From: ohhamma Date: Tue, 8 Oct 2024 18:35:11 +0900 Subject: [PATCH 07/46] KL-184/deploy: add workflow --- .github/workflows/deploy.yaml | 58 +++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 .github/workflows/deploy.yaml diff --git a/.github/workflows/deploy.yaml b/.github/workflows/deploy.yaml new file mode 100644 index 00000000..55f3f065 --- /dev/null +++ b/.github/workflows/deploy.yaml @@ -0,0 +1,58 @@ +name: Build and Deploy using Docker Compose + +on: + push: + branches: [ "main", "develop" ] + pull_request: + branches: [ "main", "develop" ] + workflow_dispatch: + +jobs: + build-deploy: + runs-on: ubuntu-latest + environment: dev + strategy: + matrix: + java-version: [ 17 ] + distribution: [ "zulu" ] + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Set up JDK + uses: actions/setup-java@v2 + with: + java-version: ${{ matrix.java-version }} + distribution: ${{ matrix.distribution }} + + - name: Grant execute permission for gradlew + run: chmod +x ./gradlew + + - name: Build with Gradle + run: ./gradlew clean build -x test + + - name: Dockerhub login + uses: docker/login-action@v3 + with: + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_PASSWORD }} + + - name: Build and Push Docker image + uses: docker/build-push-action@v5 + with: + context: . + file: ./Dockerfile + platforms: linux/amd64/v3 + push: true + tags: ${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKER_IMAGE_NAME }}:latest + + - name: Deploy to EC2 + uses: appleboy/ssh-action@master + with: + host: ${{ secrets.EC2_HOST }} + username: ${{ secrets.EC2_USERNAME }} + key: ${{ secrets.EC2_KEY }} + script: | + sudo docker pull ${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKER_IMAGE_NAME }}:latest + sudo docker-compose -p eonedu up -d + sudo docker image prune -a -f \ No newline at end of file From b4f3e3f4347088d45c50fbb038668dca6be921fa Mon Sep 17 00:00:00 2001 From: ohhamma Date: Tue, 8 Oct 2024 18:48:48 +0900 Subject: [PATCH 08/46] KL-184/deploy: use compose.yaml --- .github/workflows/deploy.yaml | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/.github/workflows/deploy.yaml b/.github/workflows/deploy.yaml index 55f3f065..e3c8fa37 100644 --- a/.github/workflows/deploy.yaml +++ b/.github/workflows/deploy.yaml @@ -31,7 +31,7 @@ jobs: - name: Build with Gradle run: ./gradlew clean build -x test - - name: Dockerhub login + - name: Login to Docker Hub uses: docker/login-action@v3 with: username: ${{ secrets.DOCKER_USERNAME }} @@ -46,6 +46,15 @@ jobs: push: true tags: ${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKER_IMAGE_NAME }}:latest + - name: Copy compose.yaml to EC2 + uses: appleboy/scp-action@master + with: + host: ${{ secrets.EC2_HOST }} + username: ${{ secrets.EC2_USERNAME }} + key: ${{ secrets.EC2_KEY }} + source: "compose.yaml" + target: "~/" + - name: Deploy to EC2 uses: appleboy/ssh-action@master with: @@ -54,5 +63,5 @@ jobs: key: ${{ secrets.EC2_KEY }} script: | sudo docker pull ${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKER_IMAGE_NAME }}:latest - sudo docker-compose -p eonedu up -d + sudo docker compose -f ~/compose.yaml -p klkl-server up -d sudo docker image prune -a -f \ No newline at end of file From 8846cbf73b4fbfd83afaf208ecf729597178521b Mon Sep 17 00:00:00 2001 From: ohhamma Date: Tue, 8 Oct 2024 19:04:43 +0900 Subject: [PATCH 09/46] KL-184/deploy: apply makefile --- .github/workflows/deploy.yaml | 49 +++++++++++++++++------------------ compose.yaml | 1 + 2 files changed, 25 insertions(+), 25 deletions(-) diff --git a/.github/workflows/deploy.yaml b/.github/workflows/deploy.yaml index e3c8fa37..f54dcb5b 100644 --- a/.github/workflows/deploy.yaml +++ b/.github/workflows/deploy.yaml @@ -1,4 +1,4 @@ -name: Build and Deploy using Docker Compose +name: Build and Deploy using Makefile and Docker Compose on: push: @@ -8,28 +8,36 @@ on: workflow_dispatch: jobs: - build-deploy: + build-and-deploy: runs-on: ubuntu-latest environment: dev - strategy: - matrix: - java-version: [ 17 ] - distribution: [ "zulu" ] + steps: - - name: Checkout + - name: Checkout Repository uses: actions/checkout@v4 - - name: Set up JDK - uses: actions/setup-java@v2 + - name: Set up JDK 17 + uses: actions/setup-java@v4 + with: + java-version: '17' + distribution: 'temurin' + + - name: Set up Gradle + uses: gradle/actions/setup-gradle@v3 with: - java-version: ${{ matrix.java-version }} - distribution: ${{ matrix.distribution }} + gradle-version: 8.8 - - name: Grant execute permission for gradlew - run: chmod +x ./gradlew + - name: Create .env file + run: | + touch .env + echo "${{ secrets.ENV }}" > .env + shell: bash - - name: Build with Gradle - run: ./gradlew clean build -x test + - name: Build with Makefile + run: make build + + - name: Run tests + run: make test - name: Login to Docker Hub uses: docker/login-action@v3 @@ -46,15 +54,6 @@ jobs: push: true tags: ${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKER_IMAGE_NAME }}:latest - - name: Copy compose.yaml to EC2 - uses: appleboy/scp-action@master - with: - host: ${{ secrets.EC2_HOST }} - username: ${{ secrets.EC2_USERNAME }} - key: ${{ secrets.EC2_KEY }} - source: "compose.yaml" - target: "~/" - - name: Deploy to EC2 uses: appleboy/ssh-action@master with: @@ -63,5 +62,5 @@ jobs: key: ${{ secrets.EC2_KEY }} script: | sudo docker pull ${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKER_IMAGE_NAME }}:latest - sudo docker compose -f ~/compose.yaml -p klkl-server up -d + sudo docker compose up -d sudo docker image prune -a -f \ No newline at end of file diff --git a/compose.yaml b/compose.yaml index 114b47d6..a4d87048 100644 --- a/compose.yaml +++ b/compose.yaml @@ -1,6 +1,7 @@ version: "3" services: spring: + image: ${DOCKER_USERNAME}/${DOCKER_IMAGE_NAME}:latest build: . ports: - "8080:8080" From a90071f9e7d78717b277f52a1d7a09a3fc46f598 Mon Sep 17 00:00:00 2001 From: ohhamma Date: Wed, 9 Oct 2024 12:21:42 +0900 Subject: [PATCH 10/46] KL-184/deploy: copy files to ec2 --- .github/workflows/deploy.yaml | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/.github/workflows/deploy.yaml b/.github/workflows/deploy.yaml index f54dcb5b..19eb1512 100644 --- a/.github/workflows/deploy.yaml +++ b/.github/workflows/deploy.yaml @@ -54,6 +54,15 @@ jobs: push: true tags: ${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKER_IMAGE_NAME }}:latest + - name: Copy files to EC2 + uses: appleboy/scp-action@master + with: + host: ${{ secrets.EC2_HOST }} + username: ${{ secrets.EC2_USERNAME }} + key: ${{ secrets.EC2_KEY }} + source: ".env,compose.yaml" + target: "~/" + - name: Deploy to EC2 uses: appleboy/ssh-action@master with: From e7465328bcec06ead67550379f67a1fe6e21a3b7 Mon Sep 17 00:00:00 2001 From: ohhamma Date: Wed, 9 Oct 2024 12:33:10 +0900 Subject: [PATCH 11/46] KL-184/deploy: remove makefile usage --- .github/workflows/deploy.yaml | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/.github/workflows/deploy.yaml b/.github/workflows/deploy.yaml index 19eb1512..89d591d5 100644 --- a/.github/workflows/deploy.yaml +++ b/.github/workflows/deploy.yaml @@ -1,4 +1,4 @@ -name: Build and Deploy using Makefile and Docker Compose +name: Build and Deploy using Docker Compose on: push: @@ -33,12 +33,6 @@ jobs: echo "${{ secrets.ENV }}" > .env shell: bash - - name: Build with Makefile - run: make build - - - name: Run tests - run: make test - - name: Login to Docker Hub uses: docker/login-action@v3 with: From fa162e10b0aa7a0ff154e01411362c87e846d343 Mon Sep 17 00:00:00 2001 From: ohhamma Date: Wed, 9 Oct 2024 12:36:16 +0900 Subject: [PATCH 12/46] KL-184/deploy: build with gradle --- .github/workflows/deploy.yaml | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/.github/workflows/deploy.yaml b/.github/workflows/deploy.yaml index 89d591d5..fb3815d0 100644 --- a/.github/workflows/deploy.yaml +++ b/.github/workflows/deploy.yaml @@ -16,6 +16,12 @@ jobs: - name: Checkout Repository uses: actions/checkout@v4 + - name: Create .env file + run: | + touch .env + echo "${{ secrets.ENV }}" > .env + shell: bash + - name: Set up JDK 17 uses: actions/setup-java@v4 with: @@ -27,11 +33,11 @@ jobs: with: gradle-version: 8.8 - - name: Create .env file - run: | - touch .env - echo "${{ secrets.ENV }}" > .env - shell: bash + - name: Grant execute permission for gradlew + run: chmod +x ./gradlew + + - name: Build with Gradle + run: ./gradlew clean build -x test - name: Login to Docker Hub uses: docker/login-action@v3 From a4a36224fc899481d533538e9adc94005e78bef4 Mon Sep 17 00:00:00 2001 From: ohhamma Date: Wed, 9 Oct 2024 12:41:39 +0900 Subject: [PATCH 13/46] KL-184/deploy: build with makefile --- .github/workflows/deploy.yaml | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/.github/workflows/deploy.yaml b/.github/workflows/deploy.yaml index fb3815d0..b6f9ec0c 100644 --- a/.github/workflows/deploy.yaml +++ b/.github/workflows/deploy.yaml @@ -33,11 +33,8 @@ jobs: with: gradle-version: 8.8 - - name: Grant execute permission for gradlew - run: chmod +x ./gradlew - - - name: Build with Gradle - run: ./gradlew clean build -x test + - name: Build with Makefile + run: make build - name: Login to Docker Hub uses: docker/login-action@v3 From a3d7029258fbf11b6be72fec49900570cd61c61b Mon Sep 17 00:00:00 2001 From: ohhamma Date: Wed, 9 Oct 2024 12:45:13 +0900 Subject: [PATCH 14/46] KL-184/deploy: build without test --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 7f15400c..93008c8f 100644 --- a/Makefile +++ b/Makefile @@ -18,7 +18,7 @@ compile: build: @make clean - @./gradlew build + @./gradlew build -x test test: @./gradlew test From b2737c02b8cd21b278cdff5dbcb6f57c672fb371 Mon Sep 17 00:00:00 2001 From: ohhamma Date: Thu, 10 Oct 2024 11:30:55 +0900 Subject: [PATCH 15/46] KL-184/deploy: remove copy file code --- .github/workflows/deploy.yaml | 32 +++++++++++++------------------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/.github/workflows/deploy.yaml b/.github/workflows/deploy.yaml index b6f9ec0c..bdc9039f 100644 --- a/.github/workflows/deploy.yaml +++ b/.github/workflows/deploy.yaml @@ -11,28 +11,32 @@ jobs: build-and-deploy: runs-on: ubuntu-latest environment: dev + strategy: + matrix: + java-version: [ 17 ] + distribution: [ "zulu" ] steps: - name: Checkout Repository uses: actions/checkout@v4 - - name: Create .env file - run: | - touch .env - echo "${{ secrets.ENV }}" > .env - shell: bash - - name: Set up JDK 17 uses: actions/setup-java@v4 with: - java-version: '17' - distribution: 'temurin' + java-version: ${{ matrix.java-version }} + distribution: ${{ matrix.distribution }} - name: Set up Gradle uses: gradle/actions/setup-gradle@v3 with: gradle-version: 8.8 + - name: Create .env file + run: | + touch .env + echo "${{ secrets.ENV }}" > .env + shell: bash + - name: Build with Makefile run: make build @@ -51,15 +55,6 @@ jobs: push: true tags: ${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKER_IMAGE_NAME }}:latest - - name: Copy files to EC2 - uses: appleboy/scp-action@master - with: - host: ${{ secrets.EC2_HOST }} - username: ${{ secrets.EC2_USERNAME }} - key: ${{ secrets.EC2_KEY }} - source: ".env,compose.yaml" - target: "~/" - - name: Deploy to EC2 uses: appleboy/ssh-action@master with: @@ -68,5 +63,4 @@ jobs: key: ${{ secrets.EC2_KEY }} script: | sudo docker pull ${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKER_IMAGE_NAME }}:latest - sudo docker compose up -d - sudo docker image prune -a -f \ No newline at end of file + sudo docker compose up --build -d \ No newline at end of file From 0e37a209d4e8b69efd21e3b7879810ddeceb64c9 Mon Sep 17 00:00:00 2001 From: ohhamma Date: Thu, 10 Oct 2024 11:42:40 +0900 Subject: [PATCH 16/46] KL-184/chore: remove compose version --- compose.yaml | 1 - 1 file changed, 1 deletion(-) diff --git a/compose.yaml b/compose.yaml index a4d87048..49c9875f 100644 --- a/compose.yaml +++ b/compose.yaml @@ -1,4 +1,3 @@ -version: "3" services: spring: image: ${DOCKER_USERNAME}/${DOCKER_IMAGE_NAME}:latest From 1ba8f7d53065d82c02b29b92d373c0038db5b0d8 Mon Sep 17 00:00:00 2001 From: ohhamma Date: Thu, 10 Oct 2024 11:43:56 +0900 Subject: [PATCH 17/46] KL-184/deploy: add docker login script in ec2 --- .github/workflows/deploy.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/deploy.yaml b/.github/workflows/deploy.yaml index bdc9039f..9d7ed04e 100644 --- a/.github/workflows/deploy.yaml +++ b/.github/workflows/deploy.yaml @@ -62,5 +62,6 @@ jobs: username: ${{ secrets.EC2_USERNAME }} key: ${{ secrets.EC2_KEY }} script: | + sudo docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }} sudo docker pull ${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKER_IMAGE_NAME }}:latest sudo docker compose up --build -d \ No newline at end of file From 19f7976f2c01e29f229004280bac4e7a7bd9a875 Mon Sep 17 00:00:00 2001 From: ohhamma Date: Thu, 10 Oct 2024 11:50:10 +0900 Subject: [PATCH 18/46] KL-184/deploy: copy files to ec2 --- .github/workflows/deploy.yaml | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/.github/workflows/deploy.yaml b/.github/workflows/deploy.yaml index 9d7ed04e..13ccdfe0 100644 --- a/.github/workflows/deploy.yaml +++ b/.github/workflows/deploy.yaml @@ -55,6 +55,15 @@ jobs: push: true tags: ${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKER_IMAGE_NAME }}:latest + - name: Copy files to EC2 + uses: appleboy/scp-action@master + with: + host: ${{ secrets.EC2_HOST }} + username: ${{ secrets.EC2_USERNAME }} + key: ${{ secrets.EC2_KEY }} + source: "compose.yaml,.env" + target: "~" + - name: Deploy to EC2 uses: appleboy/ssh-action@master with: @@ -62,6 +71,7 @@ jobs: username: ${{ secrets.EC2_USERNAME }} key: ${{ secrets.EC2_KEY }} script: | + cd ~ sudo docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }} sudo docker pull ${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKER_IMAGE_NAME }}:latest sudo docker compose up --build -d \ No newline at end of file From 32b2fa0047efa71574c392ea2a021c7ef53e1b3f Mon Sep 17 00:00:00 2001 From: ohhamma Date: Thu, 10 Oct 2024 14:06:10 +0900 Subject: [PATCH 19/46] KL-184/deploy: docker compose without build --- .github/workflows/deploy.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/deploy.yaml b/.github/workflows/deploy.yaml index 13ccdfe0..24bed839 100644 --- a/.github/workflows/deploy.yaml +++ b/.github/workflows/deploy.yaml @@ -74,4 +74,4 @@ jobs: cd ~ sudo docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }} sudo docker pull ${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKER_IMAGE_NAME }}:latest - sudo docker compose up --build -d \ No newline at end of file + sudo docker compose up -d \ No newline at end of file From 637bba313f05dc48e239648a50e3d2b8d993b951 Mon Sep 17 00:00:00 2001 From: ohhamma Date: Thu, 10 Oct 2024 15:04:08 +0900 Subject: [PATCH 20/46] KL-184/deploy: build with gradle --- .github/workflows/deploy.yaml | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/.github/workflows/deploy.yaml b/.github/workflows/deploy.yaml index 24bed839..3cdffed0 100644 --- a/.github/workflows/deploy.yaml +++ b/.github/workflows/deploy.yaml @@ -31,14 +31,17 @@ jobs: with: gradle-version: 8.8 + - name: Grant execute permission for gradlew + run: chmod +x ./gradlew + - name: Create .env file run: | touch .env echo "${{ secrets.ENV }}" > .env shell: bash - - name: Build with Makefile - run: make build + - name: Build with Gradle + run: ./gradlew clean build -x test - name: Login to Docker Hub uses: docker/login-action@v3 From 45e5b960d055f3388678f4ea88a222286974c1b1 Mon Sep 17 00:00:00 2001 From: ohhamma Date: Thu, 10 Oct 2024 15:32:35 +0900 Subject: [PATCH 21/46] KL-184/test: add active profile for tests --- .../category/controller/category/CategoryControllerTest.java | 2 ++ .../category/integration/category/CategoryIntegrationTest.java | 2 ++ .../klkl/domain/comment/controller/CommentControllerTest.java | 2 ++ .../klkl/domain/comment/integration/CommentIntegrationTest.java | 2 ++ .../taco/klkl/domain/like/controller/LikeControllerTest.java | 2 ++ .../taco/klkl/domain/like/integration/LikeIntegrationTest.java | 2 ++ .../klkl/domain/member/controller/MemberControllerTest.java | 2 ++ .../klkl/domain/member/integration/MemberIntegrationTest.java | 2 ++ .../notification/controller/NotificationControllerTest.java | 2 ++ .../notification/integration/NotificationIntegrationTest.java | 2 ++ .../klkl/domain/product/controller/ProductControllerTest.java | 2 ++ .../klkl/domain/product/integration/ProductIntegrationTest.java | 2 ++ .../domain/region/controller/country/CountryControllerTest.java | 2 ++ .../region/controller/currency/CurrencyControllerTest.java | 2 ++ .../domain/region/controller/region/RegionControllerTest.java | 2 ++ .../region/integration/country/CountryIntegrationTest.java | 2 ++ .../domain/region/integration/region/RegionIntegrationTest.java | 2 ++ .../klkl/domain/search/controller/SearchControllerTest.java | 2 ++ .../klkl/domain/search/integration/SearchIntegrationTest.java | 2 ++ 19 files changed, 38 insertions(+) diff --git a/src/test/java/taco/klkl/domain/category/controller/category/CategoryControllerTest.java b/src/test/java/taco/klkl/domain/category/controller/category/CategoryControllerTest.java index 8f857f63..5ca601b6 100644 --- a/src/test/java/taco/klkl/domain/category/controller/category/CategoryControllerTest.java +++ b/src/test/java/taco/klkl/domain/category/controller/category/CategoryControllerTest.java @@ -15,6 +15,7 @@ import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.context.annotation.Import; import org.springframework.http.MediaType; +import org.springframework.test.context.ActiveProfiles; import org.springframework.test.web.servlet.MockMvc; import taco.klkl.domain.category.domain.category.Category; @@ -31,6 +32,7 @@ @WebMvcTest(CategoryController.class) @Import(TestSecurityConfig.class) +@ActiveProfiles("test") public class CategoryControllerTest { @Autowired diff --git a/src/test/java/taco/klkl/domain/category/integration/category/CategoryIntegrationTest.java b/src/test/java/taco/klkl/domain/category/integration/category/CategoryIntegrationTest.java index 26cd9d55..54bfdace 100644 --- a/src/test/java/taco/klkl/domain/category/integration/category/CategoryIntegrationTest.java +++ b/src/test/java/taco/klkl/domain/category/integration/category/CategoryIntegrationTest.java @@ -12,6 +12,7 @@ import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.http.MediaType; +import org.springframework.test.context.ActiveProfiles; import org.springframework.test.web.servlet.MockMvc; import jakarta.transaction.Transactional; @@ -21,6 +22,7 @@ @SpringBootTest @AutoConfigureMockMvc @Transactional +@ActiveProfiles("test") class CategoryIntegrationTest { @Autowired diff --git a/src/test/java/taco/klkl/domain/comment/controller/CommentControllerTest.java b/src/test/java/taco/klkl/domain/comment/controller/CommentControllerTest.java index f8350433..a4a6b1d0 100644 --- a/src/test/java/taco/klkl/domain/comment/controller/CommentControllerTest.java +++ b/src/test/java/taco/klkl/domain/comment/controller/CommentControllerTest.java @@ -18,6 +18,7 @@ import org.springframework.context.annotation.Import; import org.springframework.http.MediaType; import org.springframework.security.test.context.support.WithMockUser; +import org.springframework.test.context.ActiveProfiles; import org.springframework.test.web.servlet.MockMvc; import com.fasterxml.jackson.databind.ObjectMapper; @@ -54,6 +55,7 @@ @WebMvcTest(CommentController.class) @Import(TestSecurityConfig.class) @WithMockUser(username = TEST_UUID, roles = "USER") +@ActiveProfiles("test") public class CommentControllerTest { @Autowired diff --git a/src/test/java/taco/klkl/domain/comment/integration/CommentIntegrationTest.java b/src/test/java/taco/klkl/domain/comment/integration/CommentIntegrationTest.java index 1912e7ab..c61430cd 100644 --- a/src/test/java/taco/klkl/domain/comment/integration/CommentIntegrationTest.java +++ b/src/test/java/taco/klkl/domain/comment/integration/CommentIntegrationTest.java @@ -16,6 +16,7 @@ import org.springframework.context.annotation.Import; import org.springframework.http.MediaType; import org.springframework.security.test.context.support.WithMockUser; +import org.springframework.test.context.ActiveProfiles; import org.springframework.test.web.servlet.MockMvc; import org.springframework.transaction.annotation.Transactional; @@ -34,6 +35,7 @@ @Transactional @Import(TestSecurityConfig.class) @WithMockUser(username = TEST_UUID, roles = "USER") +@ActiveProfiles("test") public class CommentIntegrationTest { @Autowired diff --git a/src/test/java/taco/klkl/domain/like/controller/LikeControllerTest.java b/src/test/java/taco/klkl/domain/like/controller/LikeControllerTest.java index 66b5d90c..1887d095 100644 --- a/src/test/java/taco/klkl/domain/like/controller/LikeControllerTest.java +++ b/src/test/java/taco/klkl/domain/like/controller/LikeControllerTest.java @@ -14,6 +14,7 @@ import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.context.annotation.Import; +import org.springframework.test.context.ActiveProfiles; import org.springframework.test.web.servlet.MockMvc; import taco.klkl.domain.like.dto.response.LikeResponse; @@ -28,6 +29,7 @@ @WebMvcTest(LikeController.class) @Import(TestSecurityConfig.class) +@ActiveProfiles("test") class LikeControllerTest { @Autowired diff --git a/src/test/java/taco/klkl/domain/like/integration/LikeIntegrationTest.java b/src/test/java/taco/klkl/domain/like/integration/LikeIntegrationTest.java index ada08992..6369b851 100644 --- a/src/test/java/taco/klkl/domain/like/integration/LikeIntegrationTest.java +++ b/src/test/java/taco/klkl/domain/like/integration/LikeIntegrationTest.java @@ -13,6 +13,7 @@ import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.context.annotation.Import; import org.springframework.security.test.context.support.WithMockUser; +import org.springframework.test.context.ActiveProfiles; import org.springframework.test.web.servlet.MockMvc; import jakarta.transaction.Transactional; @@ -26,6 +27,7 @@ @Transactional @Import(TestSecurityConfig.class) @WithMockUser(username = TEST_UUID, roles = "USER") +@ActiveProfiles("test") public class LikeIntegrationTest { @Autowired diff --git a/src/test/java/taco/klkl/domain/member/controller/MemberControllerTest.java b/src/test/java/taco/klkl/domain/member/controller/MemberControllerTest.java index 0b367718..d8959c5c 100644 --- a/src/test/java/taco/klkl/domain/member/controller/MemberControllerTest.java +++ b/src/test/java/taco/klkl/domain/member/controller/MemberControllerTest.java @@ -13,6 +13,7 @@ import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.context.annotation.Import; import org.springframework.http.MediaType; +import org.springframework.test.context.ActiveProfiles; import org.springframework.test.web.servlet.MockMvc; import taco.klkl.domain.member.domain.Member; @@ -26,6 +27,7 @@ @WebMvcTest(MemberController.class) @Import(TestSecurityConfig.class) +@ActiveProfiles("test") class MemberControllerTest { @Autowired diff --git a/src/test/java/taco/klkl/domain/member/integration/MemberIntegrationTest.java b/src/test/java/taco/klkl/domain/member/integration/MemberIntegrationTest.java index a1cc06c3..35a3e2bf 100644 --- a/src/test/java/taco/klkl/domain/member/integration/MemberIntegrationTest.java +++ b/src/test/java/taco/klkl/domain/member/integration/MemberIntegrationTest.java @@ -12,6 +12,7 @@ import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.context.annotation.Import; import org.springframework.security.test.context.support.WithMockUser; +import org.springframework.test.context.ActiveProfiles; import org.springframework.test.web.servlet.MockMvc; import org.springframework.transaction.annotation.Transactional; @@ -29,6 +30,7 @@ @Transactional @Import(TestSecurityConfig.class) @WithMockUser(username = TEST_UUID, roles = "USER") +@ActiveProfiles("test") public class MemberIntegrationTest { @Autowired diff --git a/src/test/java/taco/klkl/domain/notification/controller/NotificationControllerTest.java b/src/test/java/taco/klkl/domain/notification/controller/NotificationControllerTest.java index b433c79c..40331b60 100644 --- a/src/test/java/taco/klkl/domain/notification/controller/NotificationControllerTest.java +++ b/src/test/java/taco/klkl/domain/notification/controller/NotificationControllerTest.java @@ -15,6 +15,7 @@ import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.context.annotation.Import; +import org.springframework.test.context.ActiveProfiles; import org.springframework.test.web.servlet.MockMvc; import taco.klkl.domain.category.domain.category.Category; @@ -43,6 +44,7 @@ @WebMvcTest(NotificationController.class) @Import(TestSecurityConfig.class) +@ActiveProfiles("test") class NotificationControllerTest { @Autowired diff --git a/src/test/java/taco/klkl/domain/notification/integration/NotificationIntegrationTest.java b/src/test/java/taco/klkl/domain/notification/integration/NotificationIntegrationTest.java index 0396cdac..2a2811eb 100644 --- a/src/test/java/taco/klkl/domain/notification/integration/NotificationIntegrationTest.java +++ b/src/test/java/taco/klkl/domain/notification/integration/NotificationIntegrationTest.java @@ -16,6 +16,7 @@ import org.springframework.context.annotation.Import; import org.springframework.http.MediaType; import org.springframework.security.test.context.support.WithMockUser; +import org.springframework.test.context.ActiveProfiles; import org.springframework.test.web.servlet.MockMvc; import org.springframework.transaction.annotation.Transactional; @@ -31,6 +32,7 @@ @Transactional @Import(TestSecurityConfig.class) @WithMockUser(username = TEST_UUID, roles = "USER") +@ActiveProfiles("test") public class NotificationIntegrationTest { @Autowired diff --git a/src/test/java/taco/klkl/domain/product/controller/ProductControllerTest.java b/src/test/java/taco/klkl/domain/product/controller/ProductControllerTest.java index 0625573b..8341fb2a 100644 --- a/src/test/java/taco/klkl/domain/product/controller/ProductControllerTest.java +++ b/src/test/java/taco/klkl/domain/product/controller/ProductControllerTest.java @@ -23,6 +23,7 @@ import org.springframework.data.domain.Pageable; import org.springframework.http.MediaType; import org.springframework.security.test.context.support.WithMockUser; +import org.springframework.test.context.ActiveProfiles; import org.springframework.test.web.servlet.MockMvc; import com.fasterxml.jackson.databind.ObjectMapper; @@ -51,6 +52,7 @@ @WebMvcTest(ProductController.class) @Import(TestSecurityConfig.class) @WithMockUser(username = TEST_UUID, roles = "USER") +@ActiveProfiles("test") public class ProductControllerTest { @Autowired diff --git a/src/test/java/taco/klkl/domain/product/integration/ProductIntegrationTest.java b/src/test/java/taco/klkl/domain/product/integration/ProductIntegrationTest.java index bb80b8a6..27db589a 100644 --- a/src/test/java/taco/klkl/domain/product/integration/ProductIntegrationTest.java +++ b/src/test/java/taco/klkl/domain/product/integration/ProductIntegrationTest.java @@ -17,6 +17,7 @@ import org.springframework.context.annotation.Import; import org.springframework.http.MediaType; import org.springframework.security.test.context.support.WithMockUser; +import org.springframework.test.context.ActiveProfiles; import org.springframework.test.web.servlet.MockMvc; import org.springframework.transaction.annotation.Transactional; @@ -37,6 +38,7 @@ @Transactional @Import(TestSecurityConfig.class) @WithMockUser(username = TEST_UUID, roles = "USER") +@ActiveProfiles("test") public class ProductIntegrationTest { @Autowired diff --git a/src/test/java/taco/klkl/domain/region/controller/country/CountryControllerTest.java b/src/test/java/taco/klkl/domain/region/controller/country/CountryControllerTest.java index f7d9e0dc..72e8104b 100644 --- a/src/test/java/taco/klkl/domain/region/controller/country/CountryControllerTest.java +++ b/src/test/java/taco/klkl/domain/region/controller/country/CountryControllerTest.java @@ -12,6 +12,7 @@ import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.context.annotation.Import; import org.springframework.http.MediaType; +import org.springframework.test.context.ActiveProfiles; import org.springframework.test.web.servlet.MockMvc; import taco.klkl.domain.region.dao.country.CountryRepository; @@ -30,6 +31,7 @@ @WebMvcTest(CountryController.class) @Import(TestSecurityConfig.class) +@ActiveProfiles("test") public class CountryControllerTest { @Autowired diff --git a/src/test/java/taco/klkl/domain/region/controller/currency/CurrencyControllerTest.java b/src/test/java/taco/klkl/domain/region/controller/currency/CurrencyControllerTest.java index 32d56401..4a19228b 100644 --- a/src/test/java/taco/klkl/domain/region/controller/currency/CurrencyControllerTest.java +++ b/src/test/java/taco/klkl/domain/region/controller/currency/CurrencyControllerTest.java @@ -15,6 +15,7 @@ import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.context.annotation.Import; import org.springframework.http.MediaType; +import org.springframework.test.context.ActiveProfiles; import org.springframework.test.web.servlet.MockMvc; import taco.klkl.domain.region.dao.currency.CurrencyRepository; @@ -29,6 +30,7 @@ @WebMvcTest(CurrencyController.class) @Import(TestSecurityConfig.class) +@ActiveProfiles("test") public class CurrencyControllerTest { @Autowired diff --git a/src/test/java/taco/klkl/domain/region/controller/region/RegionControllerTest.java b/src/test/java/taco/klkl/domain/region/controller/region/RegionControllerTest.java index f4d22ef2..0b07db13 100644 --- a/src/test/java/taco/klkl/domain/region/controller/region/RegionControllerTest.java +++ b/src/test/java/taco/klkl/domain/region/controller/region/RegionControllerTest.java @@ -16,6 +16,7 @@ import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.context.annotation.Import; import org.springframework.http.MediaType; +import org.springframework.test.context.ActiveProfiles; import org.springframework.test.web.servlet.MockMvc; import taco.klkl.domain.region.domain.region.Region; @@ -29,6 +30,7 @@ @WebMvcTest(RegionController.class) @Import(TestSecurityConfig.class) +@ActiveProfiles("test") class RegionControllerTest { @Autowired diff --git a/src/test/java/taco/klkl/domain/region/integration/country/CountryIntegrationTest.java b/src/test/java/taco/klkl/domain/region/integration/country/CountryIntegrationTest.java index 04cd029d..0243cdfc 100644 --- a/src/test/java/taco/klkl/domain/region/integration/country/CountryIntegrationTest.java +++ b/src/test/java/taco/klkl/domain/region/integration/country/CountryIntegrationTest.java @@ -10,6 +10,7 @@ import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.http.MediaType; +import org.springframework.test.context.ActiveProfiles; import org.springframework.test.web.servlet.MockMvc; import jakarta.transaction.Transactional; @@ -19,6 +20,7 @@ @SpringBootTest @AutoConfigureMockMvc @Transactional +@ActiveProfiles("test") public class CountryIntegrationTest { @Autowired diff --git a/src/test/java/taco/klkl/domain/region/integration/region/RegionIntegrationTest.java b/src/test/java/taco/klkl/domain/region/integration/region/RegionIntegrationTest.java index 3b676961..895cca25 100644 --- a/src/test/java/taco/klkl/domain/region/integration/region/RegionIntegrationTest.java +++ b/src/test/java/taco/klkl/domain/region/integration/region/RegionIntegrationTest.java @@ -12,6 +12,7 @@ import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.http.MediaType; +import org.springframework.test.context.ActiveProfiles; import org.springframework.test.web.servlet.MockMvc; import jakarta.transaction.Transactional; @@ -21,6 +22,7 @@ @SpringBootTest @AutoConfigureMockMvc @Transactional +@ActiveProfiles("test") public class RegionIntegrationTest { @Autowired diff --git a/src/test/java/taco/klkl/domain/search/controller/SearchControllerTest.java b/src/test/java/taco/klkl/domain/search/controller/SearchControllerTest.java index 5c7b416f..ab1e4b0b 100644 --- a/src/test/java/taco/klkl/domain/search/controller/SearchControllerTest.java +++ b/src/test/java/taco/klkl/domain/search/controller/SearchControllerTest.java @@ -13,6 +13,7 @@ import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.context.annotation.Import; +import org.springframework.test.context.ActiveProfiles; import org.springframework.test.web.servlet.MockMvc; import taco.klkl.domain.category.domain.category.Category; @@ -38,6 +39,7 @@ @WebMvcTest(SearchController.class) @Import(TestSecurityConfig.class) +@ActiveProfiles("test") public class SearchControllerTest { @Autowired diff --git a/src/test/java/taco/klkl/domain/search/integration/SearchIntegrationTest.java b/src/test/java/taco/klkl/domain/search/integration/SearchIntegrationTest.java index cc29748e..5892a4e7 100644 --- a/src/test/java/taco/klkl/domain/search/integration/SearchIntegrationTest.java +++ b/src/test/java/taco/klkl/domain/search/integration/SearchIntegrationTest.java @@ -8,6 +8,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.ActiveProfiles; import org.springframework.test.web.servlet.MockMvc; import jakarta.transaction.Transactional; @@ -17,6 +18,7 @@ @SpringBootTest @AutoConfigureMockMvc @Transactional +@ActiveProfiles("test") public class SearchIntegrationTest { @Autowired From a2cf34e9096fe57fd95d89e3e72be4ff91fe9387 Mon Sep 17 00:00:00 2001 From: ohhamma Date: Thu, 10 Oct 2024 15:50:02 +0900 Subject: [PATCH 22/46] KL-184/deploy: minimize log for prod --- src/main/resources/application-prod.yaml | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/main/resources/application-prod.yaml b/src/main/resources/application-prod.yaml index dd380484..c548f023 100644 --- a/src/main/resources/application-prod.yaml +++ b/src/main/resources/application-prod.yaml @@ -19,12 +19,9 @@ spring: logging: level: - root: WARN - taco.klkl: INFO - org.springframework: WARN - org.hibernate.SQL: INFO - org.hibernate.type.descriptor.sql: INFO - + root: ERROR + taco.klkl: WARN + org.springframework: ERROR + org.hibernate: OFF pattern: - console: "%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}" - file: "%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n" + console: "%d{yyyy-MM-dd HH:mm:ss} %-5level %logger{36} - %msg%n" \ No newline at end of file From fc7a7fb5b2548bd75ff258b36dbe33855e569174 Mon Sep 17 00:00:00 2001 From: ohhamma Date: Thu, 10 Oct 2024 16:06:06 +0900 Subject: [PATCH 23/46] KL-184/deploy: add mysql volume --- compose.yaml | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/compose.yaml b/compose.yaml index 49c9875f..2227d424 100644 --- a/compose.yaml +++ b/compose.yaml @@ -21,9 +21,13 @@ services: container_name: "klkl-db" env_file: - .env + volumes: + - mysql_data:/var/lib/mysql healthcheck: - interval: 5s - test: [ "CMD", "mysqladmin" ,"ping", "-h", "localhost" ] + test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-u", "root", "--password=${MYSQL_ROOT_PASSWORD}"] + interval: 10s + timeout: 5s + retries: 5 restart: always networks: - dev-net @@ -31,3 +35,6 @@ services: networks: dev-net: driver: bridge + +volumes: + mysql_data: \ No newline at end of file From 81243f1a583c615c6024081c4127f4b8532cf2a3 Mon Sep 17 00:00:00 2001 From: ohhamma Date: Thu, 10 Oct 2024 16:45:11 +0900 Subject: [PATCH 24/46] KL-184/deploy: add log for prod --- src/main/resources/application-prod.yaml | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/main/resources/application-prod.yaml b/src/main/resources/application-prod.yaml index c548f023..cc3bcfde 100644 --- a/src/main/resources/application-prod.yaml +++ b/src/main/resources/application-prod.yaml @@ -19,9 +19,12 @@ spring: logging: level: - root: ERROR - taco.klkl: WARN - org.springframework: ERROR - org.hibernate: OFF + root: INFO + taco.klkl: DEBUG + org.springframework: INFO + org.hibernate.SQL: DEBUG + org.hibernate.type.descriptor.sql: DEBUG + pattern: - console: "%d{yyyy-MM-dd HH:mm:ss} %-5level %logger{36} - %msg%n" \ No newline at end of file + console: "%d{yyyy-MM-dd HH:mm:ss} - %msg%n" + file: "%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n" \ No newline at end of file From d02c4864180baaddd9c25e0be8ad746f0edceaf8 Mon Sep 17 00:00:00 2001 From: ohhamma Date: Thu, 10 Oct 2024 16:45:27 +0900 Subject: [PATCH 25/46] kL-184/deploy: rename docker network --- compose.yaml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/compose.yaml b/compose.yaml index 2227d424..f08eb6c1 100644 --- a/compose.yaml +++ b/compose.yaml @@ -12,7 +12,7 @@ services: condition: service_healthy restart: always networks: - - dev-net + - klkl-net klkl_db: image: mysql:8.0 @@ -22,7 +22,7 @@ services: env_file: - .env volumes: - - mysql_data:/var/lib/mysql + - mysql-data:/var/lib/mysql healthcheck: test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-u", "root", "--password=${MYSQL_ROOT_PASSWORD}"] interval: 10s @@ -30,11 +30,11 @@ services: retries: 5 restart: always networks: - - dev-net + - klkl-net networks: - dev-net: + klkl-net: driver: bridge volumes: - mysql_data: \ No newline at end of file + mysql-data: \ No newline at end of file From a118df57fd810bf3fd5ec42a8dfeb1fd11cfea1d Mon Sep 17 00:00:00 2001 From: ohhamma Date: Thu, 10 Oct 2024 17:15:31 +0900 Subject: [PATCH 26/46] KL-184/deploy: prune docker image after compose --- .github/workflows/deploy.yaml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/deploy.yaml b/.github/workflows/deploy.yaml index 3cdffed0..75163d5a 100644 --- a/.github/workflows/deploy.yaml +++ b/.github/workflows/deploy.yaml @@ -77,4 +77,5 @@ jobs: cd ~ sudo docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }} sudo docker pull ${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKER_IMAGE_NAME }}:latest - sudo docker compose up -d \ No newline at end of file + sudo docker compose up -d + sudo docker image prune -a -f \ No newline at end of file From 1a372f2d2bb82f70331c1b1edb88a8dfddb9704d Mon Sep 17 00:00:00 2001 From: ohhamma Date: Thu, 10 Oct 2024 17:40:18 +0900 Subject: [PATCH 27/46] KL-184/deploy: use makefile --- .github/workflows/deploy.yaml | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/.github/workflows/deploy.yaml b/.github/workflows/deploy.yaml index 75163d5a..790d6735 100644 --- a/.github/workflows/deploy.yaml +++ b/.github/workflows/deploy.yaml @@ -40,8 +40,8 @@ jobs: echo "${{ secrets.ENV }}" > .env shell: bash - - name: Build with Gradle - run: ./gradlew clean build -x test + - name: Build with Makefile + run: make build - name: Login to Docker Hub uses: docker/login-action@v3 @@ -64,7 +64,7 @@ jobs: host: ${{ secrets.EC2_HOST }} username: ${{ secrets.EC2_USERNAME }} key: ${{ secrets.EC2_KEY }} - source: "compose.yaml,.env" + source: "compose.yaml,.env,Makefile" target: "~" - name: Deploy to EC2 @@ -77,5 +77,4 @@ jobs: cd ~ sudo docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }} sudo docker pull ${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKER_IMAGE_NAME }}:latest - sudo docker compose up -d - sudo docker image prune -a -f \ No newline at end of file + sudo make re \ No newline at end of file From 829f9df704eddb82b3815c1b7140862e8d0de723 Mon Sep 17 00:00:00 2001 From: ohhamma Date: Thu, 10 Oct 2024 17:59:36 +0900 Subject: [PATCH 28/46] KL-184/deploy: docker compose down before deploy --- .github/workflows/deploy.yaml | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/.github/workflows/deploy.yaml b/.github/workflows/deploy.yaml index 790d6735..ba1ad7f7 100644 --- a/.github/workflows/deploy.yaml +++ b/.github/workflows/deploy.yaml @@ -64,7 +64,7 @@ jobs: host: ${{ secrets.EC2_HOST }} username: ${{ secrets.EC2_USERNAME }} key: ${{ secrets.EC2_KEY }} - source: "compose.yaml,.env,Makefile" + source: "compose.yaml,.env" target: "~" - name: Deploy to EC2 @@ -77,4 +77,6 @@ jobs: cd ~ sudo docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }} sudo docker pull ${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKER_IMAGE_NAME }}:latest - sudo make re \ No newline at end of file + sudo docker compose down + sudo docker system prune -af --volumes + sudo docker compose up --build -d \ No newline at end of file From c00d841cfb8504b241a05467d7990dd07640111e Mon Sep 17 00:00:00 2001 From: ohhamma Date: Thu, 10 Oct 2024 18:06:05 +0900 Subject: [PATCH 29/46] KL-184/deploy: remove build --- .github/workflows/deploy.yaml | 6 +++--- compose.yaml | 1 - 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/.github/workflows/deploy.yaml b/.github/workflows/deploy.yaml index ba1ad7f7..a4ba8df1 100644 --- a/.github/workflows/deploy.yaml +++ b/.github/workflows/deploy.yaml @@ -40,8 +40,8 @@ jobs: echo "${{ secrets.ENV }}" > .env shell: bash - - name: Build with Makefile - run: make build + - name: Build with Gradle + run: ./gradlew clean build -x test - name: Login to Docker Hub uses: docker/login-action@v3 @@ -79,4 +79,4 @@ jobs: sudo docker pull ${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKER_IMAGE_NAME }}:latest sudo docker compose down sudo docker system prune -af --volumes - sudo docker compose up --build -d \ No newline at end of file + sudo docker compose up -d \ No newline at end of file diff --git a/compose.yaml b/compose.yaml index f08eb6c1..db2abcd1 100644 --- a/compose.yaml +++ b/compose.yaml @@ -1,7 +1,6 @@ services: spring: image: ${DOCKER_USERNAME}/${DOCKER_IMAGE_NAME}:latest - build: . ports: - "8080:8080" container_name: "klkl-server" From d5a016bbca3965f25bb171b4952353bba2d4779c Mon Sep 17 00:00:00 2001 From: ohhamma Date: Fri, 11 Oct 2024 14:48:46 +0900 Subject: [PATCH 30/46] KL-184/chore: add default static html --- src/main/resources/static/index.html | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 src/main/resources/static/index.html diff --git a/src/main/resources/static/index.html b/src/main/resources/static/index.html new file mode 100644 index 00000000..bf0930f0 --- /dev/null +++ b/src/main/resources/static/index.html @@ -0,0 +1,10 @@ + + + + + Title + + +

Hello

+ + \ No newline at end of file From 57814b91a213b7c2cd3e2dc6ce4c9876eb6fdbc7 Mon Sep 17 00:00:00 2001 From: ohhamma Date: Fri, 11 Oct 2024 14:59:44 +0900 Subject: [PATCH 31/46] KL-184/fix: permit both endpoints --- .../taco/klkl/global/config/security/SecurityConfig.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/java/taco/klkl/global/config/security/SecurityConfig.java b/src/main/java/taco/klkl/global/config/security/SecurityConfig.java index 199c98ad..fb7d000d 100644 --- a/src/main/java/taco/klkl/global/config/security/SecurityConfig.java +++ b/src/main/java/taco/klkl/global/config/security/SecurityConfig.java @@ -84,6 +84,7 @@ public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Excepti .requestMatchers(HttpMethod.DELETE).hasAnyRole(USER.name(), ADMIN.name()) .requestMatchers(getUserRoleEndpoints()).hasRole(USER.name()) .requestMatchers(getPublicEndpoints()).permitAll() + .requestMatchers(getBothEndpoints()).permitAll() .anyRequest().authenticated() ) @@ -127,6 +128,10 @@ private RequestMatcher[] getUserRoleEndpoints() { return SecurityEndpoint.USER_ROLE.getMatchers(); } + private RequestMatcher[] getBothEndpoints() { + return SecurityEndpoint.BOTH.getMatchers(); + } + @Bean public CorsConfigurationSource corsConfigurationSource() { CorsConfiguration configuration = new CorsConfiguration(); From f8fc46851acb88bf071d07ece1f51246ef37d18a Mon Sep 17 00:00:00 2001 From: ohhamma Date: Fri, 11 Oct 2024 15:49:13 +0900 Subject: [PATCH 32/46] KL-184/chore: add temp favicon --- src/main/resources/static/favicon.ico | Bin 0 -> 6488 bytes 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 src/main/resources/static/favicon.ico diff --git a/src/main/resources/static/favicon.ico b/src/main/resources/static/favicon.ico new file mode 100644 index 0000000000000000000000000000000000000000..463f9f96e114f8beb98d3a62f4ecd9af6c549599 GIT binary patch literal 6488 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 literal 0 HcmV?d00001 From 70934536e8455e954d866744783ed61378dd4f05 Mon Sep 17 00:00:00 2001 From: ohhamma Date: Fri, 11 Oct 2024 16:02:16 +0900 Subject: [PATCH 33/46] KL-184/refactor: change security config sequence --- .../taco/klkl/global/config/security/SecurityConfig.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/taco/klkl/global/config/security/SecurityConfig.java b/src/main/java/taco/klkl/global/config/security/SecurityConfig.java index fb7d000d..0ec225c8 100644 --- a/src/main/java/taco/klkl/global/config/security/SecurityConfig.java +++ b/src/main/java/taco/klkl/global/config/security/SecurityConfig.java @@ -79,12 +79,12 @@ public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Excepti // request authentication & authorization .authorizeHttpRequests(authorizeRequests -> authorizeRequests + .requestMatchers(getPublicEndpoints()).permitAll() + .requestMatchers(getBothEndpoints()).permitAll() + .requestMatchers(getUserRoleEndpoints()).hasRole(USER.name()) .requestMatchers(HttpMethod.POST).hasAnyRole(USER.name(), ADMIN.name()) .requestMatchers(HttpMethod.PUT).hasAnyRole(USER.name(), ADMIN.name()) .requestMatchers(HttpMethod.DELETE).hasAnyRole(USER.name(), ADMIN.name()) - .requestMatchers(getUserRoleEndpoints()).hasRole(USER.name()) - .requestMatchers(getPublicEndpoints()).permitAll() - .requestMatchers(getBothEndpoints()).permitAll() .anyRequest().authenticated() ) From 34d14f40cef310275b81f3a7db24f84594ec1171 Mon Sep 17 00:00:00 2001 From: ohhamma Date: Fri, 11 Oct 2024 16:10:20 +0900 Subject: [PATCH 34/46] KL-184/fix: add static endpoint in public --- .../java/taco/klkl/global/config/security/SecurityEndpoint.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/taco/klkl/global/config/security/SecurityEndpoint.java b/src/main/java/taco/klkl/global/config/security/SecurityEndpoint.java index 06ff08a3..b3a9425b 100644 --- a/src/main/java/taco/klkl/global/config/security/SecurityEndpoint.java +++ b/src/main/java/taco/klkl/global/config/security/SecurityEndpoint.java @@ -15,6 +15,7 @@ public enum SecurityEndpoint { PUBLIC(new RequestMatcher[]{ new AntPathRequestMatcher("/"), + new AntPathRequestMatcher("/static/**"), new AntPathRequestMatcher("/login/**"), new AntPathRequestMatcher("/error"), new AntPathRequestMatcher("/favicon.ico"), From 8576de0268bb8faabdc09568fee6d738abc79ef1 Mon Sep 17 00:00:00 2001 From: ohhamma Date: Fri, 11 Oct 2024 16:18:08 +0900 Subject: [PATCH 35/46] KL-184/fix: change authorize endpoint sequence --- .../taco/klkl/global/config/security/SecurityConfig.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/taco/klkl/global/config/security/SecurityConfig.java b/src/main/java/taco/klkl/global/config/security/SecurityConfig.java index 0ec225c8..2b5eddb3 100644 --- a/src/main/java/taco/klkl/global/config/security/SecurityConfig.java +++ b/src/main/java/taco/klkl/global/config/security/SecurityConfig.java @@ -79,12 +79,12 @@ public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Excepti // request authentication & authorization .authorizeHttpRequests(authorizeRequests -> authorizeRequests - .requestMatchers(getPublicEndpoints()).permitAll() - .requestMatchers(getBothEndpoints()).permitAll() - .requestMatchers(getUserRoleEndpoints()).hasRole(USER.name()) .requestMatchers(HttpMethod.POST).hasAnyRole(USER.name(), ADMIN.name()) .requestMatchers(HttpMethod.PUT).hasAnyRole(USER.name(), ADMIN.name()) .requestMatchers(HttpMethod.DELETE).hasAnyRole(USER.name(), ADMIN.name()) + .requestMatchers(getUserRoleEndpoints()).hasRole(USER.name()) + .requestMatchers(getBothEndpoints()).permitAll() + .requestMatchers(getPublicEndpoints()).permitAll() .anyRequest().authenticated() ) From d374f5d1b4f725c5f4294f0be5e1bec13126c17b Mon Sep 17 00:00:00 2001 From: ohhamma Date: Fri, 11 Oct 2024 16:31:51 +0900 Subject: [PATCH 36/46] KL-184/fix: handle public endpoint error --- .../security/TokenAuthenticationFilter.java | 25 +++++++++++++------ 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/src/main/java/taco/klkl/global/config/security/TokenAuthenticationFilter.java b/src/main/java/taco/klkl/global/config/security/TokenAuthenticationFilter.java index 74713730..b21c7750 100644 --- a/src/main/java/taco/klkl/global/config/security/TokenAuthenticationFilter.java +++ b/src/main/java/taco/klkl/global/config/security/TokenAuthenticationFilter.java @@ -31,11 +31,8 @@ public class TokenAuthenticationFilter extends OncePerRequestFilter { @Override protected boolean shouldNotFilter(HttpServletRequest request) throws ServletException { - if ("GET".equalsIgnoreCase(request.getMethod())) { - return SecurityEndpoint.isPublicEndpoint(request) - && !SecurityEndpoint.isBothEndpoint(request); - } - return false; + return "GET".equalsIgnoreCase(request.getMethod()) + && SecurityEndpoint.isPublicEndpoint(request); } @Override @@ -45,9 +42,15 @@ protected void doFilterInternal( FilterChain filterChain ) throws ServletException, IOException { final String accessToken = tokenUtil.resolveToken(request); + final boolean isBothEndpoint = SecurityEndpoint.isBothEndpoint(request); + final boolean isGetRequest = "GET".equalsIgnoreCase(request.getMethod()); - if (accessToken == null && SecurityEndpoint.isBothEndpoint(request)) { - proceedWithoutAuthentication(request, response, filterChain); + if (!StringUtils.hasText(accessToken)) { + if (isBothEndpoint || isGetRequest) { + filterChain.doFilter(request, response); + return; + } + handleTokenException(request, response, filterChain, new UnauthorizedException()); return; } @@ -62,9 +65,17 @@ protected void doFilterInternal( } } } catch (TokenInvalidException | TokenExpiredException e) { + if (isBothEndpoint || isGetRequest) { + filterChain.doFilter(request, response); + return; + } handleTokenException(request, response, filterChain, e); return; } catch (Exception e) { + if (isBothEndpoint || isGetRequest) { + filterChain.doFilter(request, response); + return; + } handleTokenException(request, response, filterChain, new UnauthorizedException()); return; } From 425bef6646d20a946d4ea554d3cee0105817eed5 Mon Sep 17 00:00:00 2001 From: ohhamma Date: Fri, 11 Oct 2024 16:52:32 +0900 Subject: [PATCH 37/46] KL-184/fix: handle public token auth error --- .../klkl/domain/token/service/TokenProvider.java | 2 +- .../global/config/security/SecurityConfig.java | 6 +++--- .../security/TokenAuthenticationFilter.java | 16 +++------------- 3 files changed, 7 insertions(+), 17 deletions(-) diff --git a/src/main/java/taco/klkl/domain/token/service/TokenProvider.java b/src/main/java/taco/klkl/domain/token/service/TokenProvider.java index a2250a15..092cb97c 100644 --- a/src/main/java/taco/klkl/domain/token/service/TokenProvider.java +++ b/src/main/java/taco/klkl/domain/token/service/TokenProvider.java @@ -113,7 +113,7 @@ private List getAuthorities(final Claims claims) { public boolean validateToken(final String token) { if (!StringUtils.hasText(token)) { - return false; + throw new TokenInvalidException(); } try { Jwts.parser().verifyWith(secretKey).build().parseSignedClaims(token); diff --git a/src/main/java/taco/klkl/global/config/security/SecurityConfig.java b/src/main/java/taco/klkl/global/config/security/SecurityConfig.java index 2b5eddb3..0ec225c8 100644 --- a/src/main/java/taco/klkl/global/config/security/SecurityConfig.java +++ b/src/main/java/taco/klkl/global/config/security/SecurityConfig.java @@ -79,12 +79,12 @@ public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Excepti // request authentication & authorization .authorizeHttpRequests(authorizeRequests -> authorizeRequests + .requestMatchers(getPublicEndpoints()).permitAll() + .requestMatchers(getBothEndpoints()).permitAll() + .requestMatchers(getUserRoleEndpoints()).hasRole(USER.name()) .requestMatchers(HttpMethod.POST).hasAnyRole(USER.name(), ADMIN.name()) .requestMatchers(HttpMethod.PUT).hasAnyRole(USER.name(), ADMIN.name()) .requestMatchers(HttpMethod.DELETE).hasAnyRole(USER.name(), ADMIN.name()) - .requestMatchers(getUserRoleEndpoints()).hasRole(USER.name()) - .requestMatchers(getBothEndpoints()).permitAll() - .requestMatchers(getPublicEndpoints()).permitAll() .anyRequest().authenticated() ) diff --git a/src/main/java/taco/klkl/global/config/security/TokenAuthenticationFilter.java b/src/main/java/taco/klkl/global/config/security/TokenAuthenticationFilter.java index b21c7750..da67ea78 100644 --- a/src/main/java/taco/klkl/global/config/security/TokenAuthenticationFilter.java +++ b/src/main/java/taco/klkl/global/config/security/TokenAuthenticationFilter.java @@ -46,12 +46,10 @@ protected void doFilterInternal( final boolean isGetRequest = "GET".equalsIgnoreCase(request.getMethod()); if (!StringUtils.hasText(accessToken)) { - if (isBothEndpoint || isGetRequest) { - filterChain.doFilter(request, response); + if (isGetRequest && isBothEndpoint) { + proceedWithoutAuthentication(request, response, filterChain); return; } - handleTokenException(request, response, filterChain, new UnauthorizedException()); - return; } try { @@ -65,17 +63,9 @@ protected void doFilterInternal( } } } catch (TokenInvalidException | TokenExpiredException e) { - if (isBothEndpoint || isGetRequest) { - filterChain.doFilter(request, response); - return; - } handleTokenException(request, response, filterChain, e); return; } catch (Exception e) { - if (isBothEndpoint || isGetRequest) { - filterChain.doFilter(request, response); - return; - } handleTokenException(request, response, filterChain, new UnauthorizedException()); return; } @@ -95,7 +85,7 @@ private void handleTokenException( CustomException ex ) throws IOException, ServletException { SecurityContextHolder.clearContext(); - if (SecurityEndpoint.isBothEndpoint(request)) { + if ("GET".equalsIgnoreCase(request.getMethod()) && SecurityEndpoint.isBothEndpoint(request)) { proceedWithoutAuthentication(request, response, filterChain); } else { responseUtil.sendErrorResponse(response, ex); From 58ddcef4ec4a564612af6d25982c06b19da51911 Mon Sep 17 00:00:00 2001 From: ohhamma Date: Fri, 11 Oct 2024 17:05:23 +0900 Subject: [PATCH 38/46] KL-184/fix: change security authorization sequence --- .../global/config/security/SecurityConfig.java | 18 +++++++++--------- .../config/security/SecurityEndpoint.java | 7 +++++++ 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/main/java/taco/klkl/global/config/security/SecurityConfig.java b/src/main/java/taco/klkl/global/config/security/SecurityConfig.java index 0ec225c8..e60e76ac 100644 --- a/src/main/java/taco/klkl/global/config/security/SecurityConfig.java +++ b/src/main/java/taco/klkl/global/config/security/SecurityConfig.java @@ -79,12 +79,12 @@ public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Excepti // request authentication & authorization .authorizeHttpRequests(authorizeRequests -> authorizeRequests - .requestMatchers(getPublicEndpoints()).permitAll() - .requestMatchers(getBothEndpoints()).permitAll() - .requestMatchers(getUserRoleEndpoints()).hasRole(USER.name()) .requestMatchers(HttpMethod.POST).hasAnyRole(USER.name(), ADMIN.name()) .requestMatchers(HttpMethod.PUT).hasAnyRole(USER.name(), ADMIN.name()) .requestMatchers(HttpMethod.DELETE).hasAnyRole(USER.name(), ADMIN.name()) + .requestMatchers(HttpMethod.GET, getUserRoleEndpoints()).hasRole(USER.name()) + .requestMatchers(HttpMethod.GET, getBothEndpoints()).permitAll() + .requestMatchers(HttpMethod.GET, getPublicEndpoints()).permitAll() .anyRequest().authenticated() ) @@ -120,16 +120,16 @@ public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Excepti return httpSecurity.build(); } - private RequestMatcher[] getPublicEndpoints() { - return SecurityEndpoint.PUBLIC.getMatchers(); + private String[] getPublicEndpoints() { + return SecurityEndpoint.PUBLIC.getPatterns(); } - private RequestMatcher[] getUserRoleEndpoints() { - return SecurityEndpoint.USER_ROLE.getMatchers(); + private String[] getUserRoleEndpoints() { + return SecurityEndpoint.USER_ROLE.getPatterns(); } - private RequestMatcher[] getBothEndpoints() { - return SecurityEndpoint.BOTH.getMatchers(); + private String[] getBothEndpoints() { + return SecurityEndpoint.BOTH.getPatterns(); } @Bean diff --git a/src/main/java/taco/klkl/global/config/security/SecurityEndpoint.java b/src/main/java/taco/klkl/global/config/security/SecurityEndpoint.java index b3a9425b..2e72f657 100644 --- a/src/main/java/taco/klkl/global/config/security/SecurityEndpoint.java +++ b/src/main/java/taco/klkl/global/config/security/SecurityEndpoint.java @@ -1,6 +1,7 @@ package taco.klkl.global.config.security; import java.util.Arrays; +import java.util.stream.Stream; import org.springframework.security.web.util.matcher.AntPathRequestMatcher; import org.springframework.security.web.util.matcher.RequestMatcher; @@ -54,6 +55,12 @@ public enum SecurityEndpoint { private final RequestMatcher[] matchers; + public String[] getPatterns() { + return Stream.of(matchers) + .map(matcher -> ((AntPathRequestMatcher) matcher).getPattern()) + .toArray(String[]::new); + } + public static boolean isBothEndpoint(HttpServletRequest request) { return Arrays.stream(BOTH.getMatchers()) .anyMatch(matcher -> matcher.matches(request)); From fe00120872743a6e25efeae808b7db6ca73bc51f Mon Sep 17 00:00:00 2001 From: ohhamma Date: Fri, 11 Oct 2024 17:06:03 +0900 Subject: [PATCH 39/46] KL-184/refactor: add methods to simplify code --- .../security/TokenAuthenticationFilter.java | 65 +++++++++++-------- 1 file changed, 37 insertions(+), 28 deletions(-) diff --git a/src/main/java/taco/klkl/global/config/security/TokenAuthenticationFilter.java b/src/main/java/taco/klkl/global/config/security/TokenAuthenticationFilter.java index da67ea78..31458ff9 100644 --- a/src/main/java/taco/klkl/global/config/security/TokenAuthenticationFilter.java +++ b/src/main/java/taco/klkl/global/config/security/TokenAuthenticationFilter.java @@ -2,6 +2,7 @@ import java.io.IOException; +import org.springframework.http.HttpMethod; import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Component; @@ -31,7 +32,7 @@ public class TokenAuthenticationFilter extends OncePerRequestFilter { @Override protected boolean shouldNotFilter(HttpServletRequest request) throws ServletException { - return "GET".equalsIgnoreCase(request.getMethod()) + return HttpMethod.GET.matches(request.getMethod()) && SecurityEndpoint.isPublicEndpoint(request); } @@ -43,25 +44,20 @@ protected void doFilterInternal( ) throws ServletException, IOException { final String accessToken = tokenUtil.resolveToken(request); final boolean isBothEndpoint = SecurityEndpoint.isBothEndpoint(request); - final boolean isGetRequest = "GET".equalsIgnoreCase(request.getMethod()); + final boolean isGetRequest = HttpMethod.GET.matches(request.getMethod()); + + if (isGetRequest && isBothEndpoint) { + processBothEndpoint(accessToken, request, response, filterChain); + return; + } if (!StringUtils.hasText(accessToken)) { - if (isGetRequest && isBothEndpoint) { - proceedWithoutAuthentication(request, response, filterChain); - return; - } + handleTokenException(request, response, filterChain, new UnauthorizedException()); + return; } try { - if (tokenProvider.validateToken(accessToken)) { - setAuthentication(accessToken); - } else { - final String reissueAccessToken = tokenProvider.reissueAccessToken(accessToken); - if (StringUtils.hasText(reissueAccessToken)) { - setAuthentication(reissueAccessToken); - tokenUtil.addAccessTokenCookie(response, reissueAccessToken); - } - } + validateAndSetAuthentication(accessToken, response); } catch (TokenInvalidException | TokenExpiredException e) { handleTokenException(request, response, filterChain, e); return; @@ -73,6 +69,31 @@ protected void doFilterInternal( filterChain.doFilter(request, response); } + private void processBothEndpoint(String accessToken, HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { + if (StringUtils.hasText(accessToken)) { + try { + validateAndSetAuthentication(accessToken, response); + } catch (Exception e) { + // For BOTH endpoints, we proceed even if token is invalid + } + } + filterChain.doFilter(request, response); + } + + private void validateAndSetAuthentication(String accessToken, HttpServletResponse response) throws TokenInvalidException, TokenExpiredException { + if (tokenProvider.validateToken(accessToken)) { + setAuthentication(accessToken); + } else { + final String reissueAccessToken = tokenProvider.reissueAccessToken(accessToken); + if (StringUtils.hasText(reissueAccessToken)) { + setAuthentication(reissueAccessToken); + tokenUtil.addAccessTokenCookie(response, reissueAccessToken); + } else { + throw new TokenInvalidException(); + } + } + } + private void setAuthentication(final String accessToken) { Authentication authentication = tokenProvider.getAuthentication(accessToken); SecurityContextHolder.getContext().setAuthentication(authentication); @@ -85,18 +106,6 @@ private void handleTokenException( CustomException ex ) throws IOException, ServletException { SecurityContextHolder.clearContext(); - if ("GET".equalsIgnoreCase(request.getMethod()) && SecurityEndpoint.isBothEndpoint(request)) { - proceedWithoutAuthentication(request, response, filterChain); - } else { - responseUtil.sendErrorResponse(response, ex); - } - } - - private void proceedWithoutAuthentication( - HttpServletRequest request, - HttpServletResponse response, - FilterChain filterChain - ) throws IOException, ServletException { - filterChain.doFilter(request, response); + responseUtil.sendErrorResponse(response, ex); } } From cd4efb68eccff8af521fc5d0dff193c02d6fa3e6 Mon Sep 17 00:00:00 2001 From: ohhamma Date: Fri, 11 Oct 2024 17:10:03 +0900 Subject: [PATCH 40/46] KL-184/fix: fix checkstyle errors --- .../config/security/TokenAuthenticationFilter.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/main/java/taco/klkl/global/config/security/TokenAuthenticationFilter.java b/src/main/java/taco/klkl/global/config/security/TokenAuthenticationFilter.java index 31458ff9..4f182a1f 100644 --- a/src/main/java/taco/klkl/global/config/security/TokenAuthenticationFilter.java +++ b/src/main/java/taco/klkl/global/config/security/TokenAuthenticationFilter.java @@ -69,7 +69,12 @@ protected void doFilterInternal( filterChain.doFilter(request, response); } - private void processBothEndpoint(String accessToken, HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { + private void processBothEndpoint( + String accessToken, + HttpServletRequest request, + HttpServletResponse response, + FilterChain filterChain + ) throws ServletException, IOException { if (StringUtils.hasText(accessToken)) { try { validateAndSetAuthentication(accessToken, response); @@ -80,7 +85,10 @@ private void processBothEndpoint(String accessToken, HttpServletRequest request, filterChain.doFilter(request, response); } - private void validateAndSetAuthentication(String accessToken, HttpServletResponse response) throws TokenInvalidException, TokenExpiredException { + private void validateAndSetAuthentication( + String accessToken, + HttpServletResponse response + ) throws TokenInvalidException, TokenExpiredException { if (tokenProvider.validateToken(accessToken)) { setAuthentication(accessToken); } else { From 1e40ee3b374a856e0cb1c781766f0998b3f0d94e Mon Sep 17 00:00:00 2001 From: ohhamma Date: Fri, 11 Oct 2024 17:25:47 +0900 Subject: [PATCH 41/46] KL-184/fix: handle authentication error --- .../config/security/SecurityConfig.java | 18 ++--- .../config/security/SecurityEndpoint.java | 8 --- .../security/TokenAuthenticationFilter.java | 66 ++++++------------- 3 files changed, 30 insertions(+), 62 deletions(-) diff --git a/src/main/java/taco/klkl/global/config/security/SecurityConfig.java b/src/main/java/taco/klkl/global/config/security/SecurityConfig.java index e60e76ac..2b5eddb3 100644 --- a/src/main/java/taco/klkl/global/config/security/SecurityConfig.java +++ b/src/main/java/taco/klkl/global/config/security/SecurityConfig.java @@ -82,9 +82,9 @@ public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Excepti .requestMatchers(HttpMethod.POST).hasAnyRole(USER.name(), ADMIN.name()) .requestMatchers(HttpMethod.PUT).hasAnyRole(USER.name(), ADMIN.name()) .requestMatchers(HttpMethod.DELETE).hasAnyRole(USER.name(), ADMIN.name()) - .requestMatchers(HttpMethod.GET, getUserRoleEndpoints()).hasRole(USER.name()) - .requestMatchers(HttpMethod.GET, getBothEndpoints()).permitAll() - .requestMatchers(HttpMethod.GET, getPublicEndpoints()).permitAll() + .requestMatchers(getUserRoleEndpoints()).hasRole(USER.name()) + .requestMatchers(getBothEndpoints()).permitAll() + .requestMatchers(getPublicEndpoints()).permitAll() .anyRequest().authenticated() ) @@ -120,16 +120,16 @@ public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Excepti return httpSecurity.build(); } - private String[] getPublicEndpoints() { - return SecurityEndpoint.PUBLIC.getPatterns(); + private RequestMatcher[] getPublicEndpoints() { + return SecurityEndpoint.PUBLIC.getMatchers(); } - private String[] getUserRoleEndpoints() { - return SecurityEndpoint.USER_ROLE.getPatterns(); + private RequestMatcher[] getUserRoleEndpoints() { + return SecurityEndpoint.USER_ROLE.getMatchers(); } - private String[] getBothEndpoints() { - return SecurityEndpoint.BOTH.getPatterns(); + private RequestMatcher[] getBothEndpoints() { + return SecurityEndpoint.BOTH.getMatchers(); } @Bean diff --git a/src/main/java/taco/klkl/global/config/security/SecurityEndpoint.java b/src/main/java/taco/klkl/global/config/security/SecurityEndpoint.java index 2e72f657..bbea7306 100644 --- a/src/main/java/taco/klkl/global/config/security/SecurityEndpoint.java +++ b/src/main/java/taco/klkl/global/config/security/SecurityEndpoint.java @@ -1,7 +1,6 @@ package taco.klkl.global.config.security; import java.util.Arrays; -import java.util.stream.Stream; import org.springframework.security.web.util.matcher.AntPathRequestMatcher; import org.springframework.security.web.util.matcher.RequestMatcher; @@ -10,7 +9,6 @@ import lombok.Getter; import lombok.RequiredArgsConstructor; - @Getter @RequiredArgsConstructor public enum SecurityEndpoint { @@ -55,12 +53,6 @@ public enum SecurityEndpoint { private final RequestMatcher[] matchers; - public String[] getPatterns() { - return Stream.of(matchers) - .map(matcher -> ((AntPathRequestMatcher) matcher).getPattern()) - .toArray(String[]::new); - } - public static boolean isBothEndpoint(HttpServletRequest request) { return Arrays.stream(BOTH.getMatchers()) .anyMatch(matcher -> matcher.matches(request)); diff --git a/src/main/java/taco/klkl/global/config/security/TokenAuthenticationFilter.java b/src/main/java/taco/klkl/global/config/security/TokenAuthenticationFilter.java index 4f182a1f..3e219857 100644 --- a/src/main/java/taco/klkl/global/config/security/TokenAuthenticationFilter.java +++ b/src/main/java/taco/klkl/global/config/security/TokenAuthenticationFilter.java @@ -43,21 +43,15 @@ protected void doFilterInternal( FilterChain filterChain ) throws ServletException, IOException { final String accessToken = tokenUtil.resolveToken(request); - final boolean isBothEndpoint = SecurityEndpoint.isBothEndpoint(request); - final boolean isGetRequest = HttpMethod.GET.matches(request.getMethod()); - - if (isGetRequest && isBothEndpoint) { - processBothEndpoint(accessToken, request, response, filterChain); - return; - } - - if (!StringUtils.hasText(accessToken)) { - handleTokenException(request, response, filterChain, new UnauthorizedException()); - return; - } try { - validateAndSetAuthentication(accessToken, response); + if (tokenProvider.validateToken(accessToken)) { + setAuthentication(accessToken); + } else { + final String reissueAccessToken = tokenProvider.reissueAccessToken(accessToken); + setAuthentication(reissueAccessToken); + tokenUtil.addAccessTokenCookie(response, reissueAccessToken); + } } catch (TokenInvalidException | TokenExpiredException e) { handleTokenException(request, response, filterChain, e); return; @@ -69,40 +63,10 @@ protected void doFilterInternal( filterChain.doFilter(request, response); } - private void processBothEndpoint( - String accessToken, - HttpServletRequest request, - HttpServletResponse response, - FilterChain filterChain - ) throws ServletException, IOException { + private void setAuthentication(final String accessToken) { if (StringUtils.hasText(accessToken)) { - try { - validateAndSetAuthentication(accessToken, response); - } catch (Exception e) { - // For BOTH endpoints, we proceed even if token is invalid - } + throw new TokenInvalidException(); } - filterChain.doFilter(request, response); - } - - private void validateAndSetAuthentication( - String accessToken, - HttpServletResponse response - ) throws TokenInvalidException, TokenExpiredException { - if (tokenProvider.validateToken(accessToken)) { - setAuthentication(accessToken); - } else { - final String reissueAccessToken = tokenProvider.reissueAccessToken(accessToken); - if (StringUtils.hasText(reissueAccessToken)) { - setAuthentication(reissueAccessToken); - tokenUtil.addAccessTokenCookie(response, reissueAccessToken); - } else { - throw new TokenInvalidException(); - } - } - } - - private void setAuthentication(final String accessToken) { Authentication authentication = tokenProvider.getAuthentication(accessToken); SecurityContextHolder.getContext().setAuthentication(authentication); } @@ -113,7 +77,19 @@ private void handleTokenException( FilterChain filterChain, CustomException ex ) throws IOException, ServletException { + if (HttpMethod.GET.matches(request.getMethod()) && SecurityEndpoint.isBothEndpoint(request)) { + proceedWithoutAuthentication(request, response, filterChain); + return; + } SecurityContextHolder.clearContext(); responseUtil.sendErrorResponse(response, ex); } + + private void proceedWithoutAuthentication( + HttpServletRequest request, + HttpServletResponse response, + FilterChain filterChain + ) throws IOException, ServletException { + filterChain.doFilter(request, response); + } } From 6662a2272ab9f9dda28e763d8ed2d37f82326b8f Mon Sep 17 00:00:00 2001 From: ohhamma Date: Fri, 11 Oct 2024 17:31:51 +0900 Subject: [PATCH 42/46] KL-184/fix: handle token auth error --- .../config/security/TokenAuthenticationFilter.java | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/main/java/taco/klkl/global/config/security/TokenAuthenticationFilter.java b/src/main/java/taco/klkl/global/config/security/TokenAuthenticationFilter.java index 3e219857..d1f1ac79 100644 --- a/src/main/java/taco/klkl/global/config/security/TokenAuthenticationFilter.java +++ b/src/main/java/taco/klkl/global/config/security/TokenAuthenticationFilter.java @@ -44,6 +44,13 @@ protected void doFilterInternal( ) throws ServletException, IOException { final String accessToken = tokenUtil.resolveToken(request); + if (!StringUtils.hasText(accessToken)) { + if (HttpMethod.GET.matches(request.getMethod()) && SecurityEndpoint.isBothEndpoint(request)) { + proceedWithoutAuthentication(request, response, filterChain); + return; + } + } + try { if (tokenProvider.validateToken(accessToken)) { setAuthentication(accessToken); @@ -77,10 +84,6 @@ private void handleTokenException( FilterChain filterChain, CustomException ex ) throws IOException, ServletException { - if (HttpMethod.GET.matches(request.getMethod()) && SecurityEndpoint.isBothEndpoint(request)) { - proceedWithoutAuthentication(request, response, filterChain); - return; - } SecurityContextHolder.clearContext(); responseUtil.sendErrorResponse(response, ex); } From 57e4a67e87954ea5b2fc19000ab60c54c47acc8d Mon Sep 17 00:00:00 2001 From: ohhamma Date: Fri, 11 Oct 2024 17:41:53 +0900 Subject: [PATCH 43/46] KL-184/fix: handle token auth error --- .../java/taco/klkl/global/config/security/SecurityConfig.java | 1 + .../klkl/global/config/security/TokenAuthenticationFilter.java | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/taco/klkl/global/config/security/SecurityConfig.java b/src/main/java/taco/klkl/global/config/security/SecurityConfig.java index 2b5eddb3..94b3fd2f 100644 --- a/src/main/java/taco/klkl/global/config/security/SecurityConfig.java +++ b/src/main/java/taco/klkl/global/config/security/SecurityConfig.java @@ -79,6 +79,7 @@ public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Excepti // request authentication & authorization .authorizeHttpRequests(authorizeRequests -> authorizeRequests + .requestMatchers(HttpMethod.GET, "/").permitAll() .requestMatchers(HttpMethod.POST).hasAnyRole(USER.name(), ADMIN.name()) .requestMatchers(HttpMethod.PUT).hasAnyRole(USER.name(), ADMIN.name()) .requestMatchers(HttpMethod.DELETE).hasAnyRole(USER.name(), ADMIN.name()) diff --git a/src/main/java/taco/klkl/global/config/security/TokenAuthenticationFilter.java b/src/main/java/taco/klkl/global/config/security/TokenAuthenticationFilter.java index d1f1ac79..02d0d5ea 100644 --- a/src/main/java/taco/klkl/global/config/security/TokenAuthenticationFilter.java +++ b/src/main/java/taco/klkl/global/config/security/TokenAuthenticationFilter.java @@ -71,7 +71,7 @@ protected void doFilterInternal( } private void setAuthentication(final String accessToken) { - if (StringUtils.hasText(accessToken)) { + if (!StringUtils.hasText(accessToken)) { throw new TokenInvalidException(); } Authentication authentication = tokenProvider.getAuthentication(accessToken); From 3ac7bf602747515ea1291bb8deef13f49e7a12b1 Mon Sep 17 00:00:00 2001 From: ohhamma Date: Fri, 11 Oct 2024 17:52:19 +0900 Subject: [PATCH 44/46] KL-184/feat: add health check endpoint --- .../global/config/security/SecurityEndpoint.java | 4 +++- .../global/controller/HealthCheckController.java | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 src/main/java/taco/klkl/global/controller/HealthCheckController.java diff --git a/src/main/java/taco/klkl/global/config/security/SecurityEndpoint.java b/src/main/java/taco/klkl/global/config/security/SecurityEndpoint.java index bbea7306..dbacff9f 100644 --- a/src/main/java/taco/klkl/global/config/security/SecurityEndpoint.java +++ b/src/main/java/taco/klkl/global/config/security/SecurityEndpoint.java @@ -14,11 +14,13 @@ public enum SecurityEndpoint { PUBLIC(new RequestMatcher[]{ new AntPathRequestMatcher("/"), - new AntPathRequestMatcher("/static/**"), new AntPathRequestMatcher("/login/**"), new AntPathRequestMatcher("/error"), new AntPathRequestMatcher("/favicon.ico"), + // health check + new AntPathRequestMatcher("/health"), + // swagger new AntPathRequestMatcher("/swagger-ui/**"), new AntPathRequestMatcher("/swagger-ui.html"), diff --git a/src/main/java/taco/klkl/global/controller/HealthCheckController.java b/src/main/java/taco/klkl/global/controller/HealthCheckController.java new file mode 100644 index 00000000..f9ac5f1c --- /dev/null +++ b/src/main/java/taco/klkl/global/controller/HealthCheckController.java @@ -0,0 +1,14 @@ +package taco.klkl.global.controller; + +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class HealthCheckController { + + @GetMapping("/health") + public ResponseEntity healthCheck() { + return ResponseEntity.ok("OK"); + } +} \ No newline at end of file From 626f7bc46387e4ea740d76f5097564de5529c805 Mon Sep 17 00:00:00 2001 From: ohhamma Date: Fri, 11 Oct 2024 17:52:33 +0900 Subject: [PATCH 45/46] KL-184/chore: remove unnecessary endpoint --- .../java/taco/klkl/global/config/security/SecurityConfig.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/taco/klkl/global/config/security/SecurityConfig.java b/src/main/java/taco/klkl/global/config/security/SecurityConfig.java index 94b3fd2f..2b5eddb3 100644 --- a/src/main/java/taco/klkl/global/config/security/SecurityConfig.java +++ b/src/main/java/taco/klkl/global/config/security/SecurityConfig.java @@ -79,7 +79,6 @@ public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Excepti // request authentication & authorization .authorizeHttpRequests(authorizeRequests -> authorizeRequests - .requestMatchers(HttpMethod.GET, "/").permitAll() .requestMatchers(HttpMethod.POST).hasAnyRole(USER.name(), ADMIN.name()) .requestMatchers(HttpMethod.PUT).hasAnyRole(USER.name(), ADMIN.name()) .requestMatchers(HttpMethod.DELETE).hasAnyRole(USER.name(), ADMIN.name()) From f51cec278bbb00599642f0e27cc87c86c1445e94 Mon Sep 17 00:00:00 2001 From: ohhamma Date: Fri, 11 Oct 2024 17:58:05 +0900 Subject: [PATCH 46/46] KL-184/fix: fix checkstyle error --- .../java/taco/klkl/global/controller/HealthCheckController.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/taco/klkl/global/controller/HealthCheckController.java b/src/main/java/taco/klkl/global/controller/HealthCheckController.java index f9ac5f1c..ffc1f380 100644 --- a/src/main/java/taco/klkl/global/controller/HealthCheckController.java +++ b/src/main/java/taco/klkl/global/controller/HealthCheckController.java @@ -11,4 +11,4 @@ public class HealthCheckController { public ResponseEntity healthCheck() { return ResponseEntity.ok("OK"); } -} \ No newline at end of file +}