-
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.
- Loading branch information
Showing
4 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
src/main/java/com/hallym/rehab/domain/user/controller/SmtpController.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,28 @@ | ||
package com.hallym.rehab.domain.user.controller; | ||
|
||
import com.hallym.rehab.domain.user.dto.EmailDTO; | ||
import com.hallym.rehab.domain.user.service.SmtpGmailSenderImpl; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
@RestController | ||
@RequestMapping("/email") | ||
@RequiredArgsConstructor | ||
public class SmtpController { | ||
private final SmtpGmailSenderImpl senderService; | ||
|
||
@GetMapping("/send-code/{email}") | ||
public ResponseEntity<String> sendEmail(@PathVariable String email) { | ||
senderService.sendEmail(email); | ||
return ResponseEntity.ok("Email sent"); | ||
} | ||
|
||
@PostMapping(value = "/verify-code/{email}", consumes = "application/json") | ||
public ResponseEntity<String> sendEmailAndCode(@PathVariable String email, @RequestBody EmailDTO dto) { | ||
if (senderService.verifyAuthCode(email, dto.getAuthCode())) { | ||
return ResponseEntity.ok("Email verified"); | ||
} | ||
return ResponseEntity.badRequest().body("Email verification failed"); | ||
} | ||
} |
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.hallym.rehab.domain.user.dto; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class EmailDTO { | ||
private String authCode; | ||
} |
60 changes: 60 additions & 0 deletions
60
src/main/java/com/hallym/rehab/domain/user/service/SmtpGmailSenderImpl.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,60 @@ | ||
package com.hallym.rehab.domain.user.service; | ||
|
||
import com.hallym.rehab.global.util.RedisUtil; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.mail.SimpleMailMessage; | ||
import org.springframework.mail.javamail.JavaMailSender; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.Random; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class SmtpGmailSenderImpl implements SmtpGmailSenderService{ | ||
private final JavaMailSender emailSender; | ||
private final RedisUtil redisUtil; | ||
@Value("${spring.mail.username}") | ||
private String from; | ||
|
||
@Override | ||
public void sendEmail(String toEmail) { | ||
if(redisUtil.existData(toEmail)) { | ||
redisUtil.deleteData(toEmail); | ||
} | ||
|
||
String cipher = createCipher(); | ||
redisUtil.setDataExpire(toEmail, cipher, 60 * 5L); | ||
|
||
SimpleMailMessage message = new SimpleMailMessage(); | ||
message.setFrom(from); | ||
message.setTo(toEmail); | ||
message.setSubject("이메일 인증번호를 알려드립니다."); | ||
message.setText("5분안에 입력해야 합니다. \n" + "인증번호 : " + cipher); | ||
|
||
emailSender.send(message); | ||
} | ||
|
||
@Override | ||
public Boolean verifyAuthCode(String email, String authCode) { | ||
String authCodeFoundByEmail = redisUtil.getData(email); | ||
if (authCodeFoundByEmail == null) { | ||
return false; | ||
} | ||
return authCodeFoundByEmail.equals(authCode); | ||
} | ||
|
||
@Override | ||
public String createCipher() { | ||
int leftLimit = 48; // number '0' | ||
int rightLimit = 122; // alphabet 'z'ß | ||
int targetStringLength = 6; | ||
Random random = new Random(); | ||
|
||
return random.ints(leftLimit, rightLimit + 1) | ||
.filter(i -> (i <=57 || i >=65) && (i <= 90 || i>= 97)) | ||
.limit(targetStringLength) | ||
.collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append) | ||
.toString(); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/com/hallym/rehab/domain/user/service/SmtpGmailSenderService.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,7 @@ | ||
package com.hallym.rehab.domain.user.service; | ||
|
||
public interface SmtpGmailSenderService { | ||
void sendEmail(String toEmail); | ||
Boolean verifyAuthCode(String email, String authCode); | ||
String createCipher(); | ||
} |