From 7816525dbe1e93607475bb9fde8f2c11dcfe9867 Mon Sep 17 00:00:00 2001 From: moonjin-kim Date: Wed, 20 Mar 2024 11:08:05 +0900 Subject: [PATCH] =?UTF-8?q?fix=20:=20=ED=86=A0=ED=81=B0=EC=9D=98=20?= =?UTF-8?q?=EC=9C=A0=ED=9A=A8=EA=B8=B0=EA=B0=84=20=ED=99=98=EA=B2=BD?= =?UTF-8?q?=EB=B3=80=EC=88=98=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../PlanIT/common/util/JwtTokenProvider.java | 11 ++-- .../sideProject/PlanIT/config/JwtConfig.java | 5 -- .../program/service/ProgramServiceImpl.java | 51 ++++++++++--------- src/main/resources/application.yml | 6 +-- 4 files changed, 37 insertions(+), 36 deletions(-) diff --git a/src/main/java/com/sideProject/PlanIT/common/util/JwtTokenProvider.java b/src/main/java/com/sideProject/PlanIT/common/util/JwtTokenProvider.java index c4bbd13..63ab919 100644 --- a/src/main/java/com/sideProject/PlanIT/common/util/JwtTokenProvider.java +++ b/src/main/java/com/sideProject/PlanIT/common/util/JwtTokenProvider.java @@ -5,21 +5,22 @@ import io.jsonwebtoken.Jwts; import io.jsonwebtoken.SignatureAlgorithm; import lombok.AllArgsConstructor; +import lombok.RequiredArgsConstructor; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import java.util.Date; @Component -@AllArgsConstructor +@RequiredArgsConstructor public class JwtTokenProvider { private final JwtConfig jwtConfig; private final RedisUtil redisUtil; -// @Value("${jwt.access-token-expire}") - private final Long ACCESS_TOKEN_EXPIRE_LENGTH = 30 * 1000L; //5분 -// @Value("${jwt.refresh-token-expire}") - private final Long REFRESH_TOKEN_EXPIRE_LENGTH = 15 * 24 * 60 * 60 * 1000L; //15일 + @Value("${spring.jwt.access-token-expire}") + private Long ACCESS_TOKEN_EXPIRE_LENGTH; + @Value("${spring.jwt.refresh-token-expire}") + private Long REFRESH_TOKEN_EXPIRE_LENGTH; public String createAccessToken(Member member) { Date now = new Date(); diff --git a/src/main/java/com/sideProject/PlanIT/config/JwtConfig.java b/src/main/java/com/sideProject/PlanIT/config/JwtConfig.java index 9f3c381..ef07628 100644 --- a/src/main/java/com/sideProject/PlanIT/config/JwtConfig.java +++ b/src/main/java/com/sideProject/PlanIT/config/JwtConfig.java @@ -8,9 +8,4 @@ public class JwtConfig { @Value("${spring.jwt.secret-key}") public String SECRET_KEY; - @Value("${spring.jwt.access-token-expire}") - public Long ACCESS_TOKEN_EXPIRE; - - @Value("${spring.jwt.refresh-token-expire}") - public Long REFRESH_TOKEN_EXPIRE; } diff --git a/src/main/java/com/sideProject/PlanIT/domain/program/service/ProgramServiceImpl.java b/src/main/java/com/sideProject/PlanIT/domain/program/service/ProgramServiceImpl.java index 765b86b..4282f6f 100644 --- a/src/main/java/com/sideProject/PlanIT/domain/program/service/ProgramServiceImpl.java +++ b/src/main/java/com/sideProject/PlanIT/domain/program/service/ProgramServiceImpl.java @@ -369,39 +369,44 @@ private Page findProgram(ProgramSearchStatus option,Pageable pageable) } } - //조건에 맞는 Registration list 조회 @Override public Page findRegistrations(long adminId, RegistrationSearchStatus option, Pageable pageable) { - Member admin = memberRepository.findById(adminId).orElseThrow(() -> - new CustomException("존재하지 않는 회원입니다.", ErrorCode.MEMBER_NOT_FOUND) - ); - - if(admin.getRole() != MemberRole.ADMIN) { - throw new CustomException("권한이 없습니다.", ErrorCode.NO_AUTHORITY); - } - - Page registration = findRegistration(option,pageable); - - if(registration.isEmpty()) { - throw new CustomException("조건을 만족하는 Registration이 없습니다.",ErrorCode.REGISTRATION_NOT_FOUND); - } - - return registration.map(FindRegistrationResponse::of); + // 회원 검증 및 권한 확인 + Member admin = validateMemberAndAuthority(adminId, MemberRole.ADMIN); + // Registration 조회 및 변환 + return findAndConvertRegistrations(option, pageable, null); } @Override public Page findRegistrationsByUser(long userId, RegistrationSearchStatus option, Pageable pageable) { - Member member = memberRepository.findById(userId).orElseThrow(() -> - new CustomException("존재하지 않는 회원입니다.", ErrorCode.MEMBER_NOT_FOUND) - ); + // 회원 검증 + Member member = validateMemberAndAuthority(userId, null); + // Registration 조회 및 변환 + return findAndConvertRegistrations(option, pageable, member); + } - Page registration = findRegistrationByUser(member, option ,pageable); + private Member validateMemberAndAuthority(long memberId, MemberRole requiredRole) { + Member member = memberRepository.findById(memberId).orElseThrow(() -> + new CustomException("존재하지 않는 회원입니다.", ErrorCode.MEMBER_NOT_FOUND)); + if (requiredRole != null && member.getRole() != requiredRole) { + throw new CustomException("권한이 없습니다.", ErrorCode.NO_AUTHORITY); + } + return member; + } + + private Page findAndConvertRegistrations(RegistrationSearchStatus option, Pageable pageable, Member member) { + Page registrations; + if (member == null) { + registrations = findRegistration(option, pageable); + } else { + registrations = findRegistrationByUser(member, option, pageable); + } - if(registration.isEmpty()) { - throw new CustomException("조건을 만족하는 Registration이 없습니다.",ErrorCode.REGISTRATION_NOT_FOUND); + if (registrations.isEmpty()) { + throw new CustomException("조건을 만족하는 Registration이 없습니다.", ErrorCode.REGISTRATION_NOT_FOUND); } - return registration.map(FindRegistrationResponse::of); + return registrations.map(FindRegistrationResponse::of); } //todo : 리팩토링 여부 생각, INVALID 조회 추가 diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 16d86fc..ac34e94 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -1,15 +1,15 @@ # default spring: profiles: - default: prod # 기본 환경을 dev로 + default: prod # 기본 환경을 prod로 fileStorage: dir: ${FILE_STORAGE_DIR} jwt: secret-key: ${SECRET_KEY} -# access-token-expire: 432000 -# refresh-token-expire: 1209600000 + access-token-expire: ${ACCESS_EXPIRE} + refresh-token-expire: ${REFRESH_EXPIRE} datasource: url: jdbc:mysql://${DB_CONNECTION_URL}?serverTimezone=Asia/Seoul&characterEncoding=UTF-8