-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #163 from IT-Cotato/feature/162-implement-email-fu…
…nctions Feature: 이메일 관련 기능 구현(#162)
- Loading branch information
Showing
25 changed files
with
752 additions
and
30 deletions.
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
13 changes: 8 additions & 5 deletions
13
backend/src/main/java/middle_point_search/backend/BackendApplication.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
43 changes: 43 additions & 0 deletions
43
backend/src/main/java/middle_point_search/backend/common/email/EmailConfig.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,43 @@ | ||
package middle_point_search.backend.common.email; | ||
|
||
import java.util.Properties; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.mail.javamail.JavaMailSender; | ||
import org.springframework.mail.javamail.JavaMailSenderImpl; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import middle_point_search.backend.common.properties.EmailProperties; | ||
|
||
@Configuration | ||
@RequiredArgsConstructor | ||
public class EmailConfig { | ||
|
||
private final EmailProperties emailProperties; | ||
|
||
@Bean | ||
public JavaMailSender javaMailSender() { | ||
JavaMailSenderImpl mailSender = new JavaMailSenderImpl(); | ||
mailSender.setHost(emailProperties.getHost()); | ||
mailSender.setPort(emailProperties.getPort()); | ||
mailSender.setUsername(emailProperties.getUsername()); | ||
mailSender.setPassword(emailProperties.getPassword()); | ||
mailSender.setDefaultEncoding("UTF-8"); | ||
mailSender.setJavaMailProperties(getMailProperties()); | ||
|
||
return mailSender; | ||
} | ||
|
||
private Properties getMailProperties() { | ||
Properties properties = new Properties(); | ||
properties.put("mail.smtp.auth", true); | ||
properties.put("mail.smtp.starttls.enable", true); | ||
properties.put("mail.smtp.starttls.required", true); | ||
properties.put("mail.smtp.connectiontimeout", emailProperties.getConnectionTimeout()); | ||
properties.put("mail.smtp.timeout", emailProperties.getTimeout()); | ||
properties.put("mail.smtp.writetimeout", emailProperties.getWriteTimeout()); | ||
|
||
return properties; | ||
} | ||
} |
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
20 changes: 20 additions & 0 deletions
20
backend/src/main/java/middle_point_search/backend/common/properties/EmailProperties.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,20 @@ | ||
package middle_point_search.backend.common.properties; | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
@ConfigurationProperties(prefix = "mail") | ||
public class EmailProperties { | ||
|
||
private String host; | ||
private int port; | ||
private String username; | ||
private String password; | ||
private int connectionTimeout; | ||
private int timeout; | ||
private int writeTimeout; | ||
} |
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
18 changes: 0 additions & 18 deletions
18
...nd/src/main/java/middle_point_search/backend/common/util/encoder/PasswordEncoderUtil.java
This file was deleted.
Oops, something went wrong.
23 changes: 23 additions & 0 deletions
23
...ava/middle_point_search/backend/domains/email/domain/PasswordReissueVerificationCode.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,23 @@ | ||
package middle_point_search.backend.domains.email.domain; | ||
|
||
import static lombok.AccessLevel.*; | ||
|
||
import org.springframework.data.annotation.Id; | ||
import org.springframework.data.redis.core.RedisHash; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
@Getter | ||
@AllArgsConstructor | ||
@NoArgsConstructor(access = PROTECTED) | ||
@RedisHash(value = "passwordReissueVerificationCode", timeToLive = 10 * 60) | ||
public class PasswordReissueVerificationCode { | ||
|
||
@Id | ||
private String email; | ||
|
||
@Setter | ||
private String code; | ||
} |
24 changes: 24 additions & 0 deletions
24
...rc/main/java/middle_point_search/backend/domains/email/domain/SignupVerificationCode.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,24 @@ | ||
package middle_point_search.backend.domains.email.domain; | ||
|
||
import static lombok.AccessLevel.*; | ||
|
||
import org.springframework.data.annotation.Id; | ||
import org.springframework.data.redis.core.RedisHash; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
@NoArgsConstructor(access = PROTECTED) | ||
@RedisHash(value = "signupVerificationCode", timeToLive = 10 * 60) | ||
public class SignupVerificationCode { | ||
|
||
@Id | ||
private String email; | ||
|
||
@Setter | ||
private String code; | ||
} |
8 changes: 8 additions & 0 deletions
8
...nt_search/backend/domains/email/repository/PasswordReissueVerificationCodeRepository.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 middle_point_search.backend.domains.email.repository; | ||
|
||
import org.springframework.data.repository.CrudRepository; | ||
|
||
import middle_point_search.backend.domains.email.domain.PasswordReissueVerificationCode; | ||
|
||
public interface PasswordReissueVerificationCodeRepository extends CrudRepository<PasswordReissueVerificationCode, String> { | ||
} |
8 changes: 8 additions & 0 deletions
8
...iddle_point_search/backend/domains/email/repository/SignupVerificationCodeRepository.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 middle_point_search.backend.domains.email.repository; | ||
|
||
import org.springframework.data.repository.CrudRepository; | ||
|
||
import middle_point_search.backend.domains.email.domain.SignupVerificationCode; | ||
|
||
public interface SignupVerificationCodeRepository extends CrudRepository<SignupVerificationCode, String> { | ||
} |
39 changes: 39 additions & 0 deletions
39
backend/src/main/java/middle_point_search/backend/domains/email/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,39 @@ | ||
package middle_point_search.backend.domains.email.service; | ||
|
||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import middle_point_search.backend.domains.email.util.EmailUtil; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Transactional(readOnly = true) | ||
public class EmailService { | ||
|
||
private final EmailUtil emailUtil; | ||
|
||
private static final String EMAIL_VERIFICATION_TITLE = "SyncSpot 이메일 인증 코드입니다."; | ||
private static final String EMAIL_NEW_PASSWORD_TITLE = "SyncSpot 임시 비밀번호입니다."; | ||
private static final String EMAIL_VERIFICATION_NOTICE_TEXT = "인증 코드는 %s 입니다."; | ||
private static final String EMAIL_NEW_PASSWORD_NOTICE_TEXT = "임시 비밀번호는 %s 입니다. 로그인 후 비밀번호를 변경해주세요."; | ||
|
||
// 인증번호 이메일 보내기 | ||
public void sendVerificationCodeEmail(String email, String code) { | ||
String text = String.format(EMAIL_VERIFICATION_NOTICE_TEXT, code); | ||
emailUtil.sendEmail(email, EMAIL_VERIFICATION_TITLE, text); | ||
} | ||
|
||
// 비밀번호 재발급 인증번호 이메일 보내기 | ||
public void sendPasswordReissueVerificationCodeEmail(String email, String code) { | ||
String text = String.format(EMAIL_VERIFICATION_NOTICE_TEXT, code); | ||
emailUtil.sendEmail(email, EMAIL_VERIFICATION_TITLE, text); | ||
} | ||
|
||
// 새 비밀번호 이메일 보내기 | ||
public void sendNewPassword(String email, String newPassword) { | ||
String text = String.format(EMAIL_NEW_PASSWORD_NOTICE_TEXT, newPassword); | ||
|
||
emailUtil.sendEmail(email, EMAIL_NEW_PASSWORD_TITLE, text); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
...le_point_search/backend/domains/email/service/PasswordReissueVerificationCodeService.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,61 @@ | ||
package middle_point_search.backend.domains.email.service; | ||
|
||
import static middle_point_search.backend.common.exception.errorCode.UserErrorCode.*; | ||
|
||
import java.util.Random; | ||
|
||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import middle_point_search.backend.common.exception.CustomException; | ||
import middle_point_search.backend.domains.email.domain.PasswordReissueVerificationCode; | ||
import middle_point_search.backend.domains.email.repository.PasswordReissueVerificationCodeRepository; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Transactional(readOnly = true) | ||
public class PasswordReissueVerificationCodeService { | ||
|
||
private final PasswordReissueVerificationCodeRepository passwordReissueVerificationCodeRepository; | ||
|
||
@Transactional | ||
public void checkEmailCodeDuplicationAndSaveEmailCode(String email, String code) { | ||
PasswordReissueVerificationCode passwordReissueVerificationCode = passwordReissueVerificationCodeRepository | ||
.findById(email) | ||
.orElse(null); | ||
|
||
// 1. 이미 저장된 emailCode가 있으면 code만 변경 | ||
// 2. 없으면 새로 생성 | ||
if (passwordReissueVerificationCode != null) { | ||
passwordReissueVerificationCode.setCode(code); | ||
} else { | ||
passwordReissueVerificationCode = new PasswordReissueVerificationCode(email, code); | ||
} | ||
|
||
passwordReissueVerificationCodeRepository.save(passwordReissueVerificationCode); | ||
} | ||
|
||
// 인증 코드 생성 | ||
public String createVerificationCode() { | ||
Random random = new Random(); | ||
|
||
return String.format("%06d", random.nextInt(1000000)); // 000000부터 999999까지의 문자열 생성 | ||
} | ||
|
||
// 인증 코드 확인 | ||
public void verifyEmailCode(String email, String code) throws CustomException { | ||
PasswordReissueVerificationCode passwordReissueVerificationCode = passwordReissueVerificationCodeRepository.findById( | ||
email).orElse(null); | ||
|
||
if (passwordReissueVerificationCode == null) { | ||
throw CustomException.from(REQUIRE_VERIFICATION_REQUEST_FIRST); | ||
} | ||
|
||
if (!passwordReissueVerificationCode.getCode().equals(code)) { | ||
throw CustomException.from(VERIFICATION_CODE_NOT_MATCH); | ||
} | ||
|
||
passwordReissueVerificationCodeRepository.delete(passwordReissueVerificationCode); | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
...java/middle_point_search/backend/domains/email/service/SignupVerificationCodeService.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,64 @@ | ||
package middle_point_search.backend.domains.email.service; | ||
|
||
import static middle_point_search.backend.common.exception.errorCode.UserErrorCode.*; | ||
|
||
import java.util.Random; | ||
|
||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import middle_point_search.backend.common.exception.CustomException; | ||
import middle_point_search.backend.domains.email.domain.SignupVerificationCode; | ||
import middle_point_search.backend.domains.email.repository.SignupVerificationCodeRepository; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Transactional(readOnly = true) | ||
public class SignupVerificationCodeService { | ||
|
||
private final SignupVerificationCodeRepository signupVerificationCodeRepository; | ||
|
||
@Transactional | ||
public void checkEmailCodeDuplicationAndSaveEmailCode(String email, String code) { | ||
SignupVerificationCode signupVerificationCode = signupVerificationCodeRepository.findById(email).orElse(null); | ||
|
||
// 1. 이미 저장된 emailCode가 있으면 code만 변경 | ||
// 2. 없으면 새로 생성 | ||
if (signupVerificationCode != null) { | ||
signupVerificationCode.setCode(code); | ||
} else { | ||
signupVerificationCode = new SignupVerificationCode(email, code); | ||
} | ||
|
||
signupVerificationCodeRepository.save(signupVerificationCode); | ||
} | ||
|
||
// 인증 코드 생성 | ||
public String createVerificationCode() { | ||
Random random = new Random(); | ||
|
||
return String.format("%06d", random.nextInt(1000000)); // 000000부터 999999까지의 문자열 생성 | ||
} | ||
|
||
// 인증 코드 확인 | ||
public boolean verifyEmailCode(String email, String code) throws CustomException { | ||
SignupVerificationCode signupVerificationCode = signupVerificationCodeRepository.findById(email).orElse(null); | ||
|
||
if (signupVerificationCode == null) { | ||
throw CustomException.from(REQUIRE_VERIFICATION_REQUEST_FIRST); | ||
} | ||
|
||
return signupVerificationCode.getCode().equals(code); | ||
} | ||
|
||
// 인증 코드 판별 후 삭제 | ||
@Transactional | ||
public void validateEmailCodeAndDelete(String email, String code) { | ||
if (!verifyEmailCode(email, code)) { | ||
throw CustomException.from(VERIFICATION_CODE_NOT_MATCH); | ||
} | ||
|
||
signupVerificationCodeRepository.deleteById(email); | ||
} | ||
} |
Oops, something went wrong.