From 85b7ff62de35243ca6d69714741f209fbf226fc3 Mon Sep 17 00:00:00 2001 From: Doeun Date: Mon, 26 Aug 2024 14:58:41 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20claim=20key=20=EC=83=81=EC=88=98?= =?UTF-8?q?=ED=99=94,=20test=20yml=EC=97=90=20jwt=20=EC=84=A4=EC=A0=95=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80=20(#23)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../module/auth/jwt/JwtUtil.java | 8 +++-- src/test/resources/application-test.yml | 32 ------------------- src/test/resources/application.yml | 14 ++++++++ 3 files changed, 19 insertions(+), 35 deletions(-) delete mode 100644 src/test/resources/application-test.yml create mode 100644 src/test/resources/application.yml diff --git a/src/main/java/team05/integrated_feed_backend/module/auth/jwt/JwtUtil.java b/src/main/java/team05/integrated_feed_backend/module/auth/jwt/JwtUtil.java index 0f2129f..a2364ed 100644 --- a/src/main/java/team05/integrated_feed_backend/module/auth/jwt/JwtUtil.java +++ b/src/main/java/team05/integrated_feed_backend/module/auth/jwt/JwtUtil.java @@ -22,6 +22,8 @@ @Slf4j @Component public class JwtUtil { + private static final String CLAIM_MEMBER_ID = "memberId"; + private static final String CLAIM_ACCOUNT = "account"; private final Key secretKey; @@ -42,8 +44,8 @@ public String generateToken(Authentication authentication) { // JWT 토큰에 포함되는 정보(페이로드): jwt의 주체 memberId로 Claims claims = Jwts.claims().setSubject(String.valueOf(memberId)); - claims.put("memberId", memberId); - claims.put("account", account); + claims.put(CLAIM_MEMBER_ID, memberId); + claims.put(CLAIM_ACCOUNT, account); Date now = new Date(); Date validity = new Date(now.getTime() + tokenValidityInseconds * 1000); @@ -91,7 +93,7 @@ public String getAccount(String token) { .build() .parseClaimsJws(token) .getBody() - .get("account", String.class); + .get(CLAIM_ACCOUNT, String.class); } // JWT 토큰 유효성 검증 diff --git a/src/test/resources/application-test.yml b/src/test/resources/application-test.yml deleted file mode 100644 index afd6eb1..0000000 --- a/src/test/resources/application-test.yml +++ /dev/null @@ -1,32 +0,0 @@ -spring: - application: - name: integrated-feed-backend - sql: - init: - mode: always # data.sql - - datasource: - url: ${POSTGRES_DB_URL} - username: ${POSTGRES_DB_USERNAME} - password: ${POSTGRES_DB_PASSWORD} - driver-class-name: org.postgresql.Driver - - jpa: - defer-datasource-initialization: true # data.sql - properties: - hibernate: - dialect: org.hibernate.dialect.PostgreSQLDialect - show-sql: true - hibernate: - ddl-auto: validate - -springdoc: - default-consumes-media-type: application/json - default-produces-media-type: application/json - swagger-ui: - tags-sorter: alpha - operations-sorter: method - -jwt: - secret: ${JWT_SECRET} # secret key, 만료 시간 환경 변수로 관리 - token-validity-in-seconds: ${JWT_EXPIRATION} \ No newline at end of file diff --git a/src/test/resources/application.yml b/src/test/resources/application.yml new file mode 100644 index 0000000..6f15269 --- /dev/null +++ b/src/test/resources/application.yml @@ -0,0 +1,14 @@ +spring: + datasource: + url: jdbc:h2:mem:testdb;MODE=PostgreSQL + username: sa + driver-class-name: org.h2.Driver + + jpa: + show-sql: true + hibernate: + ddl-auto: create-drop + +jwt: + secret: dGVzdC1zZWNyZXQta2V5 + token-validity-in-seconds: 3600 \ No newline at end of file