-
Notifications
You must be signed in to change notification settings - Fork 0
Feat/#10 #11
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
Feat/#10 #11
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
1b3551e
:sparkles: feat: Spring μλ²μμ μ΄λ©μΌ μ μ‘μ μν build.gradle μμ‘΄μ± μΆκ°
ojy0903 dcefec3
:sparkles: feat: μ΄λ©μΌ μΈμ¦μ½λλ₯Ό μ μ₯νκΈ° μν RedisUtil ν΄λμ€ μΆκ°
ojy0903 4de758a
:sparkles: feat: μ΄λ©μΌ μΈμ¦κ΄λ ¨ Request, Response DTO μΆκ°
ojy0903 11123c3
:sparkles: feat: μ΄λ©μΌ μΈμ¦ μ²λ¦¬νλ EmailService ν΄λμ€ μΆκ°
ojy0903 5e0259f
:sparkles: feat: μ΄λ©μΌ μΈμ¦ κ΄λ ¨ μμΈμ²λ¦¬ μ½λ UserErrorCodeμ μΆκ°
ojy0903 2b8e818
:sparkles: feat: κΈ°μ‘΄ νμκ°μ
λ‘μ§μμ μ΄λ©μΌ μΈμ¦λμ΄μΌ κ°μ
κ°λ₯νλλ‘ μμ
ojy0903 22101f5
:sparkles: feat: μΈμ¦μ½λ μ μ‘ API, μΈμ¦μ½λ κ²μ¦ API μΆκ°(/api/users/...)
ojy0903 db7cdc4
:sparkles: feat: SecurityConfig μμ λ‘κ·ΈμΈ μΈμ¦ μμ΄ μ΄λ©μΌ μΈμ¦ μ κ·Ό κ°λ₯νλλ‘ μμ
ojy0903 852dc4c
:bug: fix: test λλ ν°λ¦¬ application.yml μ€μ μΆκ° -> λΉλ ν
μ€νΈ ν΅κ³Ό λͺ©μ
ojy0903 dbb18f7
:bug: fix: test λλ ν°λ¦¬ application.yml μμ
ojy0903 da68b56
:sparkles: feat: .gitignore μμ application.yml 무μ μ€μ μ£Όμ μ²λ¦¬ & .env 무μ β¦
ojy0903 5634c1c
:sparkles: feat: application.yml μΆκ°, .env νμ μμ νμΌ .env.example μΆκ°
ojy0903 ba60c4f
:sparkles: feat: application.yml JWT μ΅μ
μ£Όμμ²λ¦¬
ojy0903 f9f14e7
:green_heart: build: ci.yml λ΄ env(νκ²½λ³μλͺ
) μμ
jinnieusLab b96059d
:green_heart: build: ci.yml λ΄ jwt secret μμ μΈμ½λ© κ°μΌλ‘ μμ
jinnieusLab a8a1b59
:recycle: refactor: μ΄λ©μΌ μΈμ¦ μμ² DTO λ₯Ό κΈ°μ‘΄ EmailRequest, EmailVerifyRequeβ¦
ojy0903 6b3dfec
:truck: rename: UserErrorCode λ΄λΆ USER_400_N μ½λ μ«μ μ λ ¬
ojy0903 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 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 @@ | ||
| # Database | ||
| DB_URL=jdbc:mysql://localhost:3306/whereyouad?serverTimezone=Asia/Seoul&characterEncoding=UTF-8 | ||
| DB_USERNAME=root | ||
| DB_PASSWORD= | ||
|
|
||
| # Redis | ||
| REDIS_HOST=localhost | ||
| REDIS_PORT=6379 | ||
|
|
||
| MAIL_USERNAME=your-email@gmail.com | ||
| MAIL_PASSWORD=email-app-password | ||
|
|
||
| # JWT | ||
| JWT_SECRET=your_very_long_and_secret_random_string_for_signing_tokens_to_match_HS512 |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,4 +36,5 @@ out/ | |
| ### VS Code ### | ||
| .vscode/ | ||
|
|
||
| src/main/resources/application.yml | ||
| #src/main/resources/application.yml | ||
| .env | ||
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
21 changes: 21 additions & 0 deletions
21
...ain/java/com/whereyouad/WhereYouAd/domains/user/application/dto/request/EmailRequest.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,21 @@ | ||
| package com.whereyouad.WhereYouAd.domains.user.application.dto.request; | ||
|
|
||
| import jakarta.validation.constraints.Email; | ||
| import jakarta.validation.constraints.NotBlank; | ||
|
|
||
| public class EmailRequest { | ||
|
|
||
| public record Send ( | ||
| @NotBlank(message = "μ΄λ©μΌμ νμμ λλ€.") | ||
| @Email(message = "μ΄λ©μΌ νμμ΄ μ¬λ°λ₯΄μ§ μμ΅λλ€.") | ||
| String email | ||
| ) {} | ||
|
|
||
| public record Verify( | ||
| @NotBlank(message = "μ΄λ©μΌμ νμμ λλ€.") | ||
| @Email(message = "μ΄λ©μΌ νμμ΄ μ¬λ°λ₯΄μ§ μμ΅λλ€.") | ||
| String email, | ||
| @NotBlank | ||
| String authCode | ||
| ) {} | ||
| } |
8 changes: 8 additions & 0 deletions
8
...va/com/whereyouad/WhereYouAd/domains/user/application/dto/response/EmailSentResponse.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,8 @@ | ||
| package com.whereyouad.WhereYouAd.domains.user.application.dto.response; | ||
|
|
||
| public record EmailSentResponse( | ||
| String message, //μΈμ¦μ½λλ₯Ό μ΄λ©μΌλ‘ μ μ‘νμ΅λλ€. | ||
| String email, //μ μ‘ν μ΄λ©μΌ | ||
| long expireIn //λ§λ£μκ° (500L -> 500μ΄) | ||
| ) { | ||
| } |
107 changes: 107 additions & 0 deletions
107
src/main/java/com/whereyouad/WhereYouAd/domains/user/domain/service/EmailService.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,107 @@ | ||
| package com.whereyouad.WhereYouAd.domains.user.domain.service; | ||
|
|
||
| import com.whereyouad.WhereYouAd.domains.user.application.dto.response.EmailSentResponse; | ||
| import com.whereyouad.WhereYouAd.domains.user.exception.UserSignUpException; | ||
| import com.whereyouad.WhereYouAd.domains.user.exception.code.UserErrorCode; | ||
| import com.whereyouad.WhereYouAd.domains.user.persistence.repository.UserRepository; | ||
| import com.whereyouad.WhereYouAd.global.utils.RedisUtil; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.mail.MailException; | ||
| import org.springframework.mail.SimpleMailMessage; | ||
| import org.springframework.mail.javamail.JavaMailSender; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| @Transactional | ||
| @Service | ||
| @RequiredArgsConstructor | ||
| @Slf4j | ||
| public class EmailService { | ||
|
|
||
| private final JavaMailSender emailSender; | ||
| private final RedisUtil redisUtil; | ||
| private final UserRepository userRepository; | ||
|
|
||
| //application.yml μ μ© νμ | ||
| @Value("${spring.mail.username}") | ||
| private String senderEmail; | ||
|
|
||
| //μΈμ¦μ½λ μ΄λ©μΌ λ°μ‘ λ‘μ§ | ||
| public EmailSentResponse sendEmail(String toEmail) { | ||
|
|
||
| if (userRepository.existsByEmail(toEmail)) { //μ΄λ―Έ ν΄λΉ μ΄λ©μΌλ‘ μμ±ν κ³μ μ΄ μμΌλ©΄ | ||
| throw new UserSignUpException(UserErrorCode.USER_EMAIL_DUPLICATE); //μ΄λ©μΌ μ€λ³΅ μμΈ(νμκ°μ μ μ¬μ©νλ μμΈ) | ||
| } | ||
|
|
||
| //μΈμ¦μ½λ μ¬μ μ‘ λ‘μ§ -> μ΄λ―Έ Redis μ ν΄λΉ μ΄λ©μΌ μΈμ¦μ½λκ° μμμ μμ | ||
| String redisKey = "CODE:" + toEmail; | ||
| if (redisUtil.getData(redisKey) != null) { | ||
| redisUtil.deleteData(redisKey); | ||
| } | ||
|
|
||
| String authCode = createCode(); //μΈμ¦μ½λ μμ± | ||
|
|
||
| if (isTestEmail(toEmail)) { | ||
| //ν μ€νΈμ© κ°μ§ μ΄λ©μΌμ μλ² λ‘κ·Έλ‘λ§ μΈμ¦μ½λλ₯Ό 보μ¬μ£ΌκΈ° | ||
| //μ€μ μ΄λ©μΌ λ°μ‘ X | ||
| //μ‘΄μ¬νμ§ μλ μ΄λ©μΌ μ£Όμλ‘ κ³μ μ΄λ©μΌμ 보λ΄λ©΄ μΈμ¦μ½λ μ μ‘μ© μ΄λ©μΌ κ³μ μ΄ μ€νΈμ²λ¦¬ λ κ°λ₯μ± μ‘΄μ¬νμ¬ λ‘κ·Έλ‘ λ¨κΈ°κΈ° | ||
| log.warn("{} μ΄λ©μΌ -> ν μ€νΈ or κ°λ°μ© κ°μ§ μ΄λ©μΌ μ λλ€.", toEmail); | ||
| log.warn("[TEST λͺ¨λ] μ€μ λ°μ‘μ 건λλλλ€."); | ||
| log.warn("[TEST λͺ¨λ] μμ μ: {}", toEmail); | ||
| log.warn("[TEST λͺ¨λ] μΈμ¦μ½λ: {}", authCode); | ||
|
|
||
| } else { //μ€μ μ‘΄μ¬νλ μ΄λ©μΌμ΄λΌλ©΄ | ||
|
|
||
| try { | ||
| //μ€μ μΈμ¦ μ½λκ° λ΄κΈ΄ μ΄λ©μΌ μ μ‘ | ||
| SimpleMailMessage message = new SimpleMailMessage(); | ||
| message.setTo(toEmail); | ||
| message.setSubject("whereyouad νμκ°μ μΈμ¦λ²νΈ"); | ||
| message.setText("μΈμ¦λ²νΈ: " + authCode); | ||
| message.setFrom(senderEmail); | ||
|
|
||
| emailSender.send(message); //λ§μ½ μ€μ μ‘΄μ¬νλ μ΄λ©μΌμΈλ° μ¬μ©μκ° μ€νλ₯Ό λλ€λ©΄ | ||
| } catch (MailException e) { //μμΈ λ°μ | ||
| throw new UserSignUpException(UserErrorCode.USER_EMAIL_NOT_VALID); //ν΅ν© μλ΅ μ²λ¦¬ μμΈλ‘ λ°ν | ||
| } | ||
|
|
||
| } | ||
|
|
||
| //Redisμ μ μ₯ (Key: "CODE:μ΄λ©μΌ", Value: "123456", μ ν¨μκ°: 300μ΄(5λΆ)) | ||
| //ν μ€νΈ κ³μ λ μΈμ¦μ ν΄μΌνλ Redis μ μ½λκ° μ μ₯ λμ΄μΌ ν¨. | ||
| //ν μ€νΈ κ³μ μ μΈμ¦μ μλ² λ‘κ·Έλ₯Ό ν΅ν΄ μΈμ¦μ½λλ₯Ό μ»μ΄ μ λ ₯. | ||
| redisUtil.setDataExpire("CODE:" + toEmail, authCode, 60 * 5L); | ||
|
|
||
| return new EmailSentResponse("μΈμ¦μ½λλ₯Ό μ΄λ©μΌλ‘ μ μ‘νμ΅λλ€.", toEmail, 300L); | ||
| } | ||
|
|
||
| //μΈμ¦μ½λ κ²μ¦ λ©μλ | ||
| public void verifyEmailCode(String email, String inputCode) { | ||
| //ν΄λΉ μ΄λ©μΌ κ°μΌλ‘ Redis μμ μ‘°ν | ||
| String key = "CODE:" + email; | ||
| String savedCode = redisUtil.getData(key); | ||
|
|
||
| //λ§μ½ μΈμ¦μ½λκ° μκ±°λ μλͺ» μ λ ₯νλ€λ©΄, | ||
| if (savedCode == null || !savedCode.equals(inputCode)) { | ||
| throw new UserSignUpException(UserErrorCode.USER_EMAIL_AUTH_INVALID); //μμΈ λ°μ(BAD_REQUEST) | ||
| } | ||
|
|
||
| //μ μμ μΌλ‘ μΈμ¦μ½λλ₯Ό μ λ ₯νλ€λ©΄, | ||
| //κΈ°μ‘΄ Redis μ μ μ₯λ λ°μ΄ν°λ₯Ό μ§μ°κ³ , | ||
| redisUtil.deleteData(key); | ||
| //"ν΄λΉ μ΄λ©μΌμ΄ μ μμ μΌλ‘ μΈμ¦λμλ€" λ κ°μ λ€μ Redis μ μ μ₯ -> μ΄ν νμκ°μ (signup) λ΄λΆ λ‘μ§μμ νμ© | ||
| redisUtil.setDataExpire("VERIFIED:" + email, "TRUE", 60 * 60L); //1μκ° λ€ Expire | ||
| } | ||
|
|
||
| //ν μ€νΈμ© μ΄λ©μΌμ, "test" λ‘ μμνκ±°λ, "example.com" μΌλ‘ λλμΌ νλ€. | ||
| private boolean isTestEmail(String email) { | ||
| return email.startsWith("test") || email.endsWith("example.com"); | ||
| } | ||
|
|
||
| //무μμ μΈμ¦μ½λ κ° μμ± | ||
| private String createCode() { | ||
| return String.valueOf((int)(Math.random() * (900000)) + 100000); | ||
| } | ||
| } | ||
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
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
33 changes: 33 additions & 0 deletions
33
src/main/java/com/whereyouad/WhereYouAd/global/utils/RedisUtil.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,33 @@ | ||
| package com.whereyouad.WhereYouAd.global.utils; | ||
|
|
||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.data.redis.core.StringRedisTemplate; | ||
| import org.springframework.data.redis.core.ValueOperations; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| import java.time.Duration; | ||
|
|
||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class RedisUtil { | ||
|
|
||
| private final StringRedisTemplate template; | ||
|
|
||
| //Redis μ λ°μ΄ν° μ μ₯(μ ν¨μκ° μ€μ ) | ||
| public void setDataExpire(String key, String value, long duration) { | ||
| ValueOperations<String, String> valueOperations = template.opsForValue(); | ||
| Duration expireDuration = Duration.ofSeconds(duration); | ||
| valueOperations.set(key, value, expireDuration); | ||
| } | ||
|
|
||
| //Redis μμ λ°μ΄ν° κΊΌλ΄κΈ°(Value κΊΌλ΄κΈ°) | ||
| public String getData(String key) { | ||
| ValueOperations<String, String> valueOperations = template.opsForValue(); | ||
| return valueOperations.get(key); | ||
| } | ||
|
|
||
| //Redis μμ λ°μ΄ν° μ§μ°κΈ°(key κ° κΈ°λ°) | ||
| public void deleteData(String key) { | ||
| template.delete(key); | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.