From 8ee3b4b521a82c96c82c2a403a1d1b305d9dfedc Mon Sep 17 00:00:00 2001 From: suhoon Date: Sat, 6 Jan 2024 21:35:52 +0900 Subject: [PATCH] =?UTF-8?q?Feat:=20=ED=9A=8C=EC=9B=90=EB=B9=84=EB=B0=80?= =?UTF-8?q?=EB=B2=88=ED=98=B8=EC=88=98=EC=A0=95,=20=EC=9D=B4=EB=A9=94?= =?UTF-8?q?=EC=9D=BC=EC=88=98=EC=A0=95,=20=ED=9A=8C=EC=9B=90=EC=82=AD?= =?UTF-8?q?=EC=A0=9C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 2 + .../controller/MemberController.java | 41 ++++++++++-- .../dto/request/SearchPasswordDto.java | 20 ++++++ .../dto/response/BaseResponseStatus.java | 3 + .../repository/MemberRepository.java | 3 + .../StudyFlexBE/service/MemberService.java | 62 +++++++++++++++++-- src/main/resources/application.properties | 13 +++- 7 files changed, 135 insertions(+), 9 deletions(-) create mode 100644 src/main/java/com/umc/StudyFlexBE/dto/request/SearchPasswordDto.java diff --git a/build.gradle b/build.gradle index be232e2..370bdc8 100644 --- a/build.gradle +++ b/build.gradle @@ -45,6 +45,8 @@ dependencies { // school web mail implementation 'com.github.in-seo:univcert:master-SNAPSHOT' + // password mail + implementation 'org.springframework.boot:spring-boot-starter-mail' compileOnly 'org.projectlombok:lombok' diff --git a/src/main/java/com/umc/StudyFlexBE/controller/MemberController.java b/src/main/java/com/umc/StudyFlexBE/controller/MemberController.java index 63c7e94..06f1cce 100644 --- a/src/main/java/com/umc/StudyFlexBE/controller/MemberController.java +++ b/src/main/java/com/umc/StudyFlexBE/controller/MemberController.java @@ -1,9 +1,9 @@ package com.umc.StudyFlexBE.controller; -import com.umc.StudyFlexBE.config.jwt.JwtTokenProvider; import com.umc.StudyFlexBE.dto.request.CheckAuthCodeDto; import com.umc.StudyFlexBE.dto.request.LoginDto; +import com.umc.StudyFlexBE.dto.request.SearchPasswordDto; import com.umc.StudyFlexBE.dto.request.SendAuthCodeDto; import com.umc.StudyFlexBE.dto.request.SignUpDto; import com.umc.StudyFlexBE.dto.request.SignUpOAuthDto; @@ -21,13 +21,13 @@ import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; -import org.springframework.web.client.RestTemplate; @RestController @RequestMapping("app/member") @@ -35,8 +35,7 @@ public class MemberController { private final MemberService memberService; - private final RestTemplate restTemplate; - private final JwtTokenProvider jwtTokenProvider; + @Value("${mail.api.key}") private String mail_api_key; @@ -140,9 +139,43 @@ public BaseResponse checkAuthCode(@RequestBody CheckAuthCodeDto checkAuthCode return new BaseResponse<>(BaseResponseStatus.SUCCESS, "인증 코드 확인 완료."); } + @DeleteMapping("/deleteMember") + public BaseResponse deleteMember(String password) { + Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); + String email = authentication.getName(); + memberService.deleteMember(email); + return new BaseResponse<>(BaseResponseStatus.SUCCESS, "회원 삭제 완료."); + } + + + @GetMapping("/searchPassword") + public BaseResponse searchPassword(@RequestBody SearchPasswordDto searchPasswordDto) { + try { + memberService.sendPasswordMail(searchPasswordDto.getEmail(), searchPasswordDto.getPassword()); + return new BaseResponse<>(BaseResponseStatus.SUCCESS, "새로운 비밀번호 전송 완료."); + } catch (BaseException e) { + return new BaseResponse<>(BaseResponseStatus.CHANGE_PASSWORD_FAILED); + } + } + + @PostMapping("/changeEmail") + public BaseResponse changeEmail(String newEmail) { + try { + Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); + String email = authentication.getName(); + memberService.changeEmail(email , newEmail); + return new BaseResponse<>(BaseResponseStatus.SUCCESS, "이메일 변경 완료."); + } catch (BaseException e) { + return new BaseResponse<>(BaseResponseStatus.CHANGE_EMAIL_FAILED); + } + + } + + @GetMapping("testauth") @PreAuthorize("hasAnyRole('USER','ADMIN')") public BaseResponse test() { + Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); String email = authentication.getName(); System.out.println(email); diff --git a/src/main/java/com/umc/StudyFlexBE/dto/request/SearchPasswordDto.java b/src/main/java/com/umc/StudyFlexBE/dto/request/SearchPasswordDto.java new file mode 100644 index 0000000..ad42326 --- /dev/null +++ b/src/main/java/com/umc/StudyFlexBE/dto/request/SearchPasswordDto.java @@ -0,0 +1,20 @@ +package com.umc.StudyFlexBE.dto.request; + +import com.fasterxml.jackson.annotation.JsonProperty; +import jakarta.validation.constraints.NotNull; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +@Getter +@Setter +@NoArgsConstructor +public class SearchPasswordDto { + @JsonProperty + @NotNull + private String email; + + @JsonProperty + @NotNull + private String password; +} diff --git a/src/main/java/com/umc/StudyFlexBE/dto/response/BaseResponseStatus.java b/src/main/java/com/umc/StudyFlexBE/dto/response/BaseResponseStatus.java index f8b9f88..5c044d5 100644 --- a/src/main/java/com/umc/StudyFlexBE/dto/response/BaseResponseStatus.java +++ b/src/main/java/com/umc/StudyFlexBE/dto/response/BaseResponseStatus.java @@ -32,6 +32,9 @@ public enum BaseResponseStatus { SEND_EMAIL_FAILED(false,3005 ,"인증코드 요청 실패" ), WEB_MAIL_CODE_FAILED(false, 3006, "인증코드 불일치"), + MAIL_SEND_FAILED(false, 3007,"이메일 전송 실패" ), + CHANGE_EMAIL_FAILED(false,3008 ,"이메일 변경 실패" ), + CHANGE_PASSWORD_FAILED(false,3009 ,"비밀번호 변경 실패" ), /** * 4XXX : Post diff --git a/src/main/java/com/umc/StudyFlexBE/repository/MemberRepository.java b/src/main/java/com/umc/StudyFlexBE/repository/MemberRepository.java index 55b937c..0a2a0a7 100644 --- a/src/main/java/com/umc/StudyFlexBE/repository/MemberRepository.java +++ b/src/main/java/com/umc/StudyFlexBE/repository/MemberRepository.java @@ -9,4 +9,7 @@ public interface MemberRepository extends JpaRepository { Member findByEmail(String email); + Long deleteByEmail(String email); + + } diff --git a/src/main/java/com/umc/StudyFlexBE/service/MemberService.java b/src/main/java/com/umc/StudyFlexBE/service/MemberService.java index dda476f..e236cec 100644 --- a/src/main/java/com/umc/StudyFlexBE/service/MemberService.java +++ b/src/main/java/com/umc/StudyFlexBE/service/MemberService.java @@ -17,12 +17,15 @@ import com.umc.StudyFlexBE.entity.Member; import com.umc.StudyFlexBE.entity.OAuthProfile; import com.umc.StudyFlexBE.repository.MemberRepository; +import jakarta.mail.internet.MimeMessage; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.ResponseEntity; +import org.springframework.mail.javamail.JavaMailSender; +import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.core.Authentication; @@ -43,7 +46,7 @@ public class MemberService { private final AuthenticationManagerBuilder authenticationManagerBuilder; private final JwtTokenProvider jwtTokenProvider; private final RestTemplate restTemplate; - + private final JavaMailSender javaMailSender; public boolean checkEmail(String email) { @@ -107,8 +110,6 @@ public String login(LoginDto loginDto) { } - - public KaKaoOAuthToken getKakaoToken(String code) { try { HttpHeaders headers = new HttpHeaders(); @@ -173,4 +174,57 @@ public void certifyWebMail(String email, String school, String webEmail) { memberRepository.save(member); } -} + @Transactional + public void deleteMember(String email) { + + Long memberId = memberRepository.deleteByEmail(email); + + } + + + @Transactional + public void sendPasswordMail(String email, String password) { + MimeMessage mimeMessage = javaMailSender.createMimeMessage(); + MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, "utf-8"); + + try { + // 1. 메일 수신자 설정 + messageHelper.setTo(email); + // 2. 메일 제목 설정 + messageHelper.setSubject("[StudyFlex] Your New Password"); + // 3. 메일 내용 설정 + String mailContent = "[StudyFlex] Your new password is: " + password; + messageHelper.setText(mailContent, true); + // 4. 메일 전송 + javaMailSender.send(mimeMessage); + } catch (Exception e) { + throw new BaseException(BaseResponseStatus.MAIL_SEND_FAILED); + } + + Member member = memberRepository.findByEmail(email); + if (member == null) { + throw new BaseException(BaseResponseStatus.NO_SUCH_EMAIL); + } + member.setPassword(passwordEncoder.encode(password)); + memberRepository.save(member); + + + } + + + @Transactional + public void changeEmail(String email, String newEmail) { + + Member member = memberRepository.findByEmail(email); + if (member == null) { + throw new BaseException(BaseResponseStatus.NO_SUCH_EMAIL); + } + if(checkEmail(newEmail) == false){ + throw new BaseException(BaseResponseStatus.CHANGE_EMAIL_FAILED); + } + + member.setEmail(newEmail); + memberRepository.save(member); + + } +} \ No newline at end of file diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 7ee3ed9..80acfac 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -23,6 +23,17 @@ naver.redirect.url=http://localhost:8080/app/naver/callback -# mail api key +# univ mail api key mail.api.key=97895d00-0dbe-4a3f-9d8e-bbe19e662d04 + + + +# Mail Setting +spring.mail.host=smtp.gmail.com +spring.mail.port=587 +spring.mail.username=s4hoon +spring.mail.password=qsfc cdea ngeb nnno +spring.mail.properties.mail.smtp.starttls.enable=true +spring.mail.properties.mail.smtp.starttls.required=true +spring.mail.properties.mail.smtp.auth=true \ No newline at end of file