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 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/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); + } +} 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/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(); + } +} 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); + } }