From 610d4fe9c6112cca07e2613881e5df16126e88d6 Mon Sep 17 00:00:00 2001 From: tlarbals824 Date: Wed, 28 Feb 2024 14:47:28 +0900 Subject: [PATCH 1/4] =?UTF-8?q?feat=20:=20=ED=86=A0=ED=81=B0=20=EC=83=9D?= =?UTF-8?q?=EC=84=B1=20=EB=B0=8F=20=ED=8C=8C=EC=8B=B1=20=EC=9C=A0=ED=8B=B8?= =?UTF-8?q?=20=ED=81=B4=EB=9E=98=EC=8A=A4=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...Factory.java => BearerTokenGenerator.java} | 12 +++++--- .../java/core/kobaco/global/jwt/JwtUtils.java | 28 +++++++++++++++++++ 2 files changed, 36 insertions(+), 4 deletions(-) rename kobaco/kobaco/src/main/java/core/kobaco/global/jwt/{JwtFactory.java => BearerTokenGenerator.java} (53%) create mode 100644 kobaco/kobaco/src/main/java/core/kobaco/global/jwt/JwtUtils.java diff --git a/kobaco/kobaco/src/main/java/core/kobaco/global/jwt/JwtFactory.java b/kobaco/kobaco/src/main/java/core/kobaco/global/jwt/BearerTokenGenerator.java similarity index 53% rename from kobaco/kobaco/src/main/java/core/kobaco/global/jwt/JwtFactory.java rename to kobaco/kobaco/src/main/java/core/kobaco/global/jwt/BearerTokenGenerator.java index 84f129c..8eddbc3 100644 --- a/kobaco/kobaco/src/main/java/core/kobaco/global/jwt/JwtFactory.java +++ b/kobaco/kobaco/src/main/java/core/kobaco/global/jwt/BearerTokenGenerator.java @@ -2,14 +2,18 @@ import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Component; -import org.springframework.web.bind.annotation.RequestMapping; @Component @RequiredArgsConstructor -public class JwtFactory { +public class BearerTokenGenerator { private final JwtProvider jwtProvider; - public String generateAccessToken(PrivateClaims.UserClaims userClaims){ + public String generateAccessTokenAndAttachBearer(PrivateClaims.UserClaims userClaims){ return JwtHeaderConsts.BEARER_PREFIX + jwtProvider.generateAccessToken(userClaims); } -} + + public String detachBearerPrefix(String token){ + return token.replace(JwtHeaderConsts.BEARER_PREFIX, ""); + } + +} \ No newline at end of file diff --git a/kobaco/kobaco/src/main/java/core/kobaco/global/jwt/JwtUtils.java b/kobaco/kobaco/src/main/java/core/kobaco/global/jwt/JwtUtils.java new file mode 100644 index 0000000..7c47dd4 --- /dev/null +++ b/kobaco/kobaco/src/main/java/core/kobaco/global/jwt/JwtUtils.java @@ -0,0 +1,28 @@ +package core.kobaco.global.jwt; + +import jakarta.servlet.http.HttpServletRequest; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Component; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; + +import java.util.Objects; + +@Component +@RequiredArgsConstructor +public class JwtUtils { + private final JwtProvider jwtProvider; + private final BearerTokenGenerator bearerTokenGenerator; + + public PrivateClaims.UserClaims parseUserClaims() { + HttpServletRequest request = + ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest(); + String tokenWithBearer = request.getHeader(JwtHeaderConsts.AUTHORIZATION_HEADER); + String token = bearerTokenGenerator.detachBearerPrefix(tokenWithBearer); + return jwtProvider.extractUserClaimsFromToken(token, TokenType.ACCESS_TOKEN); + } + + public Long getRequestUserId(){ + return parseUserClaims().getUserId(); + } +} From 683ac358191fd8794593e15463c4539cca62aa8d Mon Sep 17 00:00:00 2001 From: tlarbals824 Date: Wed, 28 Feb 2024 14:47:48 +0900 Subject: [PATCH 2/4] =?UTF-8?q?feat=20:=20=EC=9A=94=EC=B2=AD=20=EB=B3=B4?= =?UTF-8?q?=EB=82=B8=20=EC=82=AC=EC=9A=A9=EC=9E=90=20=EC=A0=95=EB=B3=B4=20?= =?UTF-8?q?=EC=A1=B0=ED=9A=8C=20=EC=9C=A0=ED=8B=B8=20=ED=81=B4=EB=9E=98?= =?UTF-8?q?=EC=8A=A4=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/kobaco/domain/user/UserUtils.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 kobaco/kobaco/src/main/java/core/kobaco/domain/user/UserUtils.java diff --git a/kobaco/kobaco/src/main/java/core/kobaco/domain/user/UserUtils.java b/kobaco/kobaco/src/main/java/core/kobaco/domain/user/UserUtils.java new file mode 100644 index 0000000..89f55c0 --- /dev/null +++ b/kobaco/kobaco/src/main/java/core/kobaco/domain/user/UserUtils.java @@ -0,0 +1,18 @@ +package core.kobaco.domain.user; + +import core.kobaco.domain.user.service.UserReader; +import core.kobaco.global.jwt.JwtUtils; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Component; + +@Component +@RequiredArgsConstructor +public class UserUtils { + private final JwtUtils jwtUtils; + private final UserReader userReader; + + public User getRequestUser(){ + final Long userId = jwtUtils.getRequestUserId(); + return userReader.read(userId); + } +} From f95987088bed9647c9c2b92f764499783ce23b54 Mon Sep 17 00:00:00 2001 From: tlarbals824 Date: Wed, 28 Feb 2024 14:48:13 +0900 Subject: [PATCH 3/4] =?UTF-8?q?feat=20:=20pk=20=EA=B8=B0=EB=B0=98=20?= =?UTF-8?q?=EC=82=AC=EC=9A=A9=EC=9E=90=20=EC=A1=B0=ED=9A=8C=20=EB=A9=94?= =?UTF-8?q?=EC=86=8C=EB=93=9C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/core/kobaco/domain/user/UserRepository.java | 2 ++ .../java/core/kobaco/domain/user/service/UserReader.java | 5 +++++ .../java/core/kobaco/infra/user/UserRepositoryImpl.java | 6 ++++++ 3 files changed, 13 insertions(+) diff --git a/kobaco/kobaco/src/main/java/core/kobaco/domain/user/UserRepository.java b/kobaco/kobaco/src/main/java/core/kobaco/domain/user/UserRepository.java index 24bd426..1a805b9 100644 --- a/kobaco/kobaco/src/main/java/core/kobaco/domain/user/UserRepository.java +++ b/kobaco/kobaco/src/main/java/core/kobaco/domain/user/UserRepository.java @@ -6,4 +6,6 @@ public interface UserRepository { Boolean existsByEmailAndPassword(String email, String password); Optional findByEmail(String email); + Optional findById(Long id); + } diff --git a/kobaco/kobaco/src/main/java/core/kobaco/domain/user/service/UserReader.java b/kobaco/kobaco/src/main/java/core/kobaco/domain/user/service/UserReader.java index 5ee2497..a6defc4 100644 --- a/kobaco/kobaco/src/main/java/core/kobaco/domain/user/service/UserReader.java +++ b/kobaco/kobaco/src/main/java/core/kobaco/domain/user/service/UserReader.java @@ -15,4 +15,9 @@ public User read(String email){ return userRepository.findByEmail(email) .orElseThrow(() -> new RuntimeException("user not found")); } + + public User read(Long id){ + return userRepository.findById(id) + .orElseThrow(() -> new RuntimeException("user not found")); + } } diff --git a/kobaco/kobaco/src/main/java/core/kobaco/infra/user/UserRepositoryImpl.java b/kobaco/kobaco/src/main/java/core/kobaco/infra/user/UserRepositoryImpl.java index 0834d55..5104ac4 100644 --- a/kobaco/kobaco/src/main/java/core/kobaco/infra/user/UserRepositoryImpl.java +++ b/kobaco/kobaco/src/main/java/core/kobaco/infra/user/UserRepositoryImpl.java @@ -24,4 +24,10 @@ public Optional findByEmail(String email) { return userJpaRepository.findByEmail(email) .map(userMapper::toDomain); } + + @Override + public Optional findById(Long id) { + return userJpaRepository.findById(id) + .map(userMapper::toDomain); + } } From 2f169a684a6c5d5cb09378922477ed58395c03d4 Mon Sep 17 00:00:00 2001 From: tlarbals824 Date: Wed, 28 Feb 2024 14:48:49 +0900 Subject: [PATCH 4/4] =?UTF-8?q?refactor=20:=20=ED=86=A0=ED=81=B0=EC=97=90?= =?UTF-8?q?=20Bearer=20=EC=B6=94=EA=B0=80=ED=95=B4=EC=A3=BC=EB=8A=94=20?= =?UTF-8?q?=ED=81=B4=EB=9E=98=EC=8A=A4=20=EC=9D=B4=EB=A6=84=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD=EC=97=90=20=EB=94=B0=EB=A5=B8=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kobaco/application/user/service/UserService.java | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/kobaco/kobaco/src/main/java/core/kobaco/application/user/service/UserService.java b/kobaco/kobaco/src/main/java/core/kobaco/application/user/service/UserService.java index 44dbcac..13c5791 100644 --- a/kobaco/kobaco/src/main/java/core/kobaco/application/user/service/UserService.java +++ b/kobaco/kobaco/src/main/java/core/kobaco/application/user/service/UserService.java @@ -4,8 +4,7 @@ import core.kobaco.application.user.service.dto.response.UserLoginResponse; import core.kobaco.domain.user.service.UserReader; import core.kobaco.domain.user.service.UserValidator; -import core.kobaco.global.jwt.JwtFactory; -import core.kobaco.global.jwt.JwtProvider; +import core.kobaco.global.jwt.BearerTokenGenerator; import core.kobaco.global.jwt.PrivateClaims; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; @@ -16,14 +15,14 @@ public class UserService { private final UserReader userReader; private final UserValidator userValidator; - private final JwtFactory jwtFactory; + private final BearerTokenGenerator bearerTokenGenerator; @Transactional(readOnly = true) public UserLoginResponse login(UserLoginRequest request){ if(userValidator.existsByEmailAndPassword(request.email(), request.password())){ PrivateClaims.UserClaims userClaims = PrivateClaims.UserClaims.of(userReader.read(request.email()).getId()); - return new UserLoginResponse(jwtFactory.generateAccessToken(userClaims)); + return new UserLoginResponse(bearerTokenGenerator.generateAccessTokenAndAttachBearer(userClaims)); } throw new RuntimeException("user not found"); // TODO : 예외 정의하기 } -} +} \ No newline at end of file