diff --git a/src/main/java/com/fledge/fledgeserver/auth/jwt/filter/JwtFilter.java b/src/main/java/com/fledge/fledgeserver/auth/jwt/filter/JwtFilter.java new file mode 100644 index 0000000..05fa61d --- /dev/null +++ b/src/main/java/com/fledge/fledgeserver/auth/jwt/filter/JwtFilter.java @@ -0,0 +1,40 @@ +package com.fledge.fledgeserver.auth.jwt.filter; + +import com.fledge.fledgeserver.auth.jwt.TokenProvider; +import jakarta.servlet.FilterChain; +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import java.io.IOException; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.stereotype.Component; +import org.springframework.web.filter.OncePerRequestFilter; + +@Slf4j +@Component +@RequiredArgsConstructor +public class JwtFilter extends OncePerRequestFilter { + private final TokenProvider tokenProvider; + + @Override + protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException { + + String jwt = tokenProvider.resolveToken(request); + + if (jwt != null) { + tokenProvider.validateToken(jwt); + setAuthentication(jwt); + } + + chain.doFilter(request, response); + } + + private void setAuthentication(String accessToken) { + Authentication authentication = tokenProvider.resolveToken(accessToken); + SecurityContextHolder.getContext().setAuthentication(authentication); + } + +} \ No newline at end of file diff --git a/src/main/java/com/fledge/fledgeserver/canary/dto/CanaryProfileRequest.java b/src/main/java/com/fledge/fledgeserver/canary/dto/CanaryProfileRequest.java index e316d15..495ffc3 100644 --- a/src/main/java/com/fledge/fledgeserver/canary/dto/CanaryProfileRequest.java +++ b/src/main/java/com/fledge/fledgeserver/canary/dto/CanaryProfileRequest.java @@ -1,10 +1,7 @@ package com.fledge.fledgeserver.canary.dto; import io.swagger.v3.oas.annotations.media.Schema; -import jakarta.validation.constraints.NotBlank; -import jakarta.validation.constraints.NotNull; -import jakarta.validation.constraints.Past; -import jakarta.validation.constraints.Size; +import jakarta.validation.constraints.*; import lombok.Getter; import lombok.Setter; @@ -27,6 +24,7 @@ public class CanaryProfileRequest { @Schema(description = "전화번호", required = true, example = "010-1234-5678") @NotBlank(message = "전화번호는 필수입니다.") @Size(max = 255, message = "전화번호는 최대 255자까지 입력 가능합니다.") + @Pattern(regexp = "^01(?:0|1|[6-9])[.-]?(\\d{3}|\\d{4})[.-]?(\\d{4})$", message = "10 ~ 11 자리의 숫자만 입력 가능합니다.") private String phone; @Schema(description = "생년월일", required = true, example = "1990-01-01") diff --git a/src/main/java/com/fledge/fledgeserver/canary/dto/CanaryProfileUpdateRequest.java b/src/main/java/com/fledge/fledgeserver/canary/dto/CanaryProfileUpdateRequest.java index 685bb46..6b39a41 100644 --- a/src/main/java/com/fledge/fledgeserver/canary/dto/CanaryProfileUpdateRequest.java +++ b/src/main/java/com/fledge/fledgeserver/canary/dto/CanaryProfileUpdateRequest.java @@ -1,13 +1,10 @@ package com.fledge.fledgeserver.canary.dto; import io.swagger.v3.oas.annotations.media.Schema; +import jakarta.validation.constraints.*; import lombok.Getter; import lombok.Setter; -import jakarta.validation.constraints.NotBlank; -import jakarta.validation.constraints.NotNull; -import jakarta.validation.constraints.Past; -import jakarta.validation.constraints.Size; import java.util.Date; @Getter @@ -23,6 +20,7 @@ public class CanaryProfileUpdateRequest { @Schema(description = "전화번호", required = true, example = "010-1234-5678") @NotBlank(message = "전화번호는 필수입니다.") @Size(max = 20, message = "전화번호는 최대 20자까지 입력 가능합니다.") + @Pattern(regexp = "^01(?:0|1|[6-9])[.-]?(\\d{3}|\\d{4})[.-]?(\\d{4})$", message = "10 ~ 11 자리의 숫자만 입력 가능합니다.") private String phone; @Schema(description = "생년월일", required = true, example = "1990-01-01") diff --git a/src/main/java/com/fledge/fledgeserver/canary/repository/CanaryProfileRepository.java b/src/main/java/com/fledge/fledgeserver/canary/repository/CanaryProfileRepository.java index 23b820c..65f4a52 100644 --- a/src/main/java/com/fledge/fledgeserver/canary/repository/CanaryProfileRepository.java +++ b/src/main/java/com/fledge/fledgeserver/canary/repository/CanaryProfileRepository.java @@ -7,9 +7,13 @@ import java.util.Optional; public interface CanaryProfileRepository extends JpaRepository { - boolean existsByMember(Member member); Optional findByMemberId(Long memberId); - Optional findCanaryProfileByMemberId(Long memberId); + Optional findByMemberIdAndApprovalStatusIsTrue(Long memberId); + + boolean existsByMemberAndApprovalStatusIsTrue(Member member); + + boolean existsByMember(Member member); + } diff --git a/src/main/java/com/fledge/fledgeserver/canary/service/CanaryProfileService.java b/src/main/java/com/fledge/fledgeserver/canary/service/CanaryProfileService.java index 8421c5b..ced94dc 100644 --- a/src/main/java/com/fledge/fledgeserver/canary/service/CanaryProfileService.java +++ b/src/main/java/com/fledge/fledgeserver/canary/service/CanaryProfileService.java @@ -1,6 +1,5 @@ package com.fledge.fledgeserver.canary.service; -import com.fledge.fledgeserver.auth.dto.OAuthUserImpl; import com.fledge.fledgeserver.canary.dto.*; import com.fledge.fledgeserver.canary.entity.CanaryProfile; import com.fledge.fledgeserver.canary.repository.CanaryProfileRepository; @@ -12,8 +11,6 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import static com.fledge.fledgeserver.exception.ErrorCode.MEMBER_FORBIDDEN; - @Service @RequiredArgsConstructor @@ -25,8 +22,7 @@ public class CanaryProfileService { public void createCanaryProfile(CanaryProfileRequest request) { Member member = SecurityUtils.checkAndGetCurrentUser(request.getUserId()); - boolean exists = canaryProfileRepository.existsByMember(member); - if (exists) { + if (canaryProfileRepository.existsByMember(member)){ throw new CustomException(ErrorCode.DUPLICATE_APPLICATION); } @@ -74,7 +70,11 @@ public CanaryProfileResponse getCanaryProfile(Long userId) { @Transactional public CanaryProfileResponse updateCanaryProfile(Long userId, CanaryProfileUpdateRequest request) { - SecurityUtils.checkAndGetCurrentUser(userId); + Member member = SecurityUtils.checkAndGetCurrentUser(userId); + + if (!canaryProfileRepository.existsByMemberAndApprovalStatusIsTrue(member)){ + throw new CustomException(ErrorCode.CANARY_NOT_FOUND, "인증되지 않은 자립준비청년 입니다."); + } CanaryProfile existingProfile = canaryProfileRepository.findByMemberId(userId) .orElseThrow(() -> new CustomException(ErrorCode.CANARY_NOT_FOUND)); @@ -90,7 +90,7 @@ public CanaryProfileResponse updateCanaryProfile(Long userId, CanaryProfileUpdat @Transactional(readOnly = true) public CanaryGetDeliveryInfoResponse getCanaryDeliveryInfo() { Long userId = SecurityUtils.getCurrentUserId(); - CanaryProfile canary = canaryProfileRepository.findCanaryProfileByMemberId(userId) + CanaryProfile canary = canaryProfileRepository.findByMemberIdAndApprovalStatusIsTrue(userId) .orElseThrow(() -> new CustomException(ErrorCode.CANARY_NOT_FOUND)); return new CanaryGetDeliveryInfoResponse( canary.getName(), @@ -103,7 +103,7 @@ public CanaryGetDeliveryInfoResponse getCanaryDeliveryInfo() { @Transactional(readOnly = true) public CanaryProfileGetResponse getCanaryForSupport(Long memberId) { - CanaryProfile canaryProfile = canaryProfileRepository.findByMemberId(memberId) + CanaryProfile canaryProfile = canaryProfileRepository.findByMemberIdAndApprovalStatusIsTrue(memberId) .orElseThrow(() -> new CustomException(ErrorCode.CANARY_NOT_FOUND)); return new CanaryProfileGetResponse(canaryProfile); diff --git a/src/main/java/com/fledge/fledgeserver/challenge/service/ChallengeParticipationService.java b/src/main/java/com/fledge/fledgeserver/challenge/service/ChallengeParticipationService.java index 5f1726a..0b10e74 100644 --- a/src/main/java/com/fledge/fledgeserver/challenge/service/ChallengeParticipationService.java +++ b/src/main/java/com/fledge/fledgeserver/challenge/service/ChallengeParticipationService.java @@ -1,5 +1,6 @@ package com.fledge.fledgeserver.challenge.service; +import com.fledge.fledgeserver.canary.repository.CanaryProfileRepository; import com.fledge.fledgeserver.challenge.repository.ChallengeRepository; import com.fledge.fledgeserver.challenge.Enum.Frequency; import com.fledge.fledgeserver.challenge.dto.TopParticipantResponse; @@ -13,7 +14,6 @@ import com.fledge.fledgeserver.exception.CustomException; import com.fledge.fledgeserver.exception.ErrorCode; import com.fledge.fledgeserver.member.entity.Member; -import com.fledge.fledgeserver.member.repository.MemberRepository; import lombok.RequiredArgsConstructor; import org.springframework.data.domain.PageRequest; import org.springframework.stereotype.Service; @@ -29,7 +29,7 @@ public class ChallengeParticipationService { private final ChallengeParticipationRepository participationRepository; private final ChallengeProofRepository proofRepository; - private final MemberRepository memberRepository; + private final CanaryProfileRepository canaryProfileRepository; private final ChallengeRepository challengeRepository; @Transactional @@ -37,6 +37,10 @@ public ChallengeParticipationResponse participateInChallenge(Long memberId, Long Member member = SecurityUtils.checkAndGetCurrentUser(memberId); + if (!canaryProfileRepository.existsByMemberAndApprovalStatusIsTrue(member)){ + throw new CustomException(ErrorCode.CANARY_NOT_FOUND, "인증된 자립준비 청년이 아닙니다."); + } + Challenge challenge = challengeRepository.findById(challengeId) .orElseThrow(() -> new CustomException(ErrorCode.CHALLENGE_NOT_FOUND)); diff --git a/src/main/java/com/fledge/fledgeserver/challenge/service/ChallengeProofService.java b/src/main/java/com/fledge/fledgeserver/challenge/service/ChallengeProofService.java index 4100d80..c7b99cb 100644 --- a/src/main/java/com/fledge/fledgeserver/challenge/service/ChallengeProofService.java +++ b/src/main/java/com/fledge/fledgeserver/challenge/service/ChallengeProofService.java @@ -1,10 +1,13 @@ package com.fledge.fledgeserver.challenge.service; +import com.fledge.fledgeserver.canary.repository.CanaryProfileRepository; import com.fledge.fledgeserver.challenge.repository.ChallengeProofRepository; import com.fledge.fledgeserver.challenge.dto.ChallengeProofResponse; import com.fledge.fledgeserver.challenge.entity.ChallengeProof; +import com.fledge.fledgeserver.common.utils.SecurityUtils; import com.fledge.fledgeserver.exception.CustomException; import com.fledge.fledgeserver.exception.ErrorCode; +import com.fledge.fledgeserver.member.entity.Member; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -16,9 +19,14 @@ public class ChallengeProofService { private final ChallengeProofRepository proofRepository; + private final CanaryProfileRepository canaryProfileRepository; @Transactional public ChallengeProofResponse uploadProof(Long participationId, LocalDate proofDate, String proofImageUrl) { + Member member = SecurityUtils.getCurrentMember(); + if (!canaryProfileRepository.existsByMemberAndApprovalStatusIsTrue(member)){ + throw new CustomException(ErrorCode.CANARY_NOT_FOUND, "인증된 자립준비 청년이 아닙니다."); + } ChallengeProof proof = proofRepository.findByParticipationIdAndProofDate(participationId, proofDate) .orElseThrow(() -> new CustomException(ErrorCode.CHALLENGE_PROOF_NOT_FOUND)); diff --git a/src/main/java/com/fledge/fledgeserver/member/controller/MemberController.java b/src/main/java/com/fledge/fledgeserver/member/controller/MemberController.java index a38a3c0..85f31b3 100644 --- a/src/main/java/com/fledge/fledgeserver/member/controller/MemberController.java +++ b/src/main/java/com/fledge/fledgeserver/member/controller/MemberController.java @@ -43,8 +43,7 @@ public ResponseEntity> getMemberDetails( @PutMapping("/{id}/nickname") public ResponseEntity> updateNickname( @Parameter(description = "회원 ID", required = true, example = "1") @PathVariable Long id, - @Parameter(description = "닉네임 수정 요청", required = true) @RequestBody MemberNicknameUpdateRequest request, - @AuthenticationPrincipal OAuthUserImpl oAuth2User) { + @Parameter(description = "닉네임 수정 요청", required = true) @RequestBody MemberNicknameUpdateRequest request) { MemberResponse memberResponse = memberService.updateNickname(id, request.getNickname()); return ApiResponse.success(SuccessStatus.MEMBER_NICKNAME_UPDATE_SUCCESS, memberResponse); } diff --git a/src/main/java/com/fledge/fledgeserver/support/dto/request/PostCreateRequest.java b/src/main/java/com/fledge/fledgeserver/support/dto/request/PostCreateRequest.java index affdd31..24abadf 100644 --- a/src/main/java/com/fledge/fledgeserver/support/dto/request/PostCreateRequest.java +++ b/src/main/java/com/fledge/fledgeserver/support/dto/request/PostCreateRequest.java @@ -71,6 +71,7 @@ public class PostCreateRequest { private String recipientName; @Schema(description = "전화번호", example = "010-1234-5678") + @Pattern(regexp = "^01(?:0|1|[6-9])[.-]?(\\d{3}|\\d{4})[.-]?(\\d{4})$", message = "10 ~ 11 자리의 숫자만 입력 가능합니다.") private String phone; @Schema(description = "주소", example = "서울특별시 노원구 공릉로232") diff --git a/src/main/java/com/fledge/fledgeserver/support/dto/request/PostUpdateRequest.java b/src/main/java/com/fledge/fledgeserver/support/dto/request/PostUpdateRequest.java index 0844129..29a34d9 100644 --- a/src/main/java/com/fledge/fledgeserver/support/dto/request/PostUpdateRequest.java +++ b/src/main/java/com/fledge/fledgeserver/support/dto/request/PostUpdateRequest.java @@ -68,6 +68,7 @@ public class PostUpdateRequest { private String recipientName; @Schema(description = "전화번호", example = "010-1234-5678") + @Pattern(regexp = "^01(?:0|1|[6-9])[.-]?(\\d{3}|\\d{4})[.-]?(\\d{4})$", message = "10 ~ 11 자리의 숫자만 입력 가능합니다.") @NotBlank(message = "전화번호는 필수입니다.") private String phone;