Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Be #82

Merged
merged 2 commits into from
Mar 20, 2024
Merged

Be #82

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
5 changes: 0 additions & 5 deletions src/main/java/com/sideProject/PlanIT/config/JwtConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -369,39 +369,44 @@ private Page<Program> findProgram(ProgramSearchStatus option,Pageable pageable)
}
}

//조건에 맞는 Registration list 조회
@Override
public Page<FindRegistrationResponse> 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> 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<FindRegistrationResponse> 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> 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<FindRegistrationResponse> findAndConvertRegistrations(RegistrationSearchStatus option, Pageable pageable, Member member) {
Page<Registration> 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 조회 추가
Expand Down
6 changes: 3 additions & 3 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Loading