diff --git a/src/main/java/org/hankki/hankkiserver/HankkiserverApplication.java b/src/main/java/org/hankki/hankkiserver/HankkiserverApplication.java index 69b7594b..e299317c 100644 --- a/src/main/java/org/hankki/hankkiserver/HankkiserverApplication.java +++ b/src/main/java/org/hankki/hankkiserver/HankkiserverApplication.java @@ -2,10 +2,8 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication -@EnableFeignClients public class HankkiserverApplication { public static void main(String[] args) { diff --git a/src/main/java/org/hankki/hankkiserver/api/auth/controller/AuthController.java b/src/main/java/org/hankki/hankkiserver/api/auth/controller/AuthController.java index 270fd521..7bf77a79 100644 --- a/src/main/java/org/hankki/hankkiserver/api/auth/controller/AuthController.java +++ b/src/main/java/org/hankki/hankkiserver/api/auth/controller/AuthController.java @@ -16,7 +16,7 @@ @RestController @RequiredArgsConstructor -@RequestMapping("api/v1") +@RequestMapping("/api/v1") public class AuthController { private final AuthService authService; @@ -46,8 +46,8 @@ public ResponseEntity> withdraw( @PostMapping("/auth/reissue") public ResponseEntity> reissue( - @RequestHeader(HttpHeaders.AUTHORIZATION) final String refreshtoken) { - final UserReissueResponse response = authService.reissue(refreshtoken); + @RequestHeader(HttpHeaders.AUTHORIZATION) final String refreshToken) { + final UserReissueResponse response = authService.reissue(refreshToken); return ApiResponse.success(SuccessCode.OK, response); } } diff --git a/src/main/java/org/hankki/hankkiserver/api/auth/controller/request/UserLoginRequest.java b/src/main/java/org/hankki/hankkiserver/api/auth/controller/request/UserLoginRequest.java index 7caafcc1..708c7ebf 100644 --- a/src/main/java/org/hankki/hankkiserver/api/auth/controller/request/UserLoginRequest.java +++ b/src/main/java/org/hankki/hankkiserver/api/auth/controller/request/UserLoginRequest.java @@ -1,10 +1,11 @@ package org.hankki.hankkiserver.api.auth.controller.request; import jakarta.annotation.Nullable; +import org.hankki.hankkiserver.domain.user.model.Platform; public record UserLoginRequest( @Nullable String name, - String platform + Platform platform ) { } diff --git a/src/main/java/org/hankki/hankkiserver/api/auth/service/AuthService.java b/src/main/java/org/hankki/hankkiserver/api/auth/service/AuthService.java index 86ec5ff8..526f9912 100644 --- a/src/main/java/org/hankki/hankkiserver/api/auth/service/AuthService.java +++ b/src/main/java/org/hankki/hankkiserver/api/auth/service/AuthService.java @@ -13,8 +13,6 @@ import org.hankki.hankkiserver.common.exception.UnauthorizedException; import org.hankki.hankkiserver.external.openfeign.apple.AppleOAuthProvider; import org.hankki.hankkiserver.external.openfeign.dto.SocialInfoDto; -import org.hankki.hankkiserver.domain.user.repository.UserInfoRepository; -import org.hankki.hankkiserver.domain.user.repository.UserRepository; import org.hankki.hankkiserver.api.auth.controller.request.UserLoginRequest; import org.hankki.hankkiserver.api.auth.service.response.UserLoginResponse; import org.hankki.hankkiserver.api.auth.service.response.UserReissueResponse; @@ -22,11 +20,9 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import static org.hankki.hankkiserver.domain.user.model.MemberStatus.ACTIVE; import static org.hankki.hankkiserver.domain.user.model.MemberStatus.INACTIVE; -import static org.hankki.hankkiserver.domain.user.model.Platform.*; -import static org.hankki.hankkiserver.domain.user.model.User.createUser; -import static org.hankki.hankkiserver.domain.user.model.UserInfo.createMemberInfo; +import static org.hankki.hankkiserver.domain.user.model.Platform.APPLE; +import static org.hankki.hankkiserver.domain.user.model.Platform.KAKAO; import static org.hankki.hankkiserver.auth.filter.JwtAuthenticationFilter.BEARER; @Service @@ -34,8 +30,8 @@ @RequiredArgsConstructor public class AuthService { - private final UserRepository userRepository; - private final UserInfoRepository userInfoRepository; + private final UserAdapter userAdapter; + private final UserInfoAdapter userInfoAdapter; private final JwtProvider jwtProvider; private final JwtValidator jwtValidator; private final KakaoOAuthProvider kakaoOAuthProvider; @@ -44,22 +40,22 @@ public class AuthService { public UserLoginResponse login( final String token, final UserLoginRequest request) { - Platform platform = getEnumPlatformFromStringPlatform(request.platform()); + Platform platform = request.platform(); SocialInfoDto socialInfo = getSocialInfo(token, platform, request.name()); - boolean isRegistered = isRegisteredUser(platform, socialInfo); + boolean isRegistered = userAdapter.isRegisteredUser(platform, socialInfo); User findUser = loadOrCreateUser(platform, socialInfo, isRegistered); Token issuedToken = generateTokens(findUser.getId()); return UserLoginResponse.of(issuedToken, isRegistered); } - public void logOut(final Long userId) { - UserInfo findUserInfo = getUserInfo(userId); + public void logOut(final long userId) { + UserInfo findUserInfo = userInfoAdapter.getUserInfo(userId); updateRefreshToken(null, findUserInfo); } - public void withdraw(final Long userId, final String code) { - User user = getUser(userId); - if (user.getPlatform() == APPLE){ + public void withdraw(final long userId, final String code) { + User user = userAdapter.getUser(userId); + if (APPLE == user.getPlatform()){ try { String refreshToken = appleOAuthProvider.getAppleToken(code); appleOAuthProvider.requestRevoke(refreshToken); @@ -67,27 +63,25 @@ public void withdraw(final Long userId, final String code) { throw new InvalidValueException(ErrorCode.APPLE_REVOKE_FAILED); } } - - userRepository.softDeleteById(userId, INACTIVE); - userInfoRepository.softDeleteByUserId(userId); user.softDelete(INACTIVE); + userInfoAdapter.softDelete(userId); } @Transactional(noRollbackFor = UnauthorizedException.class) public UserReissueResponse reissue(final String refreshToken) { - Long userId = jwtProvider.getSubject(refreshToken.substring(BEARER.length())); + long userId = jwtProvider.getSubject(refreshToken.substring(BEARER.length())); validateRefreshToken(refreshToken, userId); - UserInfo findUserInfo = getUserInfo(userId); - Token issueToken = jwtProvider.issueToken(userId); - updateRefreshToken(issueToken.refreshToken(), findUserInfo); - return UserReissueResponse.of(issueToken); + UserInfo findUserInfo = userInfoAdapter.getUserInfo(userId); + Token issueTokens = jwtProvider.issueTokens(userId); + updateRefreshToken(issueTokens.refreshToken(), findUserInfo); + return UserReissueResponse.of(issueTokens); } - private Token generateTokens(final Long userId) { - Token issuedToken = jwtProvider.issueToken(userId); - UserInfo findUserInfo = getUserInfo(userId); - updateRefreshToken(issuedToken.refreshToken(), findUserInfo); - return issuedToken; + private Token generateTokens(final long userId) { + Token issuedTokens = jwtProvider.issueTokens(userId); + UserInfo findUserInfo = userInfoAdapter.getUserInfo(userId); + updateRefreshToken(issuedTokens.refreshToken(), findUserInfo); + return issuedTokens; } private SocialInfoDto getSocialInfo( @@ -99,19 +93,12 @@ private SocialInfoDto getSocialInfo( } else if (APPLE == platform){ return appleOAuthProvider.getAppleUserInfo(providerToken, name); } - throw new EntityNotFoundException(ErrorCode.INVALID_PLATFORM_TYPE); - } - - private boolean isRegisteredUser(Platform platform, SocialInfoDto socialInfo){ - return userRepository.existsByPlatformAndSerialIdAndMemberStatus( - platform, - socialInfo.serialId(), - ACTIVE); + throw new InvalidValueException(ErrorCode.INVALID_PLATFORM_TYPE); } private User loadOrCreateUser(Platform platform, SocialInfoDto socialInfo, boolean isRegistered){ if (!isRegistered){ - User newUser = createUser( + User newUser = User.createUser( socialInfo.name(), socialInfo.email(), socialInfo.serialId(), @@ -119,53 +106,25 @@ private User loadOrCreateUser(Platform platform, SocialInfoDto socialInfo, boole saveUser(newUser); return newUser; } - - User findUser = getUser(platform, socialInfo.serialId()); - if (INACTIVE == findUser.getMemberStatus()) { - findUser.softDelete(ACTIVE); - userRepository.save(findUser); - } - return findUser; - } - - private User getUser( - final Platform platform, - final String serialId) { - return userRepository.findByPlatformAndSerialId(platform, serialId) - .orElseThrow(() -> new EntityNotFoundException(ErrorCode.USER_NOT_FOUND)); - } - - private User getUser(final Long userId) { - return userRepository.findById(userId) - .orElseThrow(() -> new EntityNotFoundException(ErrorCode.USER_NOT_FOUND)); - } - - private UserInfo getUserInfo(final Long memberId) { - return userInfoRepository.findByUserId(memberId) - .orElseThrow(() -> new EntityNotFoundException(ErrorCode.USER_INFO_NOT_FOUND)); + return userAdapter.getUser(platform, socialInfo.serialId()); } private String getRefreshToken(final Long userId) { - return userInfoRepository.findByUserId(userId) - .orElseThrow(() -> new EntityNotFoundException(ErrorCode.REFRESH_TOKEN_NOT_FOUND)) + return userInfoAdapter.getUserInfo(userId) .getRefreshToken(); } private void saveUser(final User user) { - userRepository.save(user); - UserInfo userInfo = createMemberInfo(user, null); - userInfoRepository.save(userInfo); + userAdapter.saveUser(user); + UserInfo userInfo = UserInfo.createMemberInfo(user, null); + userInfoAdapter.saveUserInfo(userInfo); } - private void updateRefreshToken( - final String refreshToken, - final UserInfo userInfo) { + private void updateRefreshToken(final String refreshToken, final UserInfo userInfo) { userInfo.updateRefreshToken(refreshToken); } - private void validateRefreshToken( - final String refreshToken, - final Long userId) { + private void validateRefreshToken(final String refreshToken, final long userId) { try { jwtValidator.validateRefreshToken(refreshToken); String storedRefreshToken = getRefreshToken(userId); diff --git a/src/main/java/org/hankki/hankkiserver/api/auth/service/UserAdapter.java b/src/main/java/org/hankki/hankkiserver/api/auth/service/UserAdapter.java new file mode 100644 index 00000000..d3bd5ce6 --- /dev/null +++ b/src/main/java/org/hankki/hankkiserver/api/auth/service/UserAdapter.java @@ -0,0 +1,40 @@ +package org.hankki.hankkiserver.api.auth.service; + +import lombok.RequiredArgsConstructor; +import org.hankki.hankkiserver.common.code.ErrorCode; +import org.hankki.hankkiserver.common.exception.EntityNotFoundException; +import org.hankki.hankkiserver.domain.user.model.Platform; +import org.hankki.hankkiserver.domain.user.model.User; +import org.hankki.hankkiserver.domain.user.repository.UserRepository; +import org.hankki.hankkiserver.external.openfeign.dto.SocialInfoDto; +import org.springframework.stereotype.Component; + +import static org.hankki.hankkiserver.domain.user.model.MemberStatus.ACTIVE; + +@Component +@RequiredArgsConstructor +public class UserAdapter { + + private final UserRepository userRepository; + + public User getUser(final Platform platform, final String serialId) { + return userRepository.findByPlatformAndSerialIdAndMemberStatus(platform, serialId, ACTIVE) + .orElseThrow(() -> new EntityNotFoundException(ErrorCode.USER_NOT_FOUND)); + } + + public User getUser(final long userId) { + return userRepository.findByIdAndMemberStatus(userId, ACTIVE) + .orElseThrow(() -> new EntityNotFoundException(ErrorCode.USER_NOT_FOUND)); + } + + public boolean isRegisteredUser(final Platform platform, final SocialInfoDto socialInfo) { + return userRepository.existsByPlatformAndSerialIdAndMemberStatus( + platform, + socialInfo.serialId(), + ACTIVE); + } + + public void saveUser(final User user) { + userRepository.save(user); + } +} diff --git a/src/main/java/org/hankki/hankkiserver/api/auth/service/UserInfoAdapter.java b/src/main/java/org/hankki/hankkiserver/api/auth/service/UserInfoAdapter.java new file mode 100644 index 00000000..be82edfe --- /dev/null +++ b/src/main/java/org/hankki/hankkiserver/api/auth/service/UserInfoAdapter.java @@ -0,0 +1,29 @@ +package org.hankki.hankkiserver.api.auth.service; + +import lombok.RequiredArgsConstructor; +import org.hankki.hankkiserver.common.code.ErrorCode; +import org.hankki.hankkiserver.common.exception.EntityNotFoundException; +import org.hankki.hankkiserver.domain.user.model.UserInfo; +import org.hankki.hankkiserver.domain.user.repository.UserInfoRepository; +import org.springframework.stereotype.Component; + +@Component +@RequiredArgsConstructor +public class UserInfoAdapter { + + private final UserInfoRepository userInfoRepository; + + public UserInfo getUserInfo(final long userId) { + return userInfoRepository.findByUserId(userId) + .orElseThrow(() -> new EntityNotFoundException(ErrorCode.USER_INFO_NOT_FOUND)); + } + + public void saveUserInfo(final UserInfo userInfo) { + userInfoRepository.save(userInfo); + } + + public void softDelete(final long userId) { + userInfoRepository.softDeleteByUserId(userId); + } + +} diff --git a/src/main/java/org/hankki/hankkiserver/auth/UserIdArgumentResolver.java b/src/main/java/org/hankki/hankkiserver/auth/UserIdArgumentResolver.java index c1c1103a..1dd34b42 100644 --- a/src/main/java/org/hankki/hankkiserver/auth/UserIdArgumentResolver.java +++ b/src/main/java/org/hankki/hankkiserver/auth/UserIdArgumentResolver.java @@ -14,7 +14,7 @@ public class UserIdArgumentResolver implements HandlerMethodArgumentResolver { @Override public boolean supportsParameter(MethodParameter parameter) { boolean hasUserIdAnnotation = parameter.hasParameterAnnotation(UserId.class); - boolean isLongType = Long.class.isAssignableFrom(parameter.getParameterType()); + boolean isLongType = parameter.getParameterType().equals(Long.class); return hasUserIdAnnotation && isLongType; } diff --git a/src/main/java/org/hankki/hankkiserver/auth/jwt/JwtProvider.java b/src/main/java/org/hankki/hankkiserver/auth/jwt/JwtProvider.java index df1cdee3..c304eba0 100644 --- a/src/main/java/org/hankki/hankkiserver/auth/jwt/JwtProvider.java +++ b/src/main/java/org/hankki/hankkiserver/auth/jwt/JwtProvider.java @@ -10,7 +10,7 @@ public class JwtProvider { private final JwtGenerator jwtGenerator; - public Token issueToken(Long userId) { + public Token issueTokens(Long userId) { return Token.of(jwtGenerator.generateToken(userId, true), jwtGenerator.generateToken(userId, false)); } diff --git a/src/main/java/org/hankki/hankkiserver/domain/user/model/Platform.java b/src/main/java/org/hankki/hankkiserver/domain/user/model/Platform.java index 5ea84b49..f0de90cf 100644 --- a/src/main/java/org/hankki/hankkiserver/domain/user/model/Platform.java +++ b/src/main/java/org/hankki/hankkiserver/domain/user/model/Platform.java @@ -2,10 +2,6 @@ import lombok.Getter; import lombok.RequiredArgsConstructor; -import org.hankki.hankkiserver.common.code.ErrorCode; -import org.hankki.hankkiserver.common.exception.InvalidValueException; - -import java.util.Arrays; @Getter @RequiredArgsConstructor @@ -15,11 +11,4 @@ public enum Platform { APPLE("APPLE"); private final String loginPlatform; - - public static Platform getEnumPlatformFromStringPlatform(String loginPlatform) { - return Arrays.stream(values()) - .filter(platform -> platform.loginPlatform.equals(loginPlatform)) - .findFirst() - .orElseThrow(() -> new InvalidValueException(ErrorCode.INVALID_PLATFORM_TYPE)); - } } diff --git a/src/main/java/org/hankki/hankkiserver/domain/user/model/User.java b/src/main/java/org/hankki/hankkiserver/domain/user/model/User.java index 73f01968..9eab3920 100644 --- a/src/main/java/org/hankki/hankkiserver/domain/user/model/User.java +++ b/src/main/java/org/hankki/hankkiserver/domain/user/model/User.java @@ -44,11 +44,7 @@ public class User extends BaseTimeEntity { @Enumerated(EnumType.STRING) private MemberStatus memberStatus; - public static User createUser( - final String name, - final String email, - final String serialId, - final Platform platform) { + public static User createUser(final String name, final String email, final String serialId, final Platform platform) { return User.builder() .name(name) .email(email) @@ -60,9 +56,11 @@ public static User createUser( } public void softDelete(MemberStatus memberStatus) { + updateStatus(memberStatus); + this.deletedAt = LocalDateTime.now(); + } + + public void updateStatus(MemberStatus memberStatus) { this.memberStatus = memberStatus; - if (INACTIVE == memberStatus) { - this.deletedAt = LocalDateTime.now(); - } } } diff --git a/src/main/java/org/hankki/hankkiserver/domain/user/model/UserRole.java b/src/main/java/org/hankki/hankkiserver/domain/user/model/UserRole.java index 5e944f37..1a41a6ea 100644 --- a/src/main/java/org/hankki/hankkiserver/domain/user/model/UserRole.java +++ b/src/main/java/org/hankki/hankkiserver/domain/user/model/UserRole.java @@ -7,7 +7,8 @@ @RequiredArgsConstructor public enum UserRole { - ADMIN("ADMIN"), USER("USER"); + ADMIN("ADMIN"), + USER("USER"); private final String value; diff --git a/src/main/java/org/hankki/hankkiserver/domain/user/repository/UserInfoRepository.java b/src/main/java/org/hankki/hankkiserver/domain/user/repository/UserInfoRepository.java index c48be449..a238ea15 100644 --- a/src/main/java/org/hankki/hankkiserver/domain/user/repository/UserInfoRepository.java +++ b/src/main/java/org/hankki/hankkiserver/domain/user/repository/UserInfoRepository.java @@ -6,7 +6,6 @@ import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; -import org.springframework.transaction.annotation.Transactional; import java.util.Optional; diff --git a/src/main/java/org/hankki/hankkiserver/domain/user/repository/UserRepository.java b/src/main/java/org/hankki/hankkiserver/domain/user/repository/UserRepository.java index edc67b79..5b8dffc1 100644 --- a/src/main/java/org/hankki/hankkiserver/domain/user/repository/UserRepository.java +++ b/src/main/java/org/hankki/hankkiserver/domain/user/repository/UserRepository.java @@ -4,19 +4,12 @@ import org.hankki.hankkiserver.domain.user.model.Platform; import org.hankki.hankkiserver.domain.user.model.User; import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.data.jpa.repository.Modifying; -import org.springframework.data.jpa.repository.Query; -import org.springframework.data.repository.query.Param; -import org.springframework.transaction.annotation.Transactional; import java.util.Optional; public interface UserRepository extends JpaRepository { - boolean existsByPlatformAndSerialIdAndMemberStatus(Platform platform, String platformId, MemberStatus memberStatus); - Optional findByPlatformAndSerialId(Platform platform, String platformId); - - @Modifying - @Query("UPDATE User u SET u.memberStatus = :memberStatus WHERE u.id = :userId") - void softDeleteById(@Param("userId") Long userId, @Param("memberStatus") MemberStatus memberStatus); + boolean existsByPlatformAndSerialIdAndMemberStatus(Platform platform, String serialId, MemberStatus memberStatus); + Optional findByPlatformAndSerialIdAndMemberStatus(Platform platform, String serialId, MemberStatus memberStatus); + Optional findByIdAndMemberStatus(Long Userid, MemberStatus memberStatus); } diff --git a/src/main/java/org/hankki/hankkiserver/external/config/FeignConfig.java b/src/main/java/org/hankki/hankkiserver/external/config/FeignConfig.java new file mode 100644 index 00000000..e033254a --- /dev/null +++ b/src/main/java/org/hankki/hankkiserver/external/config/FeignConfig.java @@ -0,0 +1,10 @@ +package org.hankki.hankkiserver.external.config; + +import org.hankki.hankkiserver.HankkiserverApplication; +import org.springframework.cloud.openfeign.EnableFeignClients; +import org.springframework.context.annotation.Configuration; + +@Configuration +@EnableFeignClients(basePackageClasses = HankkiserverApplication.class) +public class FeignConfig { +}