Skip to content

Commit 221e1f5

Browse files
authored
Merge pull request #182 from SMU-SATTO/feature/#176
[feature] 임시 비밀번호 발급 / 이메일 변경 추가
2 parents 06e47ed + 2723705 commit 221e1f5

File tree

8 files changed

+108
-6
lines changed

8 files changed

+108
-6
lines changed

src/main/java/com/example/satto/domain/mail/controller/MailController.java

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.example.satto.domain.mail.dto.EmailRequestDTO;
44
import com.example.satto.domain.mail.service.EmailService;
55
import com.example.satto.domain.mail.service.Impl.EmailServiceImpl;
6+
import com.example.satto.domain.users.dto.UsersRequestDTO;
67
import com.example.satto.domain.users.service.UsersService;
78
import com.example.satto.global.common.BaseResponse;
89
import io.swagger.v3.oas.annotations.Operation;
@@ -13,7 +14,6 @@
1314
import org.springframework.web.bind.annotation.RestController;
1415

1516

16-
1717
@RestController
1818
@RequestMapping("/api/v1/users/id")
1919
@RequiredArgsConstructor
@@ -29,11 +29,11 @@ public class MailController {
2929
@PostMapping("/mail/check")
3030
public BaseResponse<Object> emailConfirm(@RequestBody EmailRequestDTO.EmailCheckRequest emailCheckRequest) throws Exception {
3131

32-
if (usersService.studentIdDuplicate(emailCheckRequest)){
32+
if (usersService.studentIdDuplicate(emailCheckRequest)) {
3333
return BaseResponse.onFailure("해당 학번으로 이미 가입하셨습니다.");
3434
} else {
3535

36-
String email = emailCheckRequest.getStudentId()+"@sangmyung.kr";
36+
String email = emailCheckRequest.getStudentId() + "@sangmyung.kr";
3737
String confirm;
3838
confirm = emailService.sendSimpleMessage(email);
3939
if (confirm.isEmpty()) {
@@ -45,15 +45,43 @@ public BaseResponse<Object> emailConfirm(@RequestBody EmailRequestDTO.EmailCheck
4545
}
4646
}
4747

48+
@Operation(summary = "임시 비밀번호 전송",
49+
description = "샘물 이메일 주소로 임시 비밀번호가 전송된다.")
50+
@PostMapping("/findPw")
51+
public BaseResponse<Object> findPw(@RequestBody EmailRequestDTO.EmailCheckRequest emailCheckRequest) throws Exception {
52+
if (usersService.studentIdDuplicate(emailCheckRequest)) {
53+
String email = emailCheckRequest.getStudentId() + "@sangmyung.kr";
54+
String confirm;
55+
confirm = emailService.sendFindPwMessage(email);
56+
if (confirm.isEmpty()) {
57+
return BaseResponse.onFailure("인증번호 전송 실패");
58+
} else {
59+
// UpdateUserPasswordDTO 객체 생성
60+
UsersRequestDTO.UpdateUserPasswordDTO updateUserPasswordDTO = new UsersRequestDTO.UpdateUserPasswordDTO();
61+
// password 필드에 confirm 값 설정
62+
updateUserPasswordDTO.setPassword(confirm);
63+
64+
Long userId = usersService.findId(email);
65+
// DTO와 userId를 resetPassword 메서드에 전달
66+
usersService.resetPassword(updateUserPasswordDTO, userId);
67+
return BaseResponse.onSuccess("인증번호 전송 성공");
68+
}
69+
} else {
70+
return BaseResponse.onFailure("존재하지 않는 이메일 입니다.");
71+
}
72+
}
73+
74+
4875
// 인증번호 확인
4976
@Operation(summary = "인증번호 확입",
5077
description = "이메일로 받은 인증번호를 입력한다.")
5178
@PostMapping("/mail/authentication")
5279
public BaseResponse<Object> emailAuthentication(@RequestBody EmailRequestDTO.EmailAuthRequest emailAuthRequest) {
53-
if (emailAuthRequest.getCertificationNum().equals(EmailServiceImpl.ePw)){
80+
if (emailAuthRequest.getCertificationNum().equals(EmailServiceImpl.ePw)) {
5481
return BaseResponse.onSuccess("인증번호가 일치합니다.");
5582
} else {
5683
return BaseResponse.onFailure("인증번호가 일치하지 않습니다.");
5784
}
5885
}
5986
}
87+

src/main/java/com/example/satto/domain/mail/dto/EmailRequestDTO.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,9 @@ public static class EmailAuthRequest {
1313
public static class EmailCheckRequest {
1414
private String studentId; // 학번
1515
}
16+
17+
@Getter
18+
public static class FindPwRequest {
19+
private String email; // 학번
20+
}
1621
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package com.example.satto.domain.mail.service;
22

3+
import com.example.satto.domain.users.entity.Users;
4+
35
public interface EmailService {
46
String sendSimpleMessage(String to)throws Exception;
7+
8+
String sendFindPwMessage(String toEmail) throws Exception;
59
}

src/main/java/com/example/satto/domain/mail/service/Impl/EmailServiceImpl.java

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.example.satto.domain.mail.service.Impl;
22

33
import com.example.satto.domain.mail.service.EmailService;
4+
import com.example.satto.domain.users.entity.Users;
45
import jakarta.mail.internet.InternetAddress;
56
import jakarta.mail.internet.MimeMessage;
67
import org.springframework.mail.MailException;
@@ -20,6 +21,7 @@ public class EmailServiceImpl implements EmailService {
2021
private final JavaMailSender emailSender;
2122

2223
public static final String ePw = createKey();
24+
public static final String findePw = createKey();
2325

2426
public EmailServiceImpl(JavaMailSender emailSender) {
2527
this.emailSender = emailSender;
@@ -47,7 +49,7 @@ private MimeMessage createMessage(String to)throws Exception{
4749
msgg+= ePw+"</strong><div><br/> ";
4850
msgg+= "</div>";
4951
message.setText(msgg, "utf-8", "html");//내용
50-
message.setFrom(new InternetAddress("rhythmpal0219@gmail.com","hyun"));//보내는 사람
52+
message.setFrom(new InternetAddress("satto202409@gmail.com","satto"));//보내는 사람
5153

5254
return message;
5355
}
@@ -88,4 +90,48 @@ public String sendSimpleMessage(String to)throws Exception {
8890
}
8991
return ePw;
9092
}
93+
94+
@Override
95+
public String sendFindPwMessage(String toEmail)throws Exception {
96+
// TODO Auto-generated method stub
97+
MimeMessage message = findPwMessage(toEmail);
98+
try{//예외처리
99+
emailSender.send(message);
100+
}catch(MailException es){
101+
es.printStackTrace();
102+
throw new IllegalArgumentException();
103+
}
104+
return findePw;
105+
}
106+
107+
108+
private MimeMessage findPwMessage(String toEmail) throws Exception {
109+
System.out.println("보내는 대상 : "+ toEmail);
110+
System.out.println("인증 번호 : "+findePw);
111+
112+
MimeMessage message = emailSender.createMimeMessage();
113+
((MimeMessage) message).addRecipients(MimeMessage.RecipientType.TO, toEmail);//보내는 대상
114+
message.setSubject("임시 비밀번호 발급");//제목
115+
116+
String msgg="";
117+
msgg+= "<div style='margin:15px;'>";
118+
msgg+= "<h1> SATTO </h1>";
119+
msgg+= "<br>";
120+
msgg+= "<p>아래 임시 비밀번호를 복사해 입력해주세요<p>";
121+
msgg+= "<br>";
122+
msgg+= "<p>감사합니다.<p>";
123+
msgg+= "<br>";
124+
msgg+= "<div align='center' style='border:1px solid black; font-family:verdana';>";
125+
msgg+= "<h3 style='color:blue;'>임시 비밀번호 코드입니다.</h3>";
126+
msgg+= "<div style='font-size:130%'>";
127+
msgg+= "CODE : <strong>";
128+
msgg+= findePw+"</strong><div><br/> ";
129+
msgg+= "</div>";
130+
message.setText(msgg, "utf-8", "html");//내용
131+
message.setFrom(new InternetAddress("[email protected]","satto"));//보내는 사람
132+
133+
return message;
134+
135+
}
136+
91137
}

src/main/java/com/example/satto/domain/users/dto/UsersRequestDTO.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.example.satto.domain.users.dto;
22

33
import lombok.Getter;
4+
import lombok.Setter;
45
import org.springframework.aop.ClassFilter;
56

67
public class UsersRequestDTO {
@@ -9,12 +10,13 @@ public class UsersRequestDTO {
910
public static class UpdateUserDTO {
1011
private String name;
1112
private String nickname;
12-
private String profileImg;
13+
private String password;
1314
private String department;
1415
private int grade;
1516
}
1617

1718
@Getter
19+
@Setter
1820
public static class UpdateUserPasswordDTO {
1921
private String password;
2022
}

src/main/java/com/example/satto/domain/users/repository/UsersRepository.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import com.example.satto.domain.users.entity.Users;
44
import com.example.satto.domain.users.repository.querydsl.UsersRepositoryCustom;
55
import org.springframework.data.jpa.repository.JpaRepository;
6+
import org.springframework.data.jpa.repository.Query;
7+
import org.springframework.data.repository.query.Param;
68
import org.springframework.stereotype.Repository;
79

810
import java.util.Optional;
@@ -23,6 +25,8 @@ public interface UsersRepository extends JpaRepository<Users, Long>, UsersReposi
2325

2426
Users findByStudentId(Users studentId);
2527

28+
@Query("SELECT u.userId FROM Users u WHERE u.email = :email")
29+
Long findIdByEmail(@Param("email") String email);
2630
void deleteProfileImgByStudentId(String studentId);
2731
}
2832

src/main/java/com/example/satto/domain/users/service/Impl/UsersServiceImpl.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ public Users updateAccount(UsersRequestDTO.UpdateUserDTO updateUserDTO, Long use
143143

144144
user.setName(updateUserDTO.getName());
145145
user.setNickname(updateUserDTO.getNickname());
146+
user.setPassword(passwordEncoder.encode(updateUserDTO.getPassword()));
146147
user.setDepartment(updateUserDTO.getDepartment());
147148
user.setGrade(updateUserDTO.getGrade());
148149

@@ -207,7 +208,9 @@ public Users userProfile(String studentId) {
207208
public void resetPassword(UsersRequestDTO.UpdateUserPasswordDTO updateUserPasswordDTO, Long userId) {
208209
Users user = usersRepository.findById(userId)
209210
.orElseThrow(() -> new UsersHandler(ErrorStatus._NOT_FOUND_USER));
211+
System.out.println("받아온 비번: " +updateUserPasswordDTO.getPassword());
210212
user.setPassword(passwordEncoder.encode(updateUserPasswordDTO.getPassword()));
213+
System.out.println("암호화 된 비번: " +passwordEncoder.encode(updateUserPasswordDTO.getPassword()));
211214
usersRepository.save(user);
212215
}
213216

@@ -235,6 +238,11 @@ public List<Map<String, String>> searchUserByName(String name) {
235238
return afterMap;
236239
}
237240

241+
@Override
242+
public Long findId(String email) {
243+
Long userId = usersRepository.findIdByEmail(email);
244+
return userId;
245+
238246
@Transactional
239247
@Override
240248
public void saveProfile(MultipartFile file, String email) {
@@ -272,6 +280,7 @@ public void deleteProfileImage(String studentId) {
272280
private String extractKeyFromUrl(String url) {
273281
// URL에서 S3 key 추출 (폴더 포함)
274282
return url.substring(url.indexOf(s3Config.getPath2()));
283+
275284
}
276285

277286
}

src/main/java/com/example/satto/domain/users/service/UsersService.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,11 @@ public interface UsersService {
5151

5252
List<Map<String, String>> searchUserByName(String query);
5353

54+
55+
Long findId(String email);
56+
5457
void saveProfile(MultipartFile multipartFile, String email);
5558

5659
void deleteProfileImage(String studentId);
60+
5761
}

0 commit comments

Comments
 (0)