diff --git a/src/main/java/com/goormthon3/team49/domain/email/application/MailService.java b/src/main/java/com/goormthon3/team49/domain/email/application/MailService.java index 64dcfef..71a2025 100644 --- a/src/main/java/com/goormthon3/team49/domain/email/application/MailService.java +++ b/src/main/java/com/goormthon3/team49/domain/email/application/MailService.java @@ -3,18 +3,27 @@ import jakarta.mail.MessagingException; import jakarta.mail.internet.MimeMessage; import lombok.RequiredArgsConstructor; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.http.HttpStatus; import org.springframework.mail.MailException; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.stereotype.Service; +import org.springframework.web.server.ResponseStatusException; import java.util.Random; +import java.util.concurrent.TimeUnit; @Service @RequiredArgsConstructor public class MailService { + @Value("${EMAIL}") + private String email; + private final JavaMailSender javaMailSender; - private static final String senderEmail = "GOORM.DANPOONG49@gmail.com"; + private final String senderEmail = email; + private final RedisTemplate redisTemplate; // 랜덤으로 숫자 생성 public String createNumber() { @@ -48,19 +57,42 @@ public MimeMessage createMail(String mail, String number) throws MessagingExcept return message; } - // 메일 발송 - public String sendSimpleMessage(String sendEmail) throws MessagingException { - String number = createNumber(); // 랜덤 인증번호 생성 + //인증코드 생성 및 Redis 저장 + public void saveAuthCodeToRedis(String email, String authCode) { + redisTemplate.opsForValue().set(email, authCode, 3, TimeUnit.MINUTES); //TTL 3분 + } + + //메일 발송 + public void sendVerificationEmail(String sendEmail) throws MessagingException { + String number = createNumber(); - MimeMessage message = createMail(sendEmail, number); // 메일 생성 + MimeMessage message = createMail(sendEmail, number); try { - javaMailSender.send(message); // 메일 발송 + javaMailSender.send(message); } catch (MailException e) { e.printStackTrace(); throw new IllegalArgumentException("메일 발송 중 오류가 발생했습니다."); } - return number; // 생성된 인증번호 반환 + saveAuthCodeToRedis(sendEmail, number); + } + + //인증코드 검증 + public boolean verifyAuthCode(String email, String inputCode) { + + String storedCode = redisTemplate.opsForValue().get(email); + + if (storedCode == null) { + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "인증 코드가 만료되었거나 존재하지 않습니다."); + } + + if (!storedCode.equals(inputCode)) { + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "인증 코드가 일치하지 않습니다."); + } + + redisTemplate.delete(email); + + return true; } } diff --git a/src/main/java/com/goormthon3/team49/domain/email/presentation/MailController.java b/src/main/java/com/goormthon3/team49/domain/email/presentation/MailController.java index 1f80d36..1f9a212 100644 --- a/src/main/java/com/goormthon3/team49/domain/email/presentation/MailController.java +++ b/src/main/java/com/goormthon3/team49/domain/email/presentation/MailController.java @@ -3,9 +3,9 @@ import com.goormthon3.team49.domain.email.application.MailService; import jakarta.mail.MessagingException; import lombok.RequiredArgsConstructor; +import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; - -import java.io.UnsupportedEncodingException; +import org.springframework.web.server.ResponseStatusException; @RestController @RequiredArgsConstructor @@ -14,11 +14,25 @@ public class MailController { private final MailService mailService; - @ResponseBody - @PostMapping("/email") // 이 부분은 각자 바꿔주시면 됩니다. - public String emailCheck(@RequestBody MailDto mailDTO) throws MessagingException, UnsupportedEncodingException { - String authCode = mailService.sendSimpleMessage(mailDTO.getEmail()); + //이메일로 인증코드 발송 + @PostMapping("/email") + public ResponseEntity sendEmail(@RequestBody MailDto mailDto) throws MessagingException { + + mailService.sendVerificationEmail(mailDto.getEmail()); + + return ResponseEntity.ok("인증 코드가 이메일로 전송되었습니다."); + } + + //인증코드 검증 + @PostMapping("/email/verify") + public ResponseEntity verifyCode(@RequestBody MailVerifyDto verifyDto) { + + try { + mailService.verifyAuthCode(verifyDto.getEmail(), verifyDto.getAuthCode()); - return authCode; // Response body에 값을 반환 + return ResponseEntity.ok("인증이 완료되었습니다."); + } catch (ResponseStatusException ex) { + return ResponseEntity.status(ex.getStatusCode()).body(ex.getReason()); + } } } diff --git a/src/main/java/com/goormthon3/team49/domain/email/presentation/MailVerifyDto.java b/src/main/java/com/goormthon3/team49/domain/email/presentation/MailVerifyDto.java new file mode 100644 index 0000000..8ccc527 --- /dev/null +++ b/src/main/java/com/goormthon3/team49/domain/email/presentation/MailVerifyDto.java @@ -0,0 +1,13 @@ +package com.goormthon3.team49.domain.email.presentation; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@NoArgsConstructor +@AllArgsConstructor +public class MailVerifyDto { + private String email; + private String authCode; +}