Skip to content

Commit

Permalink
feat: #176 인증 메일 발송 메서드 비동기 처리
Browse files Browse the repository at this point in the history
  • Loading branch information
jaewonLeeKOR committed Sep 22, 2024
1 parent dfa244c commit 052ff1c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
2 changes: 2 additions & 0 deletions src/main/java/com/server/capple/CappleApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;

import java.time.ZoneId;
Expand All @@ -18,6 +19,7 @@
@EnableConfigurationProperties
@EnableScheduling
@EnableCaching
@EnableAsync
public class CappleApplication {

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
import com.server.capple.global.exception.RestApiException;
import com.server.capple.global.exception.errorCode.MailErrorCode;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@RequiredArgsConstructor
public class MailServiceImpl implements MailService {
Expand All @@ -19,9 +21,12 @@ public class MailServiceImpl implements MailService {

@Override
public Boolean sendMailAddressCertificationMail(String email, Boolean isWhiteList) {
String certCode = mailUtil.sendMailAddressCertificationMail(email, isWhiteList);
String emailJwt = jwtService.createJwtFromEmail(email);
return mailRedisRepository.save(emailJwt, certCode);
mailUtil.sendMailAddressCertificationMail(email, isWhiteList).thenAccept(certCode -> {
String emailJwt = jwtService.createJwtFromEmail(email);
mailRedisRepository.save(emailJwt, certCode);
log.info("메일 발송 완료 : {}", email);
});
return true;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.server.capple.domain.mail.service;

import java.util.concurrent.CompletableFuture;

public interface MailUtil {
public static Boolean emailAddressFormVerification(String emailAddress) {
String emailRegex = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,6}$";
return emailAddress.matches(emailRegex);
}

String sendMailAddressCertificationMail(String receiver, Boolean isWhiteList);
CompletableFuture<String> sendMailAddressCertificationMail(String receiver, Boolean isWhiteList);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@
import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.util.concurrent.CompletableFuture;

@Slf4j
@Component
@RequiredArgsConstructor
public class MailUtilImpl implements MailUtil {
Expand All @@ -20,19 +25,21 @@ public class MailUtilImpl implements MailUtil {
@Value("${mail.white-list-cert-code}")
private String whiteListCertCode;

@Async
@Override
public String sendMailAddressCertificationMail(String receiver, Boolean isWhiteList) {
public CompletableFuture<String> sendMailAddressCertificationMail(String receiver, Boolean isWhiteList) {
String certCode = generateCertCode();
if(isWhiteList) certCode = whiteListCertCode;
if (isWhiteList) certCode = whiteListCertCode;
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
try {
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, false, "UTF-8");
mimeMessageHelper.setTo(receiver);
mimeMessageHelper.setSubject("[Capple] 회원가입 인증코드 안내");
mimeMessageHelper.setText(setCertMailContext(certCode), true);
javaMailSender.send(mimeMessage);
return certCode;
return CompletableFuture.completedFuture(certCode);
} catch (MessagingException e) {
log.error(MailErrorCode.MULTI_PART_CRAETION_FAILED.getMessage());
throw new RestApiException(MailErrorCode.MULTI_PART_CRAETION_FAILED);
}
}
Expand Down

0 comments on commit 052ff1c

Please sign in to comment.