
--- src/main/java/com/takensoft/common/verify/service/Impl/EmailServiceImpl.java
+++ src/main/java/com/takensoft/common/verify/service/Impl/EmailServiceImpl.java
... | ... | @@ -1,5 +1,7 @@ |
1 | 1 |
package com.takensoft.common.verify.service.Impl; |
2 | 2 |
|
3 |
+import com.fasterxml.jackson.core.JsonProcessingException; |
|
4 |
+import com.fasterxml.jackson.databind.ObjectMapper; |
|
3 | 5 |
import com.takensoft.common.exception.*; |
4 | 6 |
import com.takensoft.common.util.JWTUtil; |
5 | 7 |
import com.takensoft.common.verify.dao.EmailDAO; |
... | ... | @@ -7,6 +9,7 @@ |
7 | 9 |
import com.takensoft.common.verify.vo.EmailVO; |
8 | 10 |
import lombok.RequiredArgsConstructor; |
9 | 11 |
import org.egovframe.rte.fdl.cmmn.EgovAbstractServiceImpl; |
12 |
+import org.springframework.beans.factory.annotation.Autowired; |
|
10 | 13 |
import org.springframework.beans.factory.annotation.Qualifier; |
11 | 14 |
import org.springframework.beans.factory.annotation.Value; |
12 | 15 |
import org.springframework.dao.DataAccessException; |
... | ... | @@ -15,6 +18,8 @@ |
15 | 18 |
import org.springframework.mail.javamail.JavaMailSender; |
16 | 19 |
import org.springframework.stereotype.Service; |
17 | 20 |
import org.springframework.transaction.annotation.Transactional; |
21 |
+ |
|
22 |
+import java.time.Duration; |
|
18 | 23 |
|
19 | 24 |
|
20 | 25 |
/** |
... | ... | @@ -36,7 +41,8 @@ |
36 | 41 |
private final EmailDAO emailDAO; |
37 | 42 |
private final JWTUtil jwtUtil; |
38 | 43 |
private final JavaMailSender mailSender; |
39 |
- @Qualifier("redisTemplateObject") |
|
44 |
+// @Autowired |
|
45 |
+// @Qualifier("redisTemplateObject") |
|
40 | 46 |
private final RedisTemplate<String, Object> redisTemplate; |
41 | 47 |
|
42 | 48 |
@Value("${spring.mail.verifyTime}") |
... | ... | @@ -49,28 +55,23 @@ |
49 | 55 |
* @param emailVO - 이메일 정보 |
50 | 56 |
* @return boolean - 이메일 인증코드 발송 결과 |
51 | 57 |
* @throws CustomEmailSendFailException - 이메일 발송 실패 시 |
52 |
- * @throws DataAccessException - db 관련 예외 발생 시 |
|
53 | 58 |
* @throws Exception - 그 외 예외 발생 시 |
54 | 59 |
* |
55 | 60 |
* 이메일 인증코드 발송 |
56 | 61 |
*/ |
57 | 62 |
@Override |
58 |
- @Transactional(rollbackFor = Exception.class) |
|
59 |
- public boolean sendEmailVerifyCode(EmailVO emailVO){ |
|
63 |
+ public boolean sendEmailVerifyCode(EmailVO emailVO) { |
|
60 | 64 |
try { |
61 | 65 |
String email = emailVO.getEmail(); |
62 | 66 |
String code = createRandomCode(); // 인증코드 생성 |
63 | 67 |
emailVO.setCode(code); |
64 |
- long createdAt = System.currentTimeMillis(); // 현재 시간(millis) |
|
65 |
- emailVO.setCreatedAt(createdAt); |
|
68 |
+ long currentAt = System.currentTimeMillis(); // 현재 시간(millis) |
|
69 |
+ emailVO.setCreatedAt(currentAt); |
|
66 | 70 |
|
67 | 71 |
boolean isSend = redisTemplate.hasKey("email:" + email); // 이메일 인증코드 발송여부 확인 |
68 | 72 |
|
69 |
- if(isSend) { // 이미 인증코드가 발송된 경우 |
|
70 |
- EmailVO verifyVO = (EmailVO) redisTemplate.opsForValue().get("email:" + email); // 발송된 인증코드 |
|
71 |
- if(createdAt - verifyVO.getCreatedAt() > verifyTime) { // 인증코드 유효시간이 지났을 경우 |
|
72 |
- redisTemplate.delete("email:" + email); // 인증코드 삭제 |
|
73 |
- } |
|
73 |
+ if (isSend) { // 이미 인증코드가 발송된 경우 |
|
74 |
+ redisTemplate.delete("email:" + email); // 인증코드 삭제 |
|
74 | 75 |
} |
75 | 76 |
|
76 | 77 |
// 이메일 발송 |
... | ... | @@ -84,10 +85,8 @@ |
84 | 85 |
throw new CustomEmailSendFailException("이메일 발송에 실패했습니다."); |
85 | 86 |
} |
86 | 87 |
|
87 |
- redisTemplate.opsForValue().set("email:" + email, emailVO, storeTime); // 인증코드 저장 |
|
88 |
+ redisTemplate.opsForValue().set("email:" + email, emailVO, Duration.ofMillis(storeTime)); // 인증코드 저장 |
|
88 | 89 |
return true; |
89 |
- } catch (DataAccessException dae) { |
|
90 |
- throw dae; |
|
91 | 90 |
} catch (Exception e) { |
92 | 91 |
redisTemplate.delete("email:" + emailVO.getEmail()); // 실패시 인증코드 삭제 |
93 | 92 |
throw e; |
... | ... | @@ -100,24 +99,22 @@ |
100 | 99 |
* @throws CustomEmailVerifyExpireException - 이메일 인증 만료 시 |
101 | 100 |
* @throws CustomEmailCodeNotMatchException - 이메일 인증코드 불일치 시 |
102 | 101 |
* @throws CustomEmailVerifyFailException - 이메일 인증 실패 시 |
103 |
- * @throws DataAccessException - db 관련 예외 발생 시 |
|
104 | 102 |
* @throws Exception - 그 외 예외 발생 시 |
105 | 103 |
* |
106 | 104 |
* 이메일 인증코드 확인 |
107 | 105 |
*/ |
108 | 106 |
@Override |
109 |
- @Transactional(rollbackFor = Exception.class) |
|
110 | 107 |
public boolean checkEmailVerifyCode(EmailVO emailVO){ |
111 | 108 |
try { |
112 | 109 |
String email = emailVO.getEmail(); |
113 | 110 |
String code = emailVO.getCode(); |
114 |
- long createdAt = System.currentTimeMillis(); // 현재 시간(millis) |
|
111 |
+ long currentAt = System.currentTimeMillis(); // 현재 시간(millis) |
|
115 | 112 |
|
116 | 113 |
boolean isSend = redisTemplate.hasKey("email:" + email); // 이메일 인증코드 발송여부 확인 |
117 | 114 |
|
118 | 115 |
if(isSend) { // 이미 인증코드가 발송된 경우 |
119 | 116 |
EmailVO verifyVO = (EmailVO) redisTemplate.opsForValue().get("email:" + email); // 발송된 인증코드 |
120 |
- if(createdAt - verifyVO.getCreatedAt() > verifyTime) { // 인증코드 유효시간이 지났을 경우 |
|
117 |
+ if(currentAt - verifyVO.getCreatedAt() > verifyTime) { // 인증코드 유효시간이 지났을 경우 |
|
121 | 118 |
throw new CustomEmailVerifyExpireException("인증 시간이 만료되었습니다."); |
122 | 119 |
} |
123 | 120 |
String verifyCode = verifyVO.getCode(); // 발송된 인증코드 |
... | ... | @@ -130,8 +127,6 @@ |
130 | 127 |
throw new CustomEmailVerifyFailException("이메일 인증에 실패했습니다."); |
131 | 128 |
} |
132 | 129 |
return true; |
133 |
- } catch (DataAccessException dae) { |
|
134 |
- throw dae; |
|
135 | 130 |
} catch (Exception e) { |
136 | 131 |
throw e; |
137 | 132 |
} |
--- src/main/resources/message/messages_en.yml
+++ src/main/resources/message/messages_en.yml
... | ... | @@ -57,4 +57,12 @@ |
57 | 57 |
|
58 | 58 |
# 파일 관련 |
59 | 59 |
file: |
60 |
- upload_fail: "File upload failed."(파일 끝에 줄바꿈 문자 없음) |
|
60 |
+ upload_fail: "File upload failed." |
|
61 |
+ |
|
62 |
+# 이메일 인증 관련 |
|
63 |
+email: |
|
64 |
+ send_fail: "Failed to send email." |
|
65 |
+ verify_success: "Email verification completed successfully." |
|
66 |
+ verify_expired: "Email verification has expired." |
|
67 |
+ verify_fail: "Email verification failed." |
|
68 |
+ code_not_match: "verification code does not match."(파일 끝에 줄바꿈 문자 없음) |
Add a comment
Delete comment
Once you delete this comment, you won't be able to recover it. Are you sure you want to delete this comment?