From 1560e5dca4730828be12c1826ac6f92fcbff2817 Mon Sep 17 00:00:00 2001 From: shinhn Date: Wed, 15 Mar 2023 15:58:04 +0900 Subject: [PATCH] =?UTF-8?q?#73=20#8=20feat=20:=20=EC=9C=A0=EC=A0=80=20?= =?UTF-8?q?=ED=94=84=EB=A1=9C=ED=95=84=20=EC=A1=B0=ED=9A=8C=EC=8B=9C,=20?= =?UTF-8?q?=EC=B0=A8=EB=8B=A8=20=EC=9C=A0=EB=AC=B4=20=ED=8F=AC=ED=95=A8?= =?UTF-8?q?=EB=90=98=EA=B2=8C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../applelogin/service/AppleServiceImpl.java | 4 +- .../block/repository/BlockRepository.java | 2 + .../server/block/service/BlockService.java | 2 + .../block/service/BlockServiceImpl.java | 10 ++++- .../board/service/BoardServiceImpl.java | 20 +++++----- .../boarduser/BoardUserServiceImpl.java | 8 ++-- .../clipboard/ClipBoardServiceImpl.java | 12 +++--- .../service/comment/CommentServiceImpl.java | 10 +++-- .../server/global/service/TokenService.java | 35 ++++++++++++++++ .../service/ClipBoradReportServiceImpl.java | 5 ++- .../report/service/UserReportServiceImpl.java | 4 +- .../boardreport/BoardReportServiceImpl.java | 4 +- .../comment/CommentReportServiceImpl.java | 4 +- .../user/dto/response/UserProfileRes.java | 5 ++- .../server/user/service/UserService.java | 1 - .../server/user/service/UserServiceImpl.java | 40 +++++++------------ 16 files changed, 108 insertions(+), 58 deletions(-) create mode 100644 server/src/main/java/com/yogit/server/global/service/TokenService.java diff --git a/server/src/main/java/com/yogit/server/applelogin/service/AppleServiceImpl.java b/server/src/main/java/com/yogit/server/applelogin/service/AppleServiceImpl.java index 1cb70aa..0e77dc8 100644 --- a/server/src/main/java/com/yogit/server/applelogin/service/AppleServiceImpl.java +++ b/server/src/main/java/com/yogit/server/applelogin/service/AppleServiceImpl.java @@ -5,6 +5,7 @@ import com.yogit.server.applelogin.model.ServicesResponse; import com.yogit.server.applelogin.model.TokenResponse; import com.yogit.server.applelogin.util.AppleUtils; +import com.yogit.server.global.service.TokenService; import com.yogit.server.user.dto.request.CreateUserAppleReq; import com.yogit.server.user.entity.User; import com.yogit.server.user.entity.UserStatus; @@ -37,6 +38,7 @@ public class AppleServiceImpl implements AppleService { private final AppleUtils appleUtils; private final UserService userService; private final UserRepository userRepository; + private final TokenService tokenService; @Value("${APPLE.AUD}") String client_id; @@ -163,7 +165,7 @@ public void deleteUser(DeleteUserReq deleteUserReq) { restTemplate.postForEntity(revokeUrl, httpEntity, String.class); // 유저 정보 삭제 및 유저 상태 변경 (DELETE) - userService.validateRefreshToken(deleteUserReq.getUserId(), deleteUserReq.getRefreshToken()); + tokenService.validateRefreshToken(deleteUserReq.getUserId(), deleteUserReq.getRefreshToken()); User user = userRepository.findByUserId(deleteUserReq.getUserId()).orElseThrow(() -> new NotFoundUserException()); user.deleteUser(); } diff --git a/server/src/main/java/com/yogit/server/block/repository/BlockRepository.java b/server/src/main/java/com/yogit/server/block/repository/BlockRepository.java index 5188340..9111bf2 100644 --- a/server/src/main/java/com/yogit/server/block/repository/BlockRepository.java +++ b/server/src/main/java/com/yogit/server/block/repository/BlockRepository.java @@ -16,4 +16,6 @@ public interface BlockRepository extends JpaRepository { @Query("select bl from Block bl where bl.blockingUser.id = :blockingUserId and bl.status = 'ACTIVE'") List findBlocksByBlockingUserId(@Param("blockingUserId") Long blockingUserId); + + boolean existsByBlockingUserIdAndBlockedUserId(Long blockingUserId, Long blockedUserId); } diff --git a/server/src/main/java/com/yogit/server/block/service/BlockService.java b/server/src/main/java/com/yogit/server/block/service/BlockService.java index 6e600e5..72b4897 100644 --- a/server/src/main/java/com/yogit/server/block/service/BlockService.java +++ b/server/src/main/java/com/yogit/server/block/service/BlockService.java @@ -3,8 +3,10 @@ import com.yogit.server.block.dto.req.CreateBlockReq; import com.yogit.server.block.dto.res.BlockRes; import com.yogit.server.global.dto.ApplicationResponse; +import com.yogit.server.user.entity.User; public interface BlockService { ApplicationResponse createBlock(CreateBlockReq createBlockReq); + boolean isBlockingUser(Long blockingUserId, Long blockedUserId); } diff --git a/server/src/main/java/com/yogit/server/block/service/BlockServiceImpl.java b/server/src/main/java/com/yogit/server/block/service/BlockServiceImpl.java index 2ebd84f..16766a9 100644 --- a/server/src/main/java/com/yogit/server/block/service/BlockServiceImpl.java +++ b/server/src/main/java/com/yogit/server/block/service/BlockServiceImpl.java @@ -11,6 +11,7 @@ import com.yogit.server.board.repository.BoardUserRepository; import com.yogit.server.config.domain.BaseStatus; import com.yogit.server.global.dto.ApplicationResponse; +import com.yogit.server.global.service.TokenService; import com.yogit.server.user.entity.User; import com.yogit.server.user.exception.NotFoundUserException; import com.yogit.server.user.repository.UserRepository; @@ -28,15 +29,15 @@ public class BlockServiceImpl implements BlockService{ private final BlockRepository blockRepository; private final UserRepository userRepository; - private final UserService userService; private final BoardRepository boardRepository; private final BoardUserRepository boardUserRepository; + private final TokenService tokenService; @Override @Transactional(readOnly = false) public ApplicationResponse createBlock(CreateBlockReq dto){ - userService.validateRefreshToken(dto.getBlockingUserId(), dto.getRefreshToken()); + tokenService.validateRefreshToken(dto.getBlockingUserId(), dto.getRefreshToken()); // 차단 생성하는 유저 조회 User blockingUser = userRepository.findByUserId(dto.getBlockingUserId()) @@ -93,4 +94,9 @@ public ApplicationResponse createBlock(CreateBlockReq dto){ BlockRes res = BlockRes.toDto(block); return ApplicationResponse.create("자단하였습니다.", res); } + + @Override + public boolean isBlockingUser(Long blockingUserId, Long blockedUserId){ + return blockRepository.existsByBlockingUserIdAndBlockedUserId(blockingUserId, blockedUserId); + } } diff --git a/server/src/main/java/com/yogit/server/board/service/BoardServiceImpl.java b/server/src/main/java/com/yogit/server/board/service/BoardServiceImpl.java index dea0ef7..16510dc 100644 --- a/server/src/main/java/com/yogit/server/board/service/BoardServiceImpl.java +++ b/server/src/main/java/com/yogit/server/board/service/BoardServiceImpl.java @@ -16,6 +16,7 @@ import com.yogit.server.board.repository.CategoryRepository; import com.yogit.server.board.repository.BoardRepository; import com.yogit.server.global.dto.ApplicationResponse; +import com.yogit.server.global.service.TokenService; import com.yogit.server.s3.AwsS3Service; import com.yogit.server.user.entity.City; import com.yogit.server.user.entity.User; @@ -49,6 +50,7 @@ public class BoardServiceImpl implements BoardService{ private final BlockRepository blockRepository; private final UserService userService; private final BoardUserRepository boardUserRepository; + private final TokenService tokenService; private static final int PAGING_SIZE = 10; private static final String PAGING_STANDARD = "date"; @@ -57,7 +59,7 @@ public class BoardServiceImpl implements BoardService{ @Override public ApplicationResponse createBoard(CreateBoardReq dto){ - userService.validateRefreshToken(dto.getHostId(), dto.getRefreshToken()); + tokenService.validateRefreshToken(dto.getHostId(), dto.getRefreshToken()); // host 조회 User host = userRepository.findByUserId(dto.getHostId()) @@ -124,7 +126,7 @@ public ApplicationResponse createBoard(CreateBoardReq dto){ @Transactional(readOnly = false) @Override public ApplicationResponse updateBoard(PatchBoardReq dto){ - userService.validateRefreshToken(dto.getHostId(), dto.getRefreshToken()); + tokenService.validateRefreshToken(dto.getHostId(), dto.getRefreshToken()); User user = userRepository.findByUserId(dto.getHostId()) .orElseThrow(() -> new NotFoundUserException()); @@ -188,7 +190,7 @@ public ApplicationResponse updateBoard(PatchBoardReq dto){ @Override public ApplicationResponse deleteBoard(DeleteBoardReq dto){ - userService.validateRefreshToken(dto.getHostId(), dto.getRefreshToken()); + tokenService.validateRefreshToken(dto.getHostId(), dto.getRefreshToken()); Board board = boardRepository.findBoardById(dto.getBoardId()) .orElseThrow(() -> new NotFoundBoardException()); @@ -210,7 +212,7 @@ public ApplicationResponse deleteBoard(DeleteBoardReq dto){ @Override public ApplicationResponse>> findAllBoards(GetAllBoardsReq dto){ - userService.validateRefreshToken(dto.getUserId(), dto.getRefreshToken()); + tokenService.validateRefreshToken(dto.getUserId(), dto.getRefreshToken()); int cursor = dto.getCursor(); @@ -245,7 +247,7 @@ public ApplicationResponse>> findAllBoards(GetAllBoard @Override public ApplicationResponse> findMyClubBoards(GetMyClubBoardsReq dto){ - userService.validateRefreshToken(dto.getUserId(), dto.getRefreshToken()); + tokenService.validateRefreshToken(dto.getUserId(), dto.getRefreshToken()); int cursor = dto.getCursor(); @@ -297,7 +299,7 @@ else if(dto.getMyClubType().equals(MyClubType.APPLIED_CLUB.toString())){ @Override public ApplicationResponse findAllBoardsByCategory(GetAllBoardsByCategoryReq dto){ - userService.validateRefreshToken(dto.getUserId(), dto.getRefreshToken()); + tokenService.validateRefreshToken(dto.getUserId(), dto.getRefreshToken()); int cursor = dto.getCursor(); @@ -328,7 +330,7 @@ public ApplicationResponse findAllBoardsByCategory(Ge @Override public ApplicationResponse>> findBoardsByCategories(GetBoardsByCategoriesReq dto) { - userService.validateRefreshToken(dto.getUserId(), dto.getRefreshToken()); + tokenService.validateRefreshToken(dto.getUserId(), dto.getRefreshToken()); int cursor = dto.getCursor(); List> boardsByCategories = new ArrayList<>(); @@ -370,7 +372,7 @@ public ApplicationResponse>> findBoardsByCategories(Ge @Override public ApplicationResponse findBoard(GetBoardReq dto){ - userService.validateRefreshToken(dto.getUserId(), dto.getRefreshToken()); + tokenService.validateRefreshToken(dto.getUserId(), dto.getRefreshToken()); User user = userRepository.findByUserId(dto.getUserId()) .orElseThrow(() -> new NotFoundUserException()); @@ -399,7 +401,7 @@ public ApplicationResponse findBoard(GetBoardReq dto){ @Override public ApplicationResponse deleteBoardImage(DeleteBoardImageReq dto){ - userService.validateRefreshToken(dto.getUserId(), dto.getRefreshToken()); + tokenService.validateRefreshToken(dto.getUserId(), dto.getRefreshToken()); User user = userRepository.findByUserId(dto.getUserId()) .orElseThrow(() -> new NotFoundUserException()); diff --git a/server/src/main/java/com/yogit/server/board/service/boarduser/BoardUserServiceImpl.java b/server/src/main/java/com/yogit/server/board/service/boarduser/BoardUserServiceImpl.java index 433541f..fc0b1d5 100644 --- a/server/src/main/java/com/yogit/server/board/service/boarduser/BoardUserServiceImpl.java +++ b/server/src/main/java/com/yogit/server/board/service/boarduser/BoardUserServiceImpl.java @@ -13,6 +13,7 @@ import com.yogit.server.board.repository.BoardRepository; import com.yogit.server.board.repository.BoardUserRepository; import com.yogit.server.global.dto.ApplicationResponse; +import com.yogit.server.global.service.TokenService; import com.yogit.server.s3.AwsS3Service; import com.yogit.server.user.entity.User; import com.yogit.server.user.entity.UserStatus; @@ -39,12 +40,13 @@ public class BoardUserServiceImpl implements BoardUserService{ private final UserService userService; private final AwsS3Service awsS3Service; private final APNService apnService; + private final TokenService tokenService; @Transactional(readOnly = false) @Override public ApplicationResponse joinBoardUser(CreateBoardUserReq dto) { - userService.validateRefreshToken(dto.getUserId(), dto.getRefreshToken()); + tokenService.validateRefreshToken(dto.getUserId(), dto.getRefreshToken()); User user = userRepository.findByUserId(dto.getUserId()) .orElseThrow(() -> new NotFoundUserException()); @@ -99,7 +101,7 @@ public ApplicationResponse joinBoardUser(CreateBoardUserReq dto) { @Override public ApplicationResponse approveBoardUser(CreateBoardUserReq dto){ - userService.validateRefreshToken(dto.getUserId(), dto.getRefreshToken()); + tokenService.validateRefreshToken(dto.getUserId(), dto.getRefreshToken()); User user = userRepository.findByUserId(dto.getUserId()) .orElseThrow(() -> new NotFoundUserException()); @@ -139,7 +141,7 @@ public ApplicationResponse approveBoardUser(CreateBoardUserReq dto @Override public ApplicationResponse delBoardUser(CreateBoardUserReq dto){ - userService.validateRefreshToken(dto.getUserId(), dto.getRefreshToken()); + tokenService.validateRefreshToken(dto.getUserId(), dto.getRefreshToken()); User user = userRepository.findByUserId(dto.getUserId()) .orElseThrow(() -> new NotFoundUserException()); diff --git a/server/src/main/java/com/yogit/server/board/service/clipboard/ClipBoardServiceImpl.java b/server/src/main/java/com/yogit/server/board/service/clipboard/ClipBoardServiceImpl.java index 69aac1b..928d4f1 100644 --- a/server/src/main/java/com/yogit/server/board/service/clipboard/ClipBoardServiceImpl.java +++ b/server/src/main/java/com/yogit/server/board/service/clipboard/ClipBoardServiceImpl.java @@ -19,6 +19,7 @@ import com.yogit.server.board.repository.CommentRepository; import com.yogit.server.config.domain.BaseStatus; import com.yogit.server.global.dto.ApplicationResponse; +import com.yogit.server.global.service.TokenService; import com.yogit.server.s3.AwsS3Service; import com.yogit.server.user.entity.User; import com.yogit.server.user.entity.UserStatus; @@ -49,12 +50,13 @@ public class ClipBoardServiceImpl implements ClipBoardService{ private final BlockRepository blockRepository; private final UserService userService; private final APNService apnService; + private final TokenService tokenService; @Transactional(readOnly = false) @Override public ApplicationResponse createClipBoard(CreateClipBoardReq dto){ - userService.validateRefreshToken(dto.getUserId(), dto.getRefreshToken()); + tokenService.validateRefreshToken(dto.getUserId(), dto.getRefreshToken()); User user = userRepository.findByUserId(dto.getUserId()) .orElseThrow(() -> new NotFoundUserException()); @@ -97,7 +99,7 @@ public ApplicationResponse createClipBoard(CreateClipBoardReq dto) @Override public ApplicationResponse findClipBoard(GetClipBoardReq dto){ - userService.validateRefreshToken(dto.getUserId(), dto.getRefreshToken()); + tokenService.validateRefreshToken(dto.getUserId(), dto.getRefreshToken()); User user = userRepository.findByUserId(dto.getUserId()) .orElseThrow(() -> new NotFoundUserException()); @@ -154,7 +156,7 @@ public ApplicationResponse findClipBoard(GetClipBoardReq dto){ @Override public ApplicationResponse findAllClipBoards(GetAllClipBoardsReq dto){ - userService.validateRefreshToken(dto.getUserId(), dto.getRefreshToken()); + tokenService.validateRefreshToken(dto.getUserId(), dto.getRefreshToken()); int cursor = dto.getCursor(); int totalPage=0; @@ -197,7 +199,7 @@ public ApplicationResponse findAllClipBoards(GetAllClipBoardsR @Override public ApplicationResponse updateClipBoard(PatchClipBoardReq dto){ - userService.validateRefreshToken(dto.getUserId(), dto.getRefreshToken()); + tokenService.validateRefreshToken(dto.getUserId(), dto.getRefreshToken()); //필요 객체 조회 User user = userRepository.findByUserId(dto.getUserId()) @@ -227,7 +229,7 @@ public ApplicationResponse updateClipBoard(PatchClipBoardReq dto){ @Override public ApplicationResponse deleteClipBoard(DeleteClipBoardReq dto){ - userService.validateRefreshToken(dto.getUserId(), dto.getRefreshToken()); + tokenService.validateRefreshToken(dto.getUserId(), dto.getRefreshToken()); User user = userRepository.findByUserId(dto.getUserId()) .orElseThrow(() -> new NotFoundUserException()); diff --git a/server/src/main/java/com/yogit/server/board/service/comment/CommentServiceImpl.java b/server/src/main/java/com/yogit/server/board/service/comment/CommentServiceImpl.java index 7474aa0..133423c 100644 --- a/server/src/main/java/com/yogit/server/board/service/comment/CommentServiceImpl.java +++ b/server/src/main/java/com/yogit/server/board/service/comment/CommentServiceImpl.java @@ -15,6 +15,7 @@ import com.yogit.server.board.repository.ClipBoardRepository; import com.yogit.server.board.repository.CommentRepository; import com.yogit.server.global.dto.ApplicationResponse; +import com.yogit.server.global.service.TokenService; import com.yogit.server.user.entity.User; import com.yogit.server.user.exception.InvalidTokenException; import com.yogit.server.user.exception.NotFoundUserException; @@ -37,13 +38,14 @@ public class CommentServiceImpl implements CommentService{ private final ClipBoardRepository clipBoardRepository; private final BlockRepository blockRepository; private final UserService userService; + private final TokenService tokenService; @Transactional(readOnly = false) @Override public ApplicationResponse createComment(CreateCommentReq dto){ - userService.validateRefreshToken(dto.getUserId(), dto.getRefreshToken()); + tokenService.validateRefreshToken(dto.getUserId(), dto.getRefreshToken()); User user = userRepository.findByUserId(dto.getUserId()) .orElseThrow(() -> new NotFoundUserException()); @@ -61,7 +63,7 @@ public ApplicationResponse createComment(CreateCommentReq dto){ @Override public ApplicationResponse> findAllComments(Long clipBoardId, Long userId, GetCommentsReq dto){ - userService.validateRefreshToken(userId, dto.getRefreshToken()); + tokenService.validateRefreshToken(userId, dto.getRefreshToken()); User user = userRepository.findByUserId(userId) .orElseThrow(() -> new NotFoundUserException()); @@ -86,7 +88,7 @@ public ApplicationResponse> findAllComments(Long clipBoardId, L @Override public ApplicationResponse deleteComment(DeleteCommentReq dto, Long commentId){ - userService.validateRefreshToken(dto.getUserId(), dto.getRefreshToken()); + tokenService.validateRefreshToken(dto.getUserId(), dto.getRefreshToken()); User user = userRepository.findByUserId(dto.getUserId()) .orElseThrow(() -> new NotFoundUserException()); @@ -109,7 +111,7 @@ public ApplicationResponse deleteComment(DeleteCommentReq dto, @Override public ApplicationResponse updateComment(PatchCommentReq dto, Long commentId){ - userService.validateRefreshToken(dto.getUserId(), dto.getRefreshToken()); + tokenService.validateRefreshToken(dto.getUserId(), dto.getRefreshToken()); User user = userRepository.findByUserId(dto.getUserId()) .orElseThrow(() -> new NotFoundUserException()); diff --git a/server/src/main/java/com/yogit/server/global/service/TokenService.java b/server/src/main/java/com/yogit/server/global/service/TokenService.java new file mode 100644 index 0000000..830f0e2 --- /dev/null +++ b/server/src/main/java/com/yogit/server/global/service/TokenService.java @@ -0,0 +1,35 @@ +package com.yogit.server.global.service; + +import com.yogit.server.applelogin.exception.InvalidRefreshTokenException; +import com.yogit.server.applelogin.exception.NotFoundRefreshTokenException; +import com.yogit.server.user.entity.User; +import com.yogit.server.user.exception.NotFoundUserException; +import com.yogit.server.user.repository.UserRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +@RequiredArgsConstructor +@Transactional(readOnly = true) +public class TokenService { + private final UserRepository userRepository; + + /** + * 리프레시 토큰 검증 + * + * refresh_token은 만료되지 않기 때문에 권한이 필요한 요청일 경우 + * 굳이 매번 애플 ID 서버로부터 refresh_token을 통해 access_token을 발급 받기보다는 + * 유저의 refresh_token을 따로 DB나 기타 저장소에 저장해두고 캐싱해두고 조회해서 검증하는편이 성능면에서 낫다는 자료를 참고 + * https://hwannny.tistory.com/71 + */ + public Void validateRefreshToken(Long userId, String refreshToken){ + User user = userRepository.findByUserId(userId).orElseThrow(() -> new NotFoundUserException()); + + if(user.getRefreshToken() == null) throw new NotFoundRefreshTokenException(); + + if(!user.getRefreshToken().equals(refreshToken)) throw new InvalidRefreshTokenException(); + + return null; + } +} diff --git a/server/src/main/java/com/yogit/server/report/service/ClipBoradReportServiceImpl.java b/server/src/main/java/com/yogit/server/report/service/ClipBoradReportServiceImpl.java index cfce89e..cdcffce 100644 --- a/server/src/main/java/com/yogit/server/report/service/ClipBoradReportServiceImpl.java +++ b/server/src/main/java/com/yogit/server/report/service/ClipBoradReportServiceImpl.java @@ -4,6 +4,7 @@ import com.yogit.server.board.exception.clipboard.NotFoundClipBoardException; import com.yogit.server.board.repository.ClipBoardRepository; import com.yogit.server.global.dto.ApplicationResponse; +import com.yogit.server.global.service.TokenService; import com.yogit.server.report.dto.req.CreateClipBoardReportReq; import com.yogit.server.report.dto.res.ClipBoardReportRes; import com.yogit.server.report.entity.ClipBoardReport; @@ -28,13 +29,13 @@ public class ClipBoradReportServiceImpl implements ClipBoradReportService{ private final UserRepository userRepository; private final ClipBoardRepository clipBoardRepository; private final UserService userService; - + private final TokenService tokenService; @Override @Transactional(readOnly = false) public ApplicationResponse createClipBoardReport(CreateClipBoardReportReq dto) { - userService.validateRefreshToken(dto.getReportingUserId(), dto.getRefreshToken()); + tokenService.validateRefreshToken(dto.getReportingUserId(), dto.getRefreshToken()); User reportingUser = userRepository.findByUserId(dto.getReportingUserId()) .orElseThrow(() -> new NotFoundUserException()); diff --git a/server/src/main/java/com/yogit/server/report/service/UserReportServiceImpl.java b/server/src/main/java/com/yogit/server/report/service/UserReportServiceImpl.java index c0f11d3..5dccfc7 100644 --- a/server/src/main/java/com/yogit/server/report/service/UserReportServiceImpl.java +++ b/server/src/main/java/com/yogit/server/report/service/UserReportServiceImpl.java @@ -1,6 +1,7 @@ package com.yogit.server.report.service; import com.yogit.server.global.dto.ApplicationResponse; +import com.yogit.server.global.service.TokenService; import com.yogit.server.report.dto.req.CreateUserReportReq; import com.yogit.server.report.dto.res.UserReportRes; import com.yogit.server.report.entity.UserReport; @@ -24,12 +25,13 @@ public class UserReportServiceImpl implements UserReportService{ private final UserReportRepository userReportRepository; private final UserRepository userRepository; private final UserService userService; + private final TokenService tokenService; @Override @Transactional(readOnly = false) public ApplicationResponse createUserReport(CreateUserReportReq dto) { - userService.validateRefreshToken(dto.getReportingUserId(), dto.getRefreshToken()); + tokenService.validateRefreshToken(dto.getReportingUserId(), dto.getRefreshToken()); User reportingUser = userRepository.findByUserId(dto.getReportingUserId()) .orElseThrow(() -> new NotFoundUserException()); diff --git a/server/src/main/java/com/yogit/server/report/service/boardreport/BoardReportServiceImpl.java b/server/src/main/java/com/yogit/server/report/service/boardreport/BoardReportServiceImpl.java index 926360b..f22d7ad 100644 --- a/server/src/main/java/com/yogit/server/report/service/boardreport/BoardReportServiceImpl.java +++ b/server/src/main/java/com/yogit/server/report/service/boardreport/BoardReportServiceImpl.java @@ -5,6 +5,7 @@ import com.yogit.server.board.repository.BoardRepository; import com.yogit.server.board.service.BoardService; import com.yogit.server.global.dto.ApplicationResponse; +import com.yogit.server.global.service.TokenService; import com.yogit.server.report.dto.req.CreateBoardReportReq; import com.yogit.server.report.dto.res.BoardReportRes; import com.yogit.server.report.entity.BoardReport; @@ -29,12 +30,13 @@ public class BoardReportServiceImpl implements BoardReportService { private final UserRepository userRepository; private final BoardRepository boardReport; private final UserService userService; + private final TokenService tokenService; @Override @Transactional(readOnly = false) public ApplicationResponse createBoardReport(CreateBoardReportReq dto) { - userService.validateRefreshToken(dto.getReportingUserId(), dto.getRefreshToken()); + tokenService.validateRefreshToken(dto.getReportingUserId(), dto.getRefreshToken()); User reportingUser = userRepository.findByUserId(dto.getReportingUserId()) .orElseThrow(() -> new NotFoundUserException()); diff --git a/server/src/main/java/com/yogit/server/report/service/comment/CommentReportServiceImpl.java b/server/src/main/java/com/yogit/server/report/service/comment/CommentReportServiceImpl.java index 6883c15..8c886ea 100644 --- a/server/src/main/java/com/yogit/server/report/service/comment/CommentReportServiceImpl.java +++ b/server/src/main/java/com/yogit/server/report/service/comment/CommentReportServiceImpl.java @@ -4,6 +4,7 @@ import com.yogit.server.board.exception.comment.NotFoundCommentException; import com.yogit.server.board.repository.CommentRepository; import com.yogit.server.global.dto.ApplicationResponse; +import com.yogit.server.global.service.TokenService; import com.yogit.server.report.dto.req.CreateCommentReportReq; import com.yogit.server.report.dto.res.CommentReportRes; import com.yogit.server.report.entity.CommentReport; @@ -28,12 +29,13 @@ public class CommentReportServiceImpl implements CommentReportService{ private final UserRepository userRepository; private final CommentRepository commentRepository; private final UserService userService; + private final TokenService tokenService; @Override @Transactional(readOnly = false) public ApplicationResponse createCommentReport(CreateCommentReportReq dto) { - userService.validateRefreshToken(dto.getReportingUserId(), dto.getRefreshToken()); + tokenService.validateRefreshToken(dto.getReportingUserId(), dto.getRefreshToken()); User reportingUser = userRepository.findByUserId(dto.getReportingUserId()) .orElseThrow(() -> new NotFoundUserException()); diff --git a/server/src/main/java/com/yogit/server/user/dto/response/UserProfileRes.java b/server/src/main/java/com/yogit/server/user/dto/response/UserProfileRes.java index 2521d3e..b44f052 100644 --- a/server/src/main/java/com/yogit/server/user/dto/response/UserProfileRes.java +++ b/server/src/main/java/com/yogit/server/user/dto/response/UserProfileRes.java @@ -37,11 +37,12 @@ public class UserProfileRes { // 유저 추가 정보 float longtitude; float latitude; - String city; - List interests = new ArrayList<>(); + // 조회 유저가 조회할 프로필의 유저를 차단 했는지 유무 + Integer isBlockingUser; // 0: 안된사람, 1: 내가 조회할 사람을 차단 + public static UserProfileRes create(User user){ UserProfileRes userProfileRes = new UserProfileRes(); diff --git a/server/src/main/java/com/yogit/server/user/service/UserService.java b/server/src/main/java/com/yogit/server/user/service/UserService.java index 22aa7de..19a90f6 100644 --- a/server/src/main/java/com/yogit/server/user/service/UserService.java +++ b/server/src/main/java/com/yogit/server/user/service/UserService.java @@ -6,7 +6,6 @@ import com.yogit.server.user.entity.User; public interface UserService { - Void validateRefreshToken(Long userId, String refreshToken); ApplicationResponse enterProfile(CreateUserProfileReq createUserProfileReq); diff --git a/server/src/main/java/com/yogit/server/user/service/UserServiceImpl.java b/server/src/main/java/com/yogit/server/user/service/UserServiceImpl.java index 73d6a56..0c7f539 100644 --- a/server/src/main/java/com/yogit/server/user/service/UserServiceImpl.java +++ b/server/src/main/java/com/yogit/server/user/service/UserServiceImpl.java @@ -2,8 +2,10 @@ import com.yogit.server.applelogin.exception.InvalidRefreshTokenException; import com.yogit.server.applelogin.exception.NotFoundRefreshTokenException; +import com.yogit.server.block.service.BlockService; import com.yogit.server.config.domain.BaseStatus; import com.yogit.server.global.dto.ApplicationResponse; +import com.yogit.server.global.service.TokenService; import com.yogit.server.s3.AwsS3Service; import com.yogit.server.user.dto.request.*; import com.yogit.server.user.dto.response.LogoutRes; @@ -42,26 +44,8 @@ public class UserServiceImpl implements UserService { private final InterestRepository interestRepository; private final UserInterestRepository userInterestRepository; private final AwsS3Service awsS3Service; - - /** - * 리프레시 토큰 검증 - * - * refresh_token은 만료되지 않기 때문에 권한이 필요한 요청일 경우 - * 굳이 매번 애플 ID 서버로부터 refresh_token을 통해 access_token을 발급 받기보다는 - * 유저의 refresh_token을 따로 DB나 기타 저장소에 저장해두고 캐싱해두고 조회해서 검증하는편이 성능면에서 낫다는 자료를 참고 - * https://hwannny.tistory.com/71 - */ - @Override - @Transactional(readOnly = true) - public Void validateRefreshToken(Long userId, String refreshToken){ - User user = userRepository.findByUserId(userId).orElseThrow(() -> new NotFoundUserException()); - - if(user.getRefreshToken() == null) throw new NotFoundRefreshTokenException(); - - if(!user.getRefreshToken().equals(refreshToken)) throw new InvalidRefreshTokenException(); - - return null; - } + private final BlockService blockService; + private final TokenService tokenService; // 국가 정보 조회 Open Api JSONObject getNation(String nationality) { @@ -89,7 +73,7 @@ JSONObject getNation(String nationality) { @Override public ApplicationResponse enterProfile(CreateUserProfileReq createUserProfileReq){ - validateRefreshToken(createUserProfileReq.getUserId(), createUserProfileReq.getRefreshToken()); + tokenService.validateRefreshToken(createUserProfileReq.getUserId(), createUserProfileReq.getRefreshToken()); if(createUserProfileReq.getGender() != null && !createUserProfileReq.getGender().equals("Prefer not to say") && !createUserProfileReq.getGender().equals("Male") && !createUserProfileReq.getGender().equals("Female")) throw new UserGenderException(); @@ -186,12 +170,16 @@ public ApplicationResponse enterProfile(CreateUserProfileReq cre @Override public ApplicationResponse getProfile(GetUserProfileReq getUserProfileReq){ - validateRefreshToken(getUserProfileReq.getRefreshTokenUserId(), getUserProfileReq.getRefreshToken()); + tokenService.validateRefreshToken(getUserProfileReq.getRefreshTokenUserId(), getUserProfileReq.getRefreshToken()); User user = userRepository.findByUserId(getUserProfileReq.getUserId()).orElseThrow(NotFoundUserException::new); UserProfileRes userProfileRes = UserProfileRes.create(user); + // 차단 유무 + if(blockService.isBlockingUser(getUserProfileReq.getRefreshTokenUserId(), getUserProfileReq.getUserId())) userProfileRes.setIsBlockingUser(1); + else userProfileRes.setIsBlockingUser(0); + // 언어 List languages = languageRepository.findAllByUserId(getUserProfileReq.getUserId()); if(!languages.isEmpty()){ @@ -236,7 +224,7 @@ public ApplicationResponse delProfile(Long userId){ @Override public ApplicationResponse getUserImage(GetUserImageReq getUserImageReq){ - validateRefreshToken(getUserImageReq.getUserId(), getUserImageReq.getRefreshToken()); + tokenService.validateRefreshToken(getUserImageReq.getUserId(), getUserImageReq.getRefreshToken()); User user = userRepository.findByUserId(getUserImageReq.getUserId()).orElseThrow(NotFoundUserException::new); UserImagesRes userImagesRes = new UserImagesRes(); @@ -258,7 +246,7 @@ public ApplicationResponse getUserImage(GetUserImageReq getUserIm @Transactional public ApplicationResponse AddAndDeleteUserImage(AddAndDeleteUserImageReq addAndDeleteUserImageReq){ - validateRefreshToken(addAndDeleteUserImageReq.getUserId(), addAndDeleteUserImageReq.getRefreshToken()); + tokenService.validateRefreshToken(addAndDeleteUserImageReq.getUserId(), addAndDeleteUserImageReq.getRefreshToken()); User user = userRepository.findByUserId(addAndDeleteUserImageReq.getUserId()).orElseThrow(NotFoundUserException::new); @@ -342,7 +330,7 @@ public User createUserApple(CreateUserAppleReq createUserAppleReq){ @Transactional public ApplicationResponse addDeviceToken(AddUserDeviceTokenReq addUserDeviceTokenReq){ - validateRefreshToken(addUserDeviceTokenReq.getUserId(), addUserDeviceTokenReq.getRefreshToken()); + tokenService.validateRefreshToken(addUserDeviceTokenReq.getUserId(), addUserDeviceTokenReq.getRefreshToken()); User user = userRepository.findByUserId(addUserDeviceTokenReq.getUserId()).orElseThrow(NotFoundUserException::new); user.addDeviceToken(addUserDeviceTokenReq.getDeviceToken()); @@ -356,7 +344,7 @@ public ApplicationResponse addDeviceToken(AddUserDeviceToken @Transactional public ApplicationResponse logout(LogoutReq logoutReq){ - validateRefreshToken(logoutReq.getUserId(), logoutReq.getRefreshToken()); + tokenService.validateRefreshToken(logoutReq.getUserId(), logoutReq.getRefreshToken()); User user = userRepository.findByUserId(logoutReq.getUserId()).orElseThrow(NotFoundUserException::new); user.changeUserStatus(UserStatus.LOGOUT);