diff --git a/backend/build.gradle b/backend/build.gradle index f18f491..62702e7 100644 --- a/backend/build.gradle +++ b/backend/build.gradle @@ -30,6 +30,10 @@ dependencies { runtimeOnly group: 'io.jsonwebtoken', name: 'jjwt-jackson', version: '0.11.5' runtimeOnly group: 'io.jsonwebtoken', name: 'jjwt-impl', version: '0.11.5' implementation group: 'io.jsonwebtoken', name: 'jjwt-api', version: '0.11.5' + implementation group: 'org.springframework.boot', name: 'spring-boot-starter-mail', version: '3.2.5' + implementation 'org.springframework.boot:spring-boot-starter-mail' + + implementation group: 'org.glassfish.jaxb', name: 'jaxb-runtime', version: '4.0.5' diff --git a/backend/src/main/java/com/edufocus/edufocus/user/controller/UserController.java b/backend/src/main/java/com/edufocus/edufocus/user/controller/UserController.java index 16e8b9f..e185b6c 100644 --- a/backend/src/main/java/com/edufocus/edufocus/user/controller/UserController.java +++ b/backend/src/main/java/com/edufocus/edufocus/user/controller/UserController.java @@ -34,7 +34,13 @@ public class UserController { userService.join(user); return ResponseEntity.ok("User registered successfully"); } + @PostMapping("/findpassword/{user_id}") + public ResponseEntity findpassword(@PathVariable("user_id") Long user_id) throws Exception { + userService.userCheck(user_id); + return ResponseEntity.ok("임시 비밀번호가 이메일로 전송되었습니다."); + + } // // @PostMapping("/login") // public ResponseEntity login(@RequestBody User user) { diff --git a/backend/src/main/java/com/edufocus/edufocus/user/model/entity/MailDto.java b/backend/src/main/java/com/edufocus/edufocus/user/model/entity/MailDto.java new file mode 100644 index 0000000..b2e1e51 --- /dev/null +++ b/backend/src/main/java/com/edufocus/edufocus/user/model/entity/MailDto.java @@ -0,0 +1,20 @@ +package com.edufocus.edufocus.user.model.entity; + +import lombok.Data; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; +import lombok.ToString; + +@Getter +@Setter +@NoArgsConstructor +@ToString +@Data +public class MailDto { + + private String address; + private String title; + private String message; + +} \ No newline at end of file diff --git a/backend/src/main/java/com/edufocus/edufocus/user/model/entity/MemberChangeDto.java b/backend/src/main/java/com/edufocus/edufocus/user/model/entity/MemberChangeDto.java new file mode 100644 index 0000000..5576813 --- /dev/null +++ b/backend/src/main/java/com/edufocus/edufocus/user/model/entity/MemberChangeDto.java @@ -0,0 +1,19 @@ +package com.edufocus.edufocus.user.model.entity; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; +import lombok.ToString; + +@Getter +@Setter +@NoArgsConstructor +@AllArgsConstructor +@ToString +@Data +public class MemberChangeDto { + Long id; + String password; +} \ No newline at end of file diff --git a/backend/src/main/java/com/edufocus/edufocus/user/model/entity/User.java b/backend/src/main/java/com/edufocus/edufocus/user/model/entity/User.java index 8bc7952..f0403f8 100644 --- a/backend/src/main/java/com/edufocus/edufocus/user/model/entity/User.java +++ b/backend/src/main/java/com/edufocus/edufocus/user/model/entity/User.java @@ -2,6 +2,8 @@ package com.edufocus.edufocus.user.model.entity; import com.edufocus.edufocus.qna.entity.Qna; import jakarta.persistence.*; +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.Pattern; import lombok.AllArgsConstructor; import lombok.Getter; import lombok.NoArgsConstructor; @@ -25,7 +27,12 @@ public class User { @Column(name = "user_id", unique = true, nullable = false) + @Pattern(regexp = "(?=.*[0-9])(?=.*[a-zA-Z])(?=.*\\W)(?=\\S+$).{8,16}", message = "비밀번호는 8~16자 영문 대 소문자, 숫자, 특수문자를 사용하세요.") + @NotBlank(message = "아이디는 필수 입력 값입니다.") + private String userId; + + @Pattern(regexp = "^(?:\\w+\\.?)*\\w+@(?:\\w+\\.)+\\w+$", message = "이메일 형식이 올바르지 않습니다.") private String email; private String password; @Enumerated(EnumType.STRING) // 혹은 EnumType.ORDINAL diff --git a/backend/src/main/java/com/edufocus/edufocus/user/model/repository/UserRepository.java b/backend/src/main/java/com/edufocus/edufocus/user/model/repository/UserRepository.java index 53e6289..026c4aa 100644 --- a/backend/src/main/java/com/edufocus/edufocus/user/model/repository/UserRepository.java +++ b/backend/src/main/java/com/edufocus/edufocus/user/model/repository/UserRepository.java @@ -23,6 +23,12 @@ public interface UserRepository extends JpaRepository { @Query("UPDATE User u SET u.refreshToken = NULL WHERE u.id = :id") void deleteRefreshToken(@Param("id") Long id); + @Transactional + @Modifying + @Query("UPDATE User u set u.password = :password where u.id= :id") + void updatePassword(@Param("id") Long id , @Param("password") String password); + + Optional findByUserId(String userId); } diff --git a/backend/src/main/java/com/edufocus/edufocus/user/model/service/UserService.java b/backend/src/main/java/com/edufocus/edufocus/user/model/service/UserService.java index 3ba4454..4f3733a 100644 --- a/backend/src/main/java/com/edufocus/edufocus/user/model/service/UserService.java +++ b/backend/src/main/java/com/edufocus/edufocus/user/model/service/UserService.java @@ -9,4 +9,7 @@ public interface UserService { String getRefreshToken(Long id) throws Exception; void deleteRefreshToken(Long id) throws Exception; User userInfo(Long id) throws Exception; + void sendEamail(User user) throws Exception; + void userCheck(Long id) throws Exception; + } diff --git a/backend/src/main/java/com/edufocus/edufocus/user/model/service/UserServiceImpl.java b/backend/src/main/java/com/edufocus/edufocus/user/model/service/UserServiceImpl.java index 0f70838..2c39b06 100644 --- a/backend/src/main/java/com/edufocus/edufocus/user/model/service/UserServiceImpl.java +++ b/backend/src/main/java/com/edufocus/edufocus/user/model/service/UserServiceImpl.java @@ -1,12 +1,16 @@ package com.edufocus.edufocus.user.model.service; +import com.edufocus.edufocus.user.model.entity.MailDto; +import com.edufocus.edufocus.user.model.entity.MemberChangeDto; import com.edufocus.edufocus.user.model.entity.User; import com.edufocus.edufocus.user.model.exception.UserException; import com.edufocus.edufocus.user.model.repository.UserRepository; import jakarta.transaction.Transactional; import lombok.RequiredArgsConstructor; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.mail.SimpleMailMessage; +import org.springframework.mail.javamail.JavaMailSender; import org.springframework.stereotype.Service; import java.sql.SQLException; @@ -19,8 +23,10 @@ import java.util.Optional; @RequiredArgsConstructor public class UserServiceImpl implements UserService{ - @Autowired + private final UserRepository userRepository; + private final JavaMailSender mailSender; + public void join(User user) @@ -74,6 +80,74 @@ public class UserServiceImpl implements UserService{ } } + + @Override + public void sendEamail(User user) throws Exception { + MailDto mailDto = createMailAndChargePassword(user); + + System.out.println("이메일 전송 완료"); + SimpleMailMessage message = new SimpleMailMessage(); + + + + message.setTo(mailDto.getAddress()); + message.setFrom("passfinder111@gmail.com"); + message.setSubject(mailDto.getTitle()); + message.setText(mailDto.getMessage()); + System.out.println("!!!!!!!!!!!!!!!!!!"+ message); + + mailSender.send(message); + + + } + + public MailDto createMailAndChargePassword(User user) throws SQLException { + String str = getTempPassword(); + MailDto dto = new MailDto(); + dto.setAddress(user.getEmail()); + dto.setTitle(user.getUserId()+"님의 임시비밀번호 안내 이메일 입니다."); + dto.setMessage("안녕하세요. EduFoucs 입니다. "+ "\n"+ "임시비밀번호 안내 관련 메일 입니다." + "\n[" + user.getName() + "]" + "님의 임시 비밀번호는 " + + str + " 입니다."); + + System.out.println(dto); + + MemberChangeDto memberChangeDto = new MemberChangeDto(user.getId(),str); + System.out.println(memberChangeDto); + userRepository.updatePassword(memberChangeDto.getId(),memberChangeDto.getPassword()); + System.out.println(); + + return dto; + } + + @Override + public void userCheck(Long id) throws Exception { + + User user = userRepository.findById(id).orElse(null); + + + if(user == null) + { + System.out.println("불가"); + throw new RuntimeException("유효하지 않은 아이디입니다. 다시 입력하세요"); + + } + else { + + sendEamail(user); + } + } + public String getTempPassword() { + char[] charSet = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', + 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'}; + String str = ""; + + int idx = 0; + for (int i=0; i<10; i++) { + idx = (int) (charSet.length * Math.random()); + str += charSet[idx]; + } + return str; + } @Override public void saveRefreshToken(Long id, String refreshToken) throws Exception { userRepository.saveRefreshToken(id, refreshToken); diff --git a/backend/src/main/resources/application.properties b/backend/src/main/resources/application.properties index f06afe6..d24fac6 100644 --- a/backend/src/main/resources/application.properties +++ b/backend/src/main/resources/application.properties @@ -23,4 +23,15 @@ spring.mvc.pathmatch.matching-strategy=ant_path_matcher spring.jpa.database=mysql spring.jpa.hibernate.ddl-auto=create -spring.jpa.show-sql=true \ No newline at end of file +spring.jpa.show-sql=true + + +spring.mail.host=smtp.gmail.com +spring.mail.port=587 +#spring.mail.username=ssafytestpjt +#spring.mail.password=trpjbxqialufuzih +spring.mail.username=passfinder111@gmail.com +spring.mail.password=mnlyfkiprltjlsmw + +spring.mail.properties.mail.smtp.auth=true +spring.mail.properties.mail.smtp.starttls.enable=true