-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: email로 비밀번호 재설정
- Loading branch information
Showing
14 changed files
with
1,814 additions
and
10 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
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,15 @@ | ||
package com.mlog.config; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@Getter | ||
@Setter | ||
@ConfigurationProperties(prefix = "mail") | ||
public class MailConfigure { | ||
private String root; | ||
private String senderEmail; | ||
} |
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
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
31 changes: 31 additions & 0 deletions
31
src/main/java/com/mlog/user/controller/PasswordResetController.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,31 @@ | ||
package com.mlog.user.controller; | ||
|
||
import com.mlog.user.service.PasswordResetService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.*; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
|
||
import static com.mlog.util.ApiUtils.ApiResult; | ||
import static com.mlog.util.ApiUtils.success; | ||
|
||
@Controller | ||
@RequiredArgsConstructor | ||
public class PasswordResetController { | ||
|
||
private final PasswordResetService passwordResetService; | ||
|
||
// 비밀번호 재설정 요청 | ||
@PostMapping("/user/password-reset-request") | ||
@ResponseBody | ||
public ApiResult<Boolean> passwordResetRequest(@RequestParam String email) { | ||
return success(passwordResetService.sendPasswordResetMail(email)); | ||
} | ||
|
||
// 비밀번호 재설정 토큰 검증 및 비밀번호 재설정 | ||
@PostMapping("/user/reset-password") | ||
public String resetPassword(@RequestParam String token, @RequestParam String newPassword) { | ||
passwordResetService.resetPassword(token, newPassword); | ||
return "password-reset-success"; | ||
} | ||
} |
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
85 changes: 85 additions & 0 deletions
85
src/main/java/com/mlog/user/service/PasswordResetService.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,85 @@ | ||
package com.mlog.user.service; | ||
|
||
import com.mlog.config.MailConfigure; | ||
import com.mlog.error.UnauthorizedException; | ||
import com.mlog.user.repository.UserMapper; | ||
import jakarta.mail.MessagingException; | ||
import jakarta.mail.internet.MimeMessage; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.mail.javamail.JavaMailSender; | ||
import org.springframework.security.crypto.password.PasswordEncoder; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.Base64; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class PasswordResetService { | ||
|
||
private final JavaMailSender javaMailSender; | ||
private final UserMapper userMapper; | ||
private final PasswordEncoder passwordEncoder; | ||
private final MailConfigure mailConfigure; | ||
private final Map<String, String> tokenStore = new HashMap<>(); | ||
|
||
public Boolean sendPasswordResetMail(String email) { | ||
userMapper.findByEmail(email) | ||
.orElseThrow(() -> new UnauthorizedException("not valid email")); | ||
String token = generateToken(); | ||
tokenStore.put(token, email); | ||
MimeMessage message = createPasswordResetMail(email, token); | ||
javaMailSender.send(message); | ||
return true; | ||
} | ||
|
||
private String generateToken() { | ||
UUID uuid = UUID.randomUUID(); | ||
String uuidAsString = uuid.toString(); | ||
return Base64.getUrlEncoder().withoutPadding().encodeToString(uuidAsString.getBytes()); | ||
} | ||
|
||
private MimeMessage createPasswordResetMail(String email, String token) { | ||
MimeMessage message = javaMailSender.createMimeMessage(); | ||
|
||
try { | ||
message.setFrom(mailConfigure.getSenderEmail()); | ||
message.setRecipients(MimeMessage.RecipientType.TO, email); | ||
message.setSubject("M-LOG 계정 비밀번호 재설정 요청"); | ||
String body = ""; | ||
body += "<div style=\"font-family: Arial, sans-serif; padding: 20px; background-color: #f4f4f4;\">"; | ||
body += "<div style=\"max-width: 600px; margin: auto; background-color: white; padding: 20px; border-radius: 10px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);\">"; | ||
body += "<img src=\"https://m-log-photo-s3.s3.ap-southeast-2.amazonaws.com/26e1ca9d-bf7f-41ee-89cb-103a7379270b.png\" style=\"display: block; margin: 0 auto 20px; width: 100px;\">"; | ||
body += "<h2 style=\"color: #333; text-align: center;\">비밀번호 재설정 요청</h2>"; // 제목 색상 수정 | ||
body += "<p>안녕하세요,</p>"; | ||
body += "<p>M-LOG 계정 비밀번호 재설정을 요청하셨습니다. 아래 양식을 사용하여 비밀번호를 재설정하세요!</p>"; | ||
body += "<form action=\"" + mailConfigure.getRoot() + "/user/reset-password\" method=\"post\" target=\"_blank\" style=\"background-color: #f9f9f9; padding: 20px; border: 1px solid #ddd; border-radius: 5px;\">"; | ||
body += "<input type=\"hidden\" name=\"token\" value=\"" + token + "\">"; | ||
body += "<label for=\"newPassword\" style=\"display: block; margin-bottom: 10px; font-weight: bold; color: #333;\">새 비밀번호:</label>"; | ||
body += "<input type=\"password\" id=\"newPassword\" name=\"newPassword\" style=\"width: 90%; padding: 10px; margin-bottom: 20px; border: 1px solid #ccc; border-radius: 5px;\">"; | ||
body += "<button type=\"submit\" style=\"background-color: #333; color: white; padding: 10px 20px; border: none; border-radius: 5px; cursor: pointer; width: 100%;\">비밀번호 재설정</button>"; | ||
body += "</form>"; | ||
body += "<p style=\"margin-top: 20px;\">감사합니다,<br>M-LOG 팀</p>"; | ||
body += "</div>"; | ||
body += "</div>"; | ||
|
||
message.setText(body, "UTF-8", "html"); | ||
} catch (MessagingException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
return message; | ||
} | ||
|
||
public Boolean resetPassword(String token, String newPassword) { | ||
String email = tokenStore.get(token); | ||
if (email == null) { | ||
throw new UnauthorizedException("not valid email"); | ||
} | ||
userMapper.updatePassword(email, passwordEncoder.encode(newPassword)); | ||
tokenStore.remove(token); | ||
return true; | ||
} | ||
} |
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 |
---|---|---|
@@ -1,11 +1,10 @@ | ||
spring: | ||
profiles: | ||
active: | ||
# - dev | ||
include: | ||
# - local | ||
- prod | ||
# | ||
|
||
mybatis: | ||
configuration: | ||
map-underscore-to-camel-case: true | ||
map-underscore-to-camel-case: true | ||
|
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
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
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
Oops, something went wrong.