Skip to content

Commit

Permalink
Merge pull request #22 from 9oormthon-univ/feature/GOORM-8-email
Browse files Browse the repository at this point in the history
[GOORM-8]-이메일 인증 구현 with Redis
  • Loading branch information
Junghs21 authored Nov 23, 2024
2 parents 8f42163 + 4bc3a07 commit 1b5d1b5
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "[email protected]";
private final String senderEmail = email;
private final RedisTemplate<String, String> redisTemplate;

// 랜덤으로 숫자 생성
public String createNumber() {
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<String> 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());
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 1b5d1b5

Please sign in to comment.