-
Notifications
You must be signed in to change notification settings - Fork 2
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
[Feature] 이메일 인증번호 발송 API 구현 #17
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7a81932
chore: mail, thymeleaf 의존성 추가 (#16)
kyeong-hyeok db9727c
chore: SecurityConfig url 수정 (#16)
kyeong-hyeok fc2a978
feat: EmailService 및 Controller, Dto 추가 (#16)
kyeong-hyeok 83e2504
docs: mail.html 템플릿 생성 (#16)
kyeong-hyeok df87af1
feat: Valid 에러 처리 Handler 추가 (#16)
kyeong-hyeok 876fbe3
chore: OpenAPIDefinition 추가 (#16)
kyeong-hyeok dd36619
feat: 이메일 중복 검사 로직 추가 (#16)
kyeong-hyeok File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
src/main/java/com/pawwithu/connectdog/domain/auth/dto/request/EmailRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.pawwithu.connectdog.domain.auth.dto.request; | ||
|
||
import jakarta.validation.constraints.Email; | ||
import jakarta.validation.constraints.NotBlank; | ||
|
||
public record EmailRequest(@Email(message="이메일 형식에 맞지 않습니다.") | ||
@NotBlank(message = "이메일은 필수 입력 값입니다.")String email) { | ||
} |
4 changes: 4 additions & 0 deletions
4
src/main/java/com/pawwithu/connectdog/domain/auth/dto/response/EmailResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package com.pawwithu.connectdog.domain.auth.dto.response; | ||
|
||
public record EmailResponse(String authCode) { | ||
} |
106 changes: 106 additions & 0 deletions
106
src/main/java/com/pawwithu/connectdog/domain/auth/service/EmailService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package com.pawwithu.connectdog.domain.auth.service; | ||
|
||
import com.pawwithu.connectdog.domain.auth.dto.request.EmailRequest; | ||
import com.pawwithu.connectdog.domain.auth.dto.response.EmailResponse; | ||
import com.pawwithu.connectdog.domain.intermediary.repository.IntermediaryRepository; | ||
import com.pawwithu.connectdog.domain.volunteer.repository.VolunteerRepository; | ||
import com.pawwithu.connectdog.error.exception.custom.BadRequestException; | ||
import jakarta.mail.MessagingException; | ||
import jakarta.mail.internet.MimeMessage; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.mail.javamail.JavaMailSender; | ||
import org.springframework.stereotype.Service; | ||
import org.thymeleaf.context.Context; | ||
import org.thymeleaf.spring6.SpringTemplateEngine; | ||
|
||
import java.io.UnsupportedEncodingException; | ||
import java.util.Random; | ||
|
||
import static com.pawwithu.connectdog.error.ErrorCode.ALREADY_EXIST_EMAIL; | ||
import static com.pawwithu.connectdog.error.ErrorCode.EMAIL_SEND_ERROR; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class EmailService { | ||
|
||
private final JavaMailSender emailSender; | ||
private final SpringTemplateEngine templateEngine; | ||
private String authNum; //랜덤 인증 코드 | ||
private final VolunteerRepository volunteerRepository; | ||
private final IntermediaryRepository intermediaryRepository; | ||
|
||
/** | ||
* 랜덤 인증 코드 생성 | ||
*/ | ||
private void createCode() { | ||
Random random = new Random(); | ||
StringBuffer key = new StringBuffer(); | ||
|
||
for(int i = 0; i < 8; i++) { // 8자리 | ||
int index = random.nextInt(3); | ||
|
||
switch (index) { | ||
case 0 : // 소문자 | ||
key.append((char) ((int)random.nextInt(26) + 97)); | ||
break; | ||
case 1: // 대문자 | ||
key.append((char) ((int)random.nextInt(26) + 65)); | ||
break; | ||
case 2: // 0-9 숫자 | ||
key.append(random.nextInt(9)); | ||
break; | ||
} | ||
} | ||
authNum = key.toString(); | ||
} | ||
|
||
/** | ||
* 메일 양식 작성 | ||
*/ | ||
private MimeMessage createEmailForm(String email) throws MessagingException, UnsupportedEncodingException { | ||
|
||
createCode(); //인증 코드 생성 | ||
String setFrom = "[email protected]"; // email-config에 설정한 자신의 이메일 주소 | ||
String toEmail = email; // 받는 사람 | ||
String title = "ConnectDog\uD83D\uDC3E 회원가입 인증 번호입니다."; // 제목 | ||
|
||
MimeMessage message = emailSender.createMimeMessage(); | ||
message.addRecipients(MimeMessage.RecipientType.TO, toEmail); // 보낼 이메일 설정 | ||
message.setSubject(title); | ||
message.setFrom(setFrom); | ||
message.setText(setContext(authNum), "utf-8", "html"); | ||
|
||
return message; | ||
} | ||
|
||
/** | ||
* 메일 전송 | ||
*/ | ||
public EmailResponse sendEmail(EmailRequest emailRequest) throws BadRequestException { | ||
// 이메일 중복 검사 | ||
if (volunteerRepository.existsByEmail(emailRequest.email())) { | ||
throw new BadRequestException(ALREADY_EXIST_EMAIL); | ||
} | ||
if (intermediaryRepository.existsByEmail(emailRequest.email())) { | ||
throw new BadRequestException(ALREADY_EXIST_EMAIL); | ||
} | ||
try{ | ||
// 메일전송에 필요한 정보 설정 | ||
MimeMessage emailForm = createEmailForm(emailRequest.email()); | ||
emailSender.send(emailForm); | ||
return new EmailResponse(authNum); | ||
}catch (UnsupportedEncodingException | MessagingException e){ | ||
throw new BadRequestException(EMAIL_SEND_ERROR); | ||
} | ||
|
||
} | ||
|
||
/** | ||
* context 설정 | ||
*/ | ||
private String setContext(String code) { | ||
Context context = new Context(); | ||
context.setVariable("code", code); | ||
return templateEngine.process("mail", context); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<!DOCTYPE html> | ||
<html xmlns:th="http://www.thymeleaf.org"> | ||
|
||
<body> | ||
<div style="margin:100px;"> | ||
<h1> 안녕하세요. ConnectDog🐾입니다.</h1> | ||
<br> | ||
<p> 아래 코드를 회원가입 창으로 돌아가 입력해주세요.</p> | ||
<br> | ||
|
||
<div align="center" style="border:1px solid black; font-family:verdana;"> | ||
<h3 style="color:blue"> 회원가입 인증 코드 입니다. </h3> | ||
<div style="font-size:130%" th:text="${code}"> </div> | ||
</div> | ||
<br/> | ||
</div> | ||
|
||
</body> | ||
</html> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Auth 관련 묶어서 사용하기로 결정했군! 좋습니다 :)