diff --git a/src/main/java/com/onnoff/onnoff/auth/UserContext.java b/src/main/java/com/onnoff/onnoff/auth/UserContext.java index 6506202..cef2be5 100644 --- a/src/main/java/com/onnoff/onnoff/auth/UserContext.java +++ b/src/main/java/com/onnoff/onnoff/auth/UserContext.java @@ -1,17 +1,15 @@ package com.onnoff.onnoff.auth; -import com.onnoff.onnoff.domain.user.User; - public class UserContext { - private static final ThreadLocal authenticatedUser = new ThreadLocal<>(); + private static final ThreadLocal authenticatedUser = new ThreadLocal<>(); - public static void setUser(User user){ - authenticatedUser.set(user); + public static void setUserId(Long userId){ + authenticatedUser.set(userId); } - public static User getUser(){ + public static Long getUserId(){ return authenticatedUser.get(); } - public static void clearUser(){ + public static void clearUserId(){ authenticatedUser.remove(); } } diff --git a/src/main/java/com/onnoff/onnoff/auth/config/FilterConfig.java b/src/main/java/com/onnoff/onnoff/auth/config/FilterConfig.java index 9a66ac2..7300034 100644 --- a/src/main/java/com/onnoff/onnoff/auth/config/FilterConfig.java +++ b/src/main/java/com/onnoff/onnoff/auth/config/FilterConfig.java @@ -13,7 +13,6 @@ @RequiredArgsConstructor public class FilterConfig { private final TokenProvider tokenProvider; - private final UserService userService; private final JwtUtil jwtUtil; @Bean @@ -23,6 +22,6 @@ public JwtAuthFilter jwtAuthFilter() { @Bean public UserInterceptor userInterceptor() { - return new UserInterceptor(userService, jwtUtil); + return new UserInterceptor(jwtUtil); } } \ No newline at end of file diff --git a/src/main/java/com/onnoff/onnoff/auth/controller/LoginController.java b/src/main/java/com/onnoff/onnoff/auth/controller/LoginController.java index b30dead..3378199 100644 --- a/src/main/java/com/onnoff/onnoff/auth/controller/LoginController.java +++ b/src/main/java/com/onnoff/onnoff/auth/controller/LoginController.java @@ -161,7 +161,7 @@ public ApiResponse validateServerToken(@RequestBody Jw @Operation(summary = "UserContext Test API",description = "jwt 토큰 검증 성공 시 유저 객체 저장한 거 조회 가능한지 테스트") @GetMapping("/test") public ResponseEntity testAfterGetToken(){ - log.info("authenticatedUser = {}", UserContext.getUser()); // 테스트 성공 + log.info("authenticatedUser = {}", UserContext.getUserId()); // 테스트 성공 return ResponseEntity.ok(""); } } \ No newline at end of file diff --git a/src/main/java/com/onnoff/onnoff/auth/jwt/filter/UserInterceptor.java b/src/main/java/com/onnoff/onnoff/auth/jwt/filter/UserInterceptor.java index b208310..ae8e06b 100644 --- a/src/main/java/com/onnoff/onnoff/auth/jwt/filter/UserInterceptor.java +++ b/src/main/java/com/onnoff/onnoff/auth/jwt/filter/UserInterceptor.java @@ -16,7 +16,6 @@ @Slf4j @RequiredArgsConstructor public class UserInterceptor implements HandlerInterceptor { - private final UserService userService; private final JwtUtil jwtUtil; @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { @@ -28,11 +27,10 @@ public boolean preHandle(HttpServletRequest request, HttpServletResponse respons } try{ // 얻은 아이디로 유저 조회하기 - String userId = jwtUtil.getUserId(accessToken); - User user = userService.getUser(Long.valueOf(userId)); + Long userId = Long.valueOf(jwtUtil.getUserId(accessToken)); - // 조회한 유저 ThreadLocal에 저장하기 - UserContext.setUser(user); + // 조회한 유저id ThreadLocal에 저장하기 + UserContext.setUserId(userId); return true; } catch (IllegalArgumentException exception){ @@ -46,6 +44,6 @@ public boolean preHandle(HttpServletRequest request, HttpServletResponse respons @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { // 요청 작업 끝나면 ThreadLocal 자원 해제 - UserContext.clearUser(); + UserContext.clearUserId(); } } diff --git a/src/main/java/com/onnoff/onnoff/auth/service/AppleLoginService.java b/src/main/java/com/onnoff/onnoff/auth/service/AppleLoginService.java index ee75b40..ae9fc8f 100644 --- a/src/main/java/com/onnoff/onnoff/auth/service/AppleLoginService.java +++ b/src/main/java/com/onnoff/onnoff/auth/service/AppleLoginService.java @@ -9,6 +9,7 @@ import com.onnoff.onnoff.auth.service.tokenValidator.SocialTokenValidator; import com.onnoff.onnoff.domain.user.User; import com.onnoff.onnoff.domain.user.enums.SocialType; +import com.onnoff.onnoff.domain.user.service.UserService; import io.jsonwebtoken.Jwts; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -35,9 +36,9 @@ public class AppleLoginService implements LoginService{ private final AppleAuthClient appleAuthClient; private final SocialTokenValidator validator; - @Value("${apple.key.id}") + @Value("${apple.key.id}") private String kid; - @Value("${apple.key.path}") + @Value(" ${apple.key.path}") private String keyPath; @Value("${apple.client-id}") private String clientId; @@ -83,22 +84,6 @@ private String createClientSecret() { throw new RuntimeException(e); } } - public String getAccessTokenByRfToken(String code) { - // client secret 만들기 - String clientSecret = createClientSecret(); - // refreshToken 가져오기 - User user = UserContext.getUser(); - String appleRefreshToken = user.getAppleRefreshToken(); - // 요청 - MultiValueMap urlEncoded = TokenRequest.builder() - .clientId(clientId) - .clientSecret(clientSecret) - .refreshToken(appleRefreshToken) - .grantType("refresh_token") - .build().toUrlEncoded(); - TokenResponse response = appleAuthClient.getToken(urlEncoded); - return null; - } private PrivateKey getPrivateKey() throws IOException { String privateKey = new String(Files.readAllBytes(Paths.get(keyPath) ) ); log.info("privateKey = {}", privateKey); diff --git a/src/main/java/com/onnoff/onnoff/domain/off/feed/service/FeedServiceImpl.java b/src/main/java/com/onnoff/onnoff/domain/off/feed/service/FeedServiceImpl.java index 29e8a3c..eb7a966 100644 --- a/src/main/java/com/onnoff/onnoff/domain/off/feed/service/FeedServiceImpl.java +++ b/src/main/java/com/onnoff/onnoff/domain/off/feed/service/FeedServiceImpl.java @@ -8,6 +8,7 @@ import com.onnoff.onnoff.domain.off.feed.entity.Feed; import com.onnoff.onnoff.domain.off.feed.repository.FeedRepository; import com.onnoff.onnoff.domain.user.User; +import com.onnoff.onnoff.domain.user.service.UserService; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -20,18 +21,21 @@ public class FeedServiceImpl implements FeedService { private final FeedRepository feedRepository; + private final UserService userService; @Override @Transactional public Feed addFeed(FeedRequestDTO.AddFeedDTO request) { - User user = UserContext.getUser(); + Long userId = UserContext.getUserId(); + User user = userService.getUser(userId); return feedRepository.save(FeedConverter.toFeed(request, user)); } @Override @Transactional(readOnly = true) public List getFeed(LocalDate date) { - User user = UserContext.getUser(); + Long userId = UserContext.getUserId(); + User user = userService.getUser(userId); return feedRepository.findAllByUserAndDateOrderByCreatedAtAsc(user, date); } diff --git a/src/main/java/com/onnoff/onnoff/domain/off/feedImage/service/FeedImageServiceImpl.java b/src/main/java/com/onnoff/onnoff/domain/off/feedImage/service/FeedImageServiceImpl.java index 94f71d4..9003940 100644 --- a/src/main/java/com/onnoff/onnoff/domain/off/feedImage/service/FeedImageServiceImpl.java +++ b/src/main/java/com/onnoff/onnoff/domain/off/feedImage/service/FeedImageServiceImpl.java @@ -14,7 +14,7 @@ import com.onnoff.onnoff.domain.off.feedImage.entity.FeedImage; import com.onnoff.onnoff.domain.off.feedImage.repository.FeedImageRepository; import com.onnoff.onnoff.domain.user.User; -import com.onnoff.onnoff.domain.user.repository.UserRepository; +import com.onnoff.onnoff.domain.user.service.UserService; import lombok.RequiredArgsConstructor; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; @@ -38,11 +38,13 @@ public class FeedImageServiceImpl implements FeedImageService { private final AmazonS3Client amazonS3Client; private final FeedImageRepository feedImageRepository; + private final UserService userService; @Override @Transactional public FeedImageResponseDTO.FeedImageDTO uploadFeedImage(MultipartFile multipartFile) { - User user = UserContext.getUser(); + Long userId = UserContext.getUserId(); + User user = userService.getUser(userId); FeedImage feedImage = FeedImage.builder() .user(user) @@ -57,7 +59,8 @@ public FeedImageResponseDTO.FeedImageDTO uploadFeedImage(MultipartFile multipart @Override @Transactional(readOnly = true) public List getFeedImage() { - User user = UserContext.getUser(); + Long userId = UserContext.getUserId(); + User user = userService.getUser(userId); List feedImageList = feedImageRepository.findByUserOrderByCreatedAtAsc(user); return feedImageList.stream() diff --git a/src/main/java/com/onnoff/onnoff/domain/off/memoir/service/MemoirServiceImpl.java b/src/main/java/com/onnoff/onnoff/domain/off/memoir/service/MemoirServiceImpl.java index 06c6054..541828b 100644 --- a/src/main/java/com/onnoff/onnoff/domain/off/memoir/service/MemoirServiceImpl.java +++ b/src/main/java/com/onnoff/onnoff/domain/off/memoir/service/MemoirServiceImpl.java @@ -13,6 +13,7 @@ import com.onnoff.onnoff.domain.off.memoir.repository.MemoirQuestionRepository; import com.onnoff.onnoff.domain.off.memoir.repository.MemoirRepository; import com.onnoff.onnoff.domain.user.User; +import com.onnoff.onnoff.domain.user.service.UserService; import lombok.RequiredArgsConstructor; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; @@ -22,7 +23,6 @@ import java.time.LocalDate; import java.util.ArrayList; import java.util.List; -import java.util.stream.Collectors; @Service @RequiredArgsConstructor @@ -32,11 +32,13 @@ public class MemoirServiceImpl implements MemoirService { private final MemoirAnswerRepository memoirAnswerRepository; private final MemoirQuestionRepository memoirQuestionRepository; private final EmoticonRepository emoticonRepository; + private final UserService userService; @Override @Transactional public Memoir writeMemoir(MemoirRequestDTO.MemoirWriteDTO request) { - User user = UserContext.getUser(); + Long userId = UserContext.getUserId(); + User user = userService.getUser(userId); if (memoirRepository.findByUserAndDate(user, request.getDate()).isPresent()) { throw new MemoirHandler(ErrorStatus.MEMOIR_EXIST); @@ -75,21 +77,24 @@ public Memoir writeMemoir(MemoirRequestDTO.MemoirWriteDTO request) { @Override @Transactional(readOnly = true) public Memoir getMemoirPreview(LocalDate date) { - User user = UserContext.getUser(); + Long userId = UserContext.getUserId(); + User user = userService.getUser(userId); return memoirRepository.findByUserAndDate(user, date).orElse(null); } @Override @Transactional(readOnly = true) public Memoir getMemoir(LocalDate date) { - User user = UserContext.getUser(); + Long userId = UserContext.getUserId(); + User user = userService.getUser(userId); return memoirRepository.findByUserAndDate(user, date).orElseThrow(() -> new MemoirHandler(ErrorStatus.MEMOIR_NOT_FOUND)); } @Override @Transactional(readOnly = true) public Page getBookmarkedMemoir(Integer pageNumber) { - User user = UserContext.getUser(); + Long userId = UserContext.getUserId(); + User user = userService.getUser(userId); return memoirRepository.findByUserAndIsBookmarkedOrderByDateDesc(user, true, PageRequest.of(pageNumber, 10)); } diff --git a/src/main/java/com/onnoff/onnoff/domain/on/resolution/service/ResolutionServiceImpl.java b/src/main/java/com/onnoff/onnoff/domain/on/resolution/service/ResolutionServiceImpl.java index e2fbaad..a1e0971 100644 --- a/src/main/java/com/onnoff/onnoff/domain/on/resolution/service/ResolutionServiceImpl.java +++ b/src/main/java/com/onnoff/onnoff/domain/on/resolution/service/ResolutionServiceImpl.java @@ -8,6 +8,7 @@ import com.onnoff.onnoff.domain.on.resolution.entity.Resolution; import com.onnoff.onnoff.domain.on.resolution.repository.ResolutionRepository; import com.onnoff.onnoff.domain.user.User; +import com.onnoff.onnoff.domain.user.service.UserService; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -21,18 +22,20 @@ @RequiredArgsConstructor public class ResolutionServiceImpl implements ResolutionService{ private final ResolutionRepository resolutionRepository; + private final UserService userService; @Override public List getAll(LocalDate date){ - User user = UserContext.getUser(); - + Long userId = UserContext.getUserId(); + User user = userService.getUser(userId); return resolutionRepository.findAllByUserAndDateOrderByOrder(user, date).stream().toList(); } @Override @Transactional public Resolution addResolution(ResolutionRequest.AddResolutionDTO request){ - User user = UserContext.getUser(); + Long userId = UserContext.getUserId(); + User user = userService.getUser(userId); Long order = resolutionRepository.countByUserAndDate(user, request.getDate()); @@ -63,7 +66,8 @@ public void deleteResolution(LocalDate date, Long resolutionId){ .orElseThrow(() -> new ResolutionHandler(ErrorStatus.RESOLUTION_NOT_FOUND)); //해당 resolution 아래 객체들 순서 당겨주기 - User user = UserContext.getUser(); + Long userId = UserContext.getUserId(); + User user = userService.getUser(userId); List resolutionList = resolutionRepository.findAllByUserAndDateOrderByOrder(user, date).stream().toList(); for(Resolution resolution : resolutionList){ diff --git a/src/main/java/com/onnoff/onnoff/domain/on/worklog/service/OnServiceImpl.java b/src/main/java/com/onnoff/onnoff/domain/on/worklog/service/OnServiceImpl.java index c2073a9..b51bc7f 100644 --- a/src/main/java/com/onnoff/onnoff/domain/on/worklog/service/OnServiceImpl.java +++ b/src/main/java/com/onnoff/onnoff/domain/on/worklog/service/OnServiceImpl.java @@ -8,6 +8,7 @@ import com.onnoff.onnoff.domain.on.worklog.entity.Worklog; import com.onnoff.onnoff.domain.on.worklog.repository.WorklogRepository; import com.onnoff.onnoff.domain.user.User; +import com.onnoff.onnoff.domain.user.service.UserService; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -22,12 +23,14 @@ public class OnServiceImpl implements OnService{ private final ResolutionRepository resolutionRepository; private final WorklogRepository worklogRepository; + private final UserService userService; @Override public OnResponse.OnViewDTO getOn(LocalDate date){ LocalDate localDate = Objects.requireNonNullElseGet(date, LocalDate::now); - User user = UserContext.getUser(); + Long userId = UserContext.getUserId(); + User user = userService.getUser(userId); List resolutionList = resolutionRepository.findAllByUserAndDateOrderByOrder(user, localDate); diff --git a/src/main/java/com/onnoff/onnoff/domain/on/worklog/service/WorklogServiceImpl.java b/src/main/java/com/onnoff/onnoff/domain/on/worklog/service/WorklogServiceImpl.java index 6d354cb..d9a73a3 100644 --- a/src/main/java/com/onnoff/onnoff/domain/on/worklog/service/WorklogServiceImpl.java +++ b/src/main/java/com/onnoff/onnoff/domain/on/worklog/service/WorklogServiceImpl.java @@ -8,6 +8,7 @@ import com.onnoff.onnoff.domain.on.worklog.entity.Worklog; import com.onnoff.onnoff.domain.on.worklog.repository.WorklogRepository; import com.onnoff.onnoff.domain.user.User; +import com.onnoff.onnoff.domain.user.service.UserService; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -20,11 +21,13 @@ @RequiredArgsConstructor public class WorklogServiceImpl implements WorklogService{ private final WorklogRepository worklogRepository; + private final UserService userService; @Override @Transactional public List getWorklog(LocalDate date){ - User user = UserContext.getUser(); + Long userId = UserContext.getUserId(); + User user = userService.getUser(userId); return worklogRepository.findAllByUserAndDateOrderByCreatedAt(user, date); } @@ -32,7 +35,8 @@ public List getWorklog(LocalDate date){ @Override @Transactional public Worklog addWorklog(WorklogRequest.AddWorklogDTO request){ - User user = UserContext.getUser(); + Long userId = UserContext.getUserId(); + User user = userService.getUser(userId); Worklog worklog = WorklogConverter.toAddWorklog(request); worklog.setUser(user); diff --git a/src/main/java/com/onnoff/onnoff/domain/push/service/PushNotificationSettingService.java b/src/main/java/com/onnoff/onnoff/domain/push/service/PushNotificationSettingService.java index a3e1ea8..0882215 100644 --- a/src/main/java/com/onnoff/onnoff/domain/push/service/PushNotificationSettingService.java +++ b/src/main/java/com/onnoff/onnoff/domain/push/service/PushNotificationSettingService.java @@ -7,6 +7,7 @@ import com.onnoff.onnoff.domain.push.entity.PushNotificationSetting; import com.onnoff.onnoff.domain.push.repository.PushNotificationSettingRepository; import com.onnoff.onnoff.domain.user.User; +import com.onnoff.onnoff.domain.user.service.UserService; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -17,10 +18,12 @@ @RequiredArgsConstructor public class PushNotificationSettingService { public final PushNotificationSettingRepository pushNotificationSettingRepository; + private final UserService userService; @Transactional public PushNotificationSettingResponseDTO setPushNotification(PushNotificationSettingRequestDTO pushNotificationSettingRequestDTO) { - User user = UserContext.getUser(); + Long userId = UserContext.getUserId(); + User user = userService.getUser(userId); PushNotificationSetting pushNotificationSetting = pushNotificationSettingRepository.findByUser(user) .orElseGet(() -> PushNotificationConverter.toPushNotificationSetting(pushNotificationSettingRequestDTO, user)); if (pushNotificationSetting.getId() != null) { @@ -34,7 +37,8 @@ public PushNotificationSettingResponseDTO setPushNotification(PushNotificationSe @Transactional(readOnly = true) public PushNotificationSettingResponseDTO getPushNotification() { - User user = UserContext.getUser(); + Long userId = UserContext.getUserId(); + User user = userService.getUser(userId); Optional pushNotificationSetting = pushNotificationSettingRepository.findByUser(user); if (pushNotificationSetting.isPresent()) { return PushNotificationConverter.toPushNotificationResponseDTO(pushNotificationSetting.get()); diff --git a/src/main/java/com/onnoff/onnoff/domain/stats/service/StatsServiceImpl.java b/src/main/java/com/onnoff/onnoff/domain/stats/service/StatsServiceImpl.java index a1a234f..82690eb 100644 --- a/src/main/java/com/onnoff/onnoff/domain/stats/service/StatsServiceImpl.java +++ b/src/main/java/com/onnoff/onnoff/domain/stats/service/StatsServiceImpl.java @@ -10,6 +10,7 @@ import com.onnoff.onnoff.domain.stats.converter.StatsConverter; import com.onnoff.onnoff.domain.stats.dto.StatsResponseDTO; import com.onnoff.onnoff.domain.user.User; +import com.onnoff.onnoff.domain.user.service.UserService; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; @@ -26,12 +27,14 @@ public class StatsServiceImpl implements StatsService{ private final FeedRepository feedRepository; private final MemoirRepository memoirRepository; private final MemoirAnswerRepository memoirAnswerRepository; + private final UserService userService; @Override public StatsResponseDTO.WeekDTO getWeekStats(){ LocalDate today = LocalDate.now(); - User user = UserContext.getUser(); + Long userId = UserContext.getUserId(); + User user = userService.getUser(userId); Double on = worklogRepository.countByUserAndDateAndIsChecked(user, today, true) / worklogRepository.countByUserAndDate(user, today).doubleValue(); if(on.isNaN()) { @@ -72,7 +75,8 @@ public StatsResponseDTO.MonthDTO getMonthStats(LocalDate date){ Double avg = 0.0; - User user = UserContext.getUser(); + Long userId = UserContext.getUserId(); + User user = userService.getUser(userId); List monthStatsList = new ArrayList<>(); LocalDate localDate = date.minusDays(date.getDayOfMonth() - 1); diff --git a/src/main/java/com/onnoff/onnoff/domain/user/controller/UserController.java b/src/main/java/com/onnoff/onnoff/domain/user/controller/UserController.java index 3860a77..ffac86b 100644 --- a/src/main/java/com/onnoff/onnoff/domain/user/controller/UserController.java +++ b/src/main/java/com/onnoff/onnoff/domain/user/controller/UserController.java @@ -26,7 +26,7 @@ public class UserController { @GetMapping("/information") @Operation(summary = "마이페이지 유저 정보 조회 API",description = "마이페이지의 유저 정보를 가져오는 API 입니다.") public ApiResponse getUserInformation(){ - return ApiResponse.onSuccess(UserConverter.toUserInformationResponseDTO(UserContext.getUser())); + return ApiResponse.onSuccess(UserConverter.toUserInformationResponseDTO(userService.getUser(UserContext.getUserId() ) ) ); } @PutMapping("/withdraw") diff --git a/src/main/java/com/onnoff/onnoff/domain/user/service/UserServiceImpl.java b/src/main/java/com/onnoff/onnoff/domain/user/service/UserServiceImpl.java index 605bc43..3b3ac81 100644 --- a/src/main/java/com/onnoff/onnoff/domain/user/service/UserServiceImpl.java +++ b/src/main/java/com/onnoff/onnoff/domain/user/service/UserServiceImpl.java @@ -74,7 +74,8 @@ public User getUserByOauthId(String oauthId) { @Transactional @Override public User withdrawUser(){ - User user = UserContext.getUser(); + Long userId = UserContext.getUserId(); + User user = this.getUser(userId); user.setUserStatusInactive(); return user; } @@ -82,7 +83,8 @@ public User withdrawUser(){ @Transactional @Override public User modifyUser(UserRequestDTO.ModifyUserDTO modifyUserDTO){ - User user = UserContext.getUser(); + Long userId = UserContext.getUserId(); + User user = this.getUser(userId); user.updateUser(modifyUserDTO); userRepository.save(user); return user; diff --git a/src/test/java/com/onnoff/onnoff/domain/user/service/UserServiceImplTest.java b/src/test/java/com/onnoff/onnoff/domain/user/service/UserServiceImplTest.java index 2585534..ac6a696 100644 --- a/src/test/java/com/onnoff/onnoff/domain/user/service/UserServiceImplTest.java +++ b/src/test/java/com/onnoff/onnoff/domain/user/service/UserServiceImplTest.java @@ -18,9 +18,14 @@ public UserServiceImplTest(@Autowired UserService userService) { } @Test void 생성_및_조회() { - User user = User.builder().name("우성").nickname("우스").build(); + User user = User.builder() + .oauthId("asd") + .name("우성") + .nickname("우스") + .email("jws001228@naver.com") + .build(); - User createdUser = userService.create(user); + User createdUser = userService.create(user); User findUser = userService.getUser(createdUser.getId()); Assertions.assertThat(user.getName()).isEqualTo(findUser.getName());