-
Notifications
You must be signed in to change notification settings - Fork 0
๐ Email Send #27
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
Merged
Merged
๐ Email Send #27
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
c5b7666
:sparkles: ์ด๋ฉ์ผ ์ธ์ฆ ๊ธฐ๋ฅ ์์กด์ฑ ์ถ๊ฐ
ohyuchan123 8a35e5b
:sparkles: Email ์ธ์ฆ ๋ฒํธ ์ ์ก ๋ฐ ์ฒดํฌ ๊ธฐ๋ฅ ๊ตฌํ
ohyuchan123 b4a5657
:recycle: ์ด๋ฉ์ผ ์ธ์ฆ ๋ฒํธ ์ ๋ฌ ๋ฐฉ์ ๋ฆฌํฉํ ๋ง(์กฐ๊ธ๋ ๋ณด์์ ์ผ๋ก)
ohyuchan123 92944df
:sparkles: ์ด๋ฉ์ผ ์ธ์ฆ ๋ฒํธ ์ฌ๋ฌ๋ฒ ์์ฒญ ์ ํ (1๋ถ๋น ์ต๋ 3๋ฒ)
ohyuchan123 3915305
:recycle: ์ง๊ธ์ ์ค์ผ์ค๋ฌ ๋ฐฉ์์ ํตํด์ ์ธ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ ๊ฑฐ ์ถํ Redis๋ฅผ ํตํด์ ๊ฐ์ ํ์
ohyuchan123 d6f20ad
:recycle: ์ธ์ฆ๋ฒํธ ๋ง๋ฃ ์๊ฐ ์ง์
ohyuchan123 5fca75a
:recycle: ์ธ์ฆ๋ฒํธ ์์ฒญ ๋ง์ ์ ๋ฐ์ํ๋ ์์ธ์ฒ๋ฆฌ ์์ธํ
ohyuchan123 b40a9e5
:recycle: VerificationData Record๋ฅผ ํตํด์ ๋ ํจ์จ์ ์ผ๋ก ์ฝ๋ ๊ด๋ฆฌ
ohyuchan123 0bae0de
:recycle: ์ฝ๋ ๊ตฌ์กฐ ๊ฐ์
ohyuchan123 0bb17dc
:fire: ๋ถํ์ํ import ๋ฌธ ์ ๊ฑฐ
ohyuchan123 13cd90f
:fire: ๋ถํ์ํ gitkeep ์ ๊ฑฐ
ohyuchan123 1da4aef
:recycle: ApiResponse ์ ์ก ๋ฐฉ์ ์์ ๋ณ๊ฒฝ
ohyuchan123 579d964
:recycle: ์์ธ ์ฒ๋ฆฌ ์์ ๋ณ๊ฒฝ
ohyuchan123 ed0d689
:recycle: ์ด๋ฉ์ผ ์ธ์ฆ ๋ฐฉ์ Request ์ Response Dto ๋ฐฉ์์ผ๋ก ์ฝ๋ ๊ฐ์
ohyuchan123 8aa701d
:fire: ๋ถํ์ํ import ์ ๊ฑฐ
ohyuchan123 9991a3d
:recycle: ์์ธ์ฒ๋ฆฌ ๋ก์ง ๊ฐ์
ohyuchan123 89a7f63
:recycle: ์ด๋ฉ์ผ ์ธ์ฆ ๋ก์ง ๊ฐ์ Get -> Post
ohyuchan123 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
Empty file.
This file contains hidden or 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 hidden or 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 hidden or 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
11 changes: 11 additions & 0 deletions
11
src/main/java/com/mycom/socket/auth/dto/request/EmailRequestDto.java
This file contains hidden or 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,11 @@ | ||
| package com.mycom.socket.auth.dto.request; | ||
|
|
||
| import jakarta.validation.constraints.Email; | ||
| import jakarta.validation.constraints.NotEmpty; | ||
|
|
||
| public record EmailRequestDto( | ||
| @NotEmpty(message = "์ด๋ฉ์ผ ์ฃผ์๋ฅผ ์ ๋ ฅํด์ฃผ์ธ์.") | ||
| @Email(message = "์ ํจํ์ง ์์ ์ด๋ฉ์ผ ํ์์ ๋๋ค.") | ||
| String email | ||
| ) { | ||
| } |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/mycom/socket/auth/dto/request/EmailVerificationRequestDto.java
This file contains hidden or 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,15 @@ | ||
| package com.mycom.socket.auth.dto.request; | ||
|
|
||
| import jakarta.validation.constraints.Email; | ||
| import jakarta.validation.constraints.NotEmpty; | ||
| import jakarta.validation.constraints.Pattern; | ||
|
|
||
| public record EmailVerificationRequestDto( | ||
| @NotEmpty(message = "์ด๋ฉ์ผ ์ฃผ์๋ฅผ ์ ๋ ฅํด์ฃผ์ธ์.") | ||
| @Email(message = "์ ํจํ์ง ์์ ์ด๋ฉ์ผ ํ์์ ๋๋ค.") | ||
| String email, | ||
| @NotEmpty(message = "์ธ์ฆ ์ฝ๋๋ฅผ ์ ๋ ฅํด์ฃผ์ธ์.") | ||
| @Pattern(regexp = "^[0-9]{6}$", message = "์ธ์ฆ ์ฝ๋๋ 6์๋ฆฌ ์ซ์์ฌ์ผ ํฉ๋๋ค.") | ||
| String code | ||
| ) { | ||
| } |
14 changes: 14 additions & 0 deletions
14
src/main/java/com/mycom/socket/auth/dto/response/EmailVerificationCheckResponseDto.java
This file contains hidden or 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,14 @@ | ||
| package com.mycom.socket.auth.dto.response; | ||
|
|
||
| import com.mycom.socket.global.dto.ApiResponse; | ||
|
|
||
| public record EmailVerificationCheckResponseDto(ApiResponse<Boolean> apiResponse) { | ||
|
|
||
| public static EmailVerificationCheckResponseDto createSuccessResponse() { | ||
| return new EmailVerificationCheckResponseDto(ApiResponse.success("์ด๋ฉ์ผ ์ธ์ฆ ์ฑ๊ณต", true)); | ||
| } | ||
|
|
||
| public static EmailVerificationCheckResponseDto createFailureResponse(String errorMessage) { | ||
| return new EmailVerificationCheckResponseDto(ApiResponse.error(errorMessage)); | ||
| } | ||
| } |
14 changes: 14 additions & 0 deletions
14
src/main/java/com/mycom/socket/auth/dto/response/EmailVerificationResponseDto.java
This file contains hidden or 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,14 @@ | ||
| package com.mycom.socket.auth.dto.response; | ||
|
|
||
| import com.mycom.socket.global.dto.ApiResponse; | ||
|
|
||
| public record EmailVerificationResponseDto(ApiResponse<String> apiResponse) { | ||
|
|
||
| public static EmailVerificationResponseDto createSuccessResponse() { | ||
| return new EmailVerificationResponseDto(ApiResponse.success("์ด๋ฉ์ผ ์ ์ก ์ฑ๊ณต")); | ||
| } | ||
|
|
||
| public static EmailVerificationResponseDto createFailureResponse(String errorMessage) { | ||
| return new EmailVerificationResponseDto(ApiResponse.error(errorMessage)); | ||
| } | ||
| } |
This file contains hidden or 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
111 changes: 111 additions & 0 deletions
111
src/main/java/com/mycom/socket/auth/service/MailService.java
This file contains hidden or 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,111 @@ | ||
| package com.mycom.socket.auth.service; | ||
|
|
||
| import com.mycom.socket.auth.service.data.VerificationData; | ||
| import com.mycom.socket.global.exception.BaseException; | ||
| import jakarta.mail.MessagingException; | ||
| import jakarta.mail.internet.MimeMessage; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.mail.javamail.JavaMailSender; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.util.StringUtils; | ||
|
|
||
| import java.security.SecureRandom; | ||
| import java.util.Map; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
|
|
||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class MailService { | ||
|
|
||
| private final JavaMailSender javaMailSender; | ||
| private final RateLimiter rateLimiter; // ์ธ์ฆ ๋ฒํธ ์์ฒญ ์ ํ | ||
|
|
||
| private final Map<String, VerificationData> verificationDataMap = new ConcurrentHashMap<>(); | ||
|
|
||
| @Value("${spring.mail.username}") | ||
| private String senderEmail; | ||
|
|
||
| /** | ||
| * 6์๋ฆฌ ๋์ ์ธ์ฆ๋ฒํธ ์์ฑ | ||
| * SecureRandom ์ฌ์ฉํ์ฌ ๋ณด์์ฑ ํฅ์ | ||
| * @return 100000~999999 ๋ฒ์์ ์ธ์ฆ๋ฒํธ | ||
| */ | ||
| private String createVerificationCode() { | ||
| // Math.random()์ ์์ธก ๊ฐ๋ฅํ ๋์๋ฅผ ์์ฑํ ์ ์์ด ๋ณด์์ ์ทจ์ฝ | ||
| // SecureRandom์ ์ํธํ์ ์ผ๋ก ์์ ํ ๋์๋ฅผ ์์ฑํ๋ฏ๋ก ์ธ์ฆ๋ฒํธ ์์ฑ์ ๋ ์ ํฉ | ||
| SecureRandom secureRandom = new SecureRandom(); | ||
| return String.format("%06d", secureRandom.nextInt(1000000)); | ||
| } | ||
|
|
||
| /** | ||
| * ์ธ์ฆ๋ฉ์ผ ์์ฑ | ||
| * @param mail ์์ ์ ์ด๋ฉ์ผ ์ฃผ์ | ||
| * @return ์์ฑ๋ ์ธ์ฆ๋ฉ์ผ | ||
| */ | ||
| public MimeMessage createMail(String mail, String verificationCode) { | ||
| MimeMessage message = javaMailSender.createMimeMessage(); | ||
| try { | ||
| message.setFrom(senderEmail); | ||
| message.setRecipients(MimeMessage.RecipientType.TO, mail); | ||
| message.setSubject("์ด๋ฉ์ผ ์ธ์ฆ"); | ||
| String body = String.format(""" | ||
| <h3>์์ฒญํ์ ์ธ์ฆ ๋ฒํธ์ ๋๋ค.</h3> | ||
| <h1>%s</h1> | ||
| <h3>๊ฐ์ฌํฉ๋๋ค.</h3> | ||
| """, verificationCode); | ||
| message.setText(body, "UTF-8", "html"); | ||
| } catch (MessagingException e) { | ||
| throw new BaseException("์ด๋ฉ์ผ ์์ฑ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: " + e.getMessage(), | ||
| HttpStatus.BAD_REQUEST); | ||
| } | ||
| return message; | ||
| } | ||
|
|
||
| /** | ||
| * ์ธ์ฆ๋ฉ์ผ ๋ฐ์ก ๋ฐ ์ธ์ฆ๋ฒํธ ๋ฐํ | ||
| * @param mail ์์ ์ ์ด๋ฉ์ผ ์ฃผ์ | ||
| * @return ์์ฑ๋ ์ธ์ฆ๋ฒํธ | ||
| */ | ||
| public boolean sendMail(String mail) { | ||
| rateLimiter.checkRateLimit(mail); | ||
| String verificationCode = createVerificationCode(); | ||
| verificationDataMap.put(mail, new VerificationData(verificationCode)); | ||
|
|
||
| MimeMessage message = createMail(mail, verificationCode); | ||
| try{ | ||
| javaMailSender.send(message); | ||
| return true; | ||
| }catch (Exception e) { | ||
| throw new BaseException("์ด๋ฉ์ผ ๋ฐ์ก ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: " + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * ์ธ์ฆ๋ฒํธ ๊ฒ์ฆ | ||
| * @param email ์์ ์ ์ด๋ฉ์ผ ์ฃผ์ | ||
| * @param code ์ฌ์ฉ์๊ฐ ์ ๋ ฅํ ์ธ์ฆ๋ฒํธ | ||
| * @return ์ธ์ฆ๋ฒํธ ์ผ์น ์ฌ๋ถ | ||
| */ | ||
| public boolean verifyCode(String email, String code) { | ||
| if (!StringUtils.hasText(code) || !code.matches("\\d{6}")) { | ||
| return false; | ||
| } | ||
|
|
||
| VerificationData data = verificationDataMap.get(email); | ||
|
|
||
| if (data == null || data.isExpired()) { | ||
| return false; | ||
| } | ||
|
|
||
| boolean isVerified = data.code().equals(code); | ||
|
|
||
| if (isVerified){ | ||
| verificationDataMap.remove(email); | ||
| } | ||
|
|
||
| return isVerified; | ||
| } | ||
| } | ||
|
|
47 changes: 47 additions & 0 deletions
47
src/main/java/com/mycom/socket/auth/service/RateLimiter.java
This file contains hidden or 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,47 @@ | ||
| package com.mycom.socket.auth.service; | ||
|
|
||
| import com.mycom.socket.global.exception.BaseException; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.scheduling.annotation.Scheduled; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| import java.time.Duration; | ||
| import java.time.LocalDateTime; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
|
|
||
| @Component | ||
| @RequiredArgsConstructor | ||
| public class RateLimiter { | ||
| private final Map<String, List<LocalDateTime>> requestMap = new ConcurrentHashMap<>(); | ||
| private static final int MAX_REQUESTS = 3; // 1๋ถ๋น ์ต๋ 3๋ฒ | ||
| private static final Duration WINDOW_SIZE = Duration.ofMinutes(1); // 1๋ถ์ ์๊ฐ ๊ฐ๊ฒฉ | ||
|
|
||
| @Scheduled(fixedRate = 3600000) // 1์๊ฐ๋ง๋ค ์คํ | ||
| public void cleanup() { | ||
| LocalDateTime threshold = LocalDateTime.now().minus(WINDOW_SIZE); | ||
| requestMap.entrySet().removeIf(entry -> | ||
| entry.getValue().stream().allMatch(time -> time.isBefore(threshold))); | ||
| } | ||
|
|
||
| public void checkRateLimit(String email) { | ||
| List<LocalDateTime> requests = requestMap.computeIfAbsent(email, k -> new ArrayList<>()); | ||
| LocalDateTime now = LocalDateTime.now(); | ||
|
|
||
| requests.removeIf(requestTime -> | ||
| requestTime.plus(WINDOW_SIZE).isBefore(now)); | ||
|
|
||
| if (requests.size() >= MAX_REQUESTS) { | ||
| LocalDateTime oldestRequest = requests.get(0); | ||
| Duration waitTime = WINDOW_SIZE.minus(Duration.between(oldestRequest, now)); | ||
| throw new BaseException( | ||
| String.format("๋๋ฌด ๋ง์ ์์ฒญ์ ๋๋ค. %d์ด ํ์ ๋ค์ ์๋ํด์ฃผ์ธ์.",waitTime.getSeconds()), | ||
| HttpStatus.TOO_MANY_REQUESTS); | ||
| } | ||
|
|
||
| requests.add(now); | ||
| } | ||
| } | ||
18 changes: 18 additions & 0 deletions
18
src/main/java/com/mycom/socket/auth/service/data/VerificationData.java
This file contains hidden or 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,18 @@ | ||
| package com.mycom.socket.auth.service.data; | ||
|
|
||
| import java.time.LocalDateTime; | ||
|
|
||
| import java.time.Duration; | ||
|
|
||
| public record VerificationData(String code, LocalDateTime expiryTime) { | ||
|
|
||
| private static final Duration CODE_VALID_DURATION = Duration.ofMinutes(5); | ||
|
|
||
| public VerificationData(String code) { | ||
| this(code, LocalDateTime.now().plus(CODE_VALID_DURATION)); | ||
| } | ||
|
|
||
| public boolean isExpired() { | ||
| return LocalDateTime.now().isAfter(expiryTime); | ||
| } | ||
| } |
Oops, something went wrong.
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.
๐ ๏ธ Refactor suggestion
๋ฉ๋ชจ๋ฆฌ ๋์ ๊ฐ๋ฅ์ฑ ๊ฐ์ ํ์
requestMap์ ์ ์ฅ๋ ์ด๋ฉ์ผ ์ฃผ์์ ์์ฒญ ์๊ฐ์ด ์ง์์ ์ผ๋ก ๋์ ๋์ด ๋ฉ๋ชจ๋ฆฌ ๋์๊ฐ ๋ฐ์ํ ์ ์์ต๋๋ค. ์ฃผ๊ธฐ์ ์ผ๋ก ์ค๋๋ ๋ฐ์ดํฐ๋ฅผ ์ ๋ฆฌํ๋ ๋ฉ์ปค๋์ฆ์ด ํ์ํฉ๋๋ค.๋ค์๊ณผ ๊ฐ์ ๊ฐ์ ์ ์ ์ํฉ๋๋ค: