From 23c1d220b03faa5ebab75f91db456b88cd4bfb66 Mon Sep 17 00:00:00 2001 From: junwon <67488973+wjdwnsdnjs13@users.noreply.github.com> Date: Fri, 19 Jul 2024 21:34:20 +0900 Subject: [PATCH] =?UTF-8?q?[NO=5FJIRA]=20JwtTokenResponse=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80=20(#57)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat : JwtTokenResponse 구현 * refactor : 1년 동안 유요한 토큰 발급 * refactor : Jwt 토큰을 바디에 리턴하도록 변경 --- .../spot/application/common/jwt/JwtTokenUtil.java | 12 ++++-------- .../member/controller/MemberController.java | 10 +++++----- .../member/dto/response/JwtTokenResponse.java | 3 +++ .../member/dto/response/MemberResponse.java | 10 ---------- 4 files changed, 12 insertions(+), 23 deletions(-) create mode 100644 application/src/main/java/org/depromeet/spot/application/member/dto/response/JwtTokenResponse.java delete mode 100644 application/src/main/java/org/depromeet/spot/application/member/dto/response/MemberResponse.java diff --git a/application/src/main/java/org/depromeet/spot/application/common/jwt/JwtTokenUtil.java b/application/src/main/java/org/depromeet/spot/application/common/jwt/JwtTokenUtil.java index da785949..f6ba7ef0 100644 --- a/application/src/main/java/org/depromeet/spot/application/common/jwt/JwtTokenUtil.java +++ b/application/src/main/java/org/depromeet/spot/application/common/jwt/JwtTokenUtil.java @@ -10,7 +10,6 @@ import org.depromeet.spot.domain.member.Member; import org.depromeet.spot.domain.member.enums.MemberRole; import org.springframework.beans.factory.annotation.Value; -import org.springframework.http.HttpHeaders; import org.springframework.stereotype.Component; import io.jsonwebtoken.Claims; @@ -34,15 +33,11 @@ public class JwtTokenUtil { @Value("${spring.jwt.secret}") private String SECRETKEY; - public HttpHeaders getJWTToken(Member member) { + public String getJWTToken(Member member) { // TODO 토큰 구현하기. // jwt 토큰 생성 - String token = generateToken(member.getId(), member.getRole()); - - HttpHeaders headers = new HttpHeaders(); - headers.set(HttpHeaders.AUTHORIZATION, "Bearer " + token); - return headers; + return generateToken(member.getId(), member.getRole()); } public String generateToken(Long memberId, MemberRole memberRole) { @@ -53,7 +48,8 @@ public String generateToken(Long memberId, MemberRole memberRole) { .setIssuedAt(new Date(System.currentTimeMillis())) .setExpiration( new Date( - System.currentTimeMillis() + 1000 * 60 * 60 * 24 * 30L)) // 토큰 만료 시간 + System.currentTimeMillis() + + 1000 * 60 * 60 * 24 * 30 * 12L)) // 토큰 만료 시간 .signWith(SignatureAlgorithm.HS256, SECRETKEY.getBytes()) .compact(); } diff --git a/application/src/main/java/org/depromeet/spot/application/member/controller/MemberController.java b/application/src/main/java/org/depromeet/spot/application/member/controller/MemberController.java index 1e22cec7..9829b95c 100644 --- a/application/src/main/java/org/depromeet/spot/application/member/controller/MemberController.java +++ b/application/src/main/java/org/depromeet/spot/application/member/controller/MemberController.java @@ -4,9 +4,9 @@ import org.depromeet.spot.application.common.jwt.JwtTokenUtil; import org.depromeet.spot.application.member.dto.request.RegisterReq; +import org.depromeet.spot.application.member.dto.response.JwtTokenResponse; import org.depromeet.spot.domain.member.Member; import org.depromeet.spot.usecase.port.in.member.MemberUsecase; -import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; @@ -36,25 +36,25 @@ public class MemberController { @PostMapping @ResponseStatus(HttpStatus.CREATED) @Operation(summary = "Member 회원가입 API") - public HttpHeaders create(@RequestBody @Valid RegisterReq request) { + public JwtTokenResponse create(@RequestBody @Valid RegisterReq request) { Member member = request.toDomain(); Member memberResult = memberUsecase.create(member); - return jwtTokenUtil.getJWTToken(memberResult); + return new JwtTokenResponse(jwtTokenUtil.getJWTToken(memberResult)); } @GetMapping("/{idCode}") @ResponseStatus(HttpStatus.OK) @Operation(summary = "Member 로그인 API") - public HttpHeaders login( + public JwtTokenResponse login( @PathVariable("idCode") @Parameter(name = "idCode", description = "sns idCode", required = true) String idCode) { Member member = memberUsecase.login(idCode); - return jwtTokenUtil.getJWTToken(member); + return new JwtTokenResponse(jwtTokenUtil.getJWTToken(member)); } @GetMapping("/duplicatedNickname/{nickname}") diff --git a/application/src/main/java/org/depromeet/spot/application/member/dto/response/JwtTokenResponse.java b/application/src/main/java/org/depromeet/spot/application/member/dto/response/JwtTokenResponse.java new file mode 100644 index 00000000..0efc4eac --- /dev/null +++ b/application/src/main/java/org/depromeet/spot/application/member/dto/response/JwtTokenResponse.java @@ -0,0 +1,3 @@ +package org.depromeet.spot.application.member.dto.response; + +public record JwtTokenResponse(String jwtToken) {} diff --git a/application/src/main/java/org/depromeet/spot/application/member/dto/response/MemberResponse.java b/application/src/main/java/org/depromeet/spot/application/member/dto/response/MemberResponse.java deleted file mode 100644 index 06ab8203..00000000 --- a/application/src/main/java/org/depromeet/spot/application/member/dto/response/MemberResponse.java +++ /dev/null @@ -1,10 +0,0 @@ -package org.depromeet.spot.application.member.dto.response; - -import org.depromeet.spot.domain.member.Member; - -public record MemberResponse(Long id, String name) { - - public static MemberResponse from(Member member) { - return new MemberResponse(member.getId(), member.getName()); - } -}