Skip to content

Commit

Permalink
[FEATURE] E4-S10 사용자에게 4자리의 코드번호 메일로 전달 #285
Browse files Browse the repository at this point in the history
  • Loading branch information
HyeJeongIm committed Jan 24, 2022
1 parent 73565ba commit ddb67bc
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 2 deletions.
1 change: 1 addition & 0 deletions backend/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ dependencies {
testImplementation 'org.springframework.security:spring-security-test'
implementation 'com.sun.xml.bind:jaxb1-impl:2.2.5.1'
annotationProcessor "org.springframework.boot:spring-boot-configuration-processor"
implementation 'org.springframework.boot:spring-boot-starter-mail'
}

test {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ public void addCorsMappings(CorsRegistry registry) {
"https://vuelogin.choicloudlab.com",
"http://localhost:8070",
"https://ciat-dev.choicloudlab.com",
"https://ciat.choicloudlab.com"
"https://ciat.choicloudlab.com",
"https://smtp.naver.com"

)
.allowedMethods("*");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public void addCorsMappings(CorsRegistry registry) {
"http://localhost:8070",
"https://ciat-frontend.choicloudlab.com",
"https://ciat-dev.choicloudlab.com",
"https://ciat.choicloudlab.com"
"https://ciat.choicloudlab.com",
"https://smtp.naver.com"
)
.allowedMethods("*");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
package com.infp.ciat.user.controller;

import com.infp.ciat.config.auth.PrincipalDetails;
import com.infp.ciat.user.controller.dto.request.ResetPasswordRequestDTO;
import com.infp.ciat.user.controller.dto.request.SignupRequestDTO;
import com.infp.ciat.user.controller.dto.response.LoginSuccessResponse;
import com.infp.ciat.user.controller.dto.response.ResetPasswordResponse;
import com.infp.ciat.user.controller.dto.response.SignUpResponse;
import com.infp.ciat.user.service.AccountService;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import javax.mail.internet.MimeMessage;
import javax.validation.Valid;
import java.util.Random;

@RestController
@RequiredArgsConstructor
Expand All @@ -21,6 +29,11 @@
public class AccountController {
private final AccountService accountService;

private final JavaMailSender javaMailSender;

@Value("${spring.mail.username}")
private String fromMail;

@PostMapping("/signup")
public ResponseEntity<SignUpResponse> signUp(@Valid @RequestBody SignupRequestDTO requestDTO) {

Expand All @@ -39,4 +52,34 @@ public ResponseEntity<SignUpResponse> signUp(@Valid @RequestBody SignupRequestDT
public ResponseEntity<LoginSuccessResponse> login_success(@AuthenticationPrincipal PrincipalDetails user) {
return new ResponseEntity<>(new LoginSuccessResponse(user.getUsername()), HttpStatus.OK);
}

@SneakyThrows
@PostMapping("/resetPassword")
public ResponseEntity<ResetPasswordResponse> resetPassword(@RequestBody ResetPasswordRequestDTO requestDTO) {

String toMail = requestDTO.getEmail();

MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
mimeMessageHelper.setFrom(fromMail);
mimeMessageHelper.setTo(toMail);
mimeMessageHelper.setSubject("[ciat] 임시 비밀번호 안내");

Random random = new Random();
String originCode = "";
for (int i = 0; i < 4; i++) {
originCode += random.nextInt(10);
}

StringBuilder body = new StringBuilder();

body.append("3시간 안에 코드를 입력해주세요.");
body.append("코드: " + originCode);

mimeMessageHelper.setText(body.toString(), true);
javaMailSender.send(mimeMessage);

return new ResponseEntity<>(new ResetPasswordResponse(toMail, originCode), HttpStatus.OK);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.infp.ciat.user.controller.dto.request;

import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@NoArgsConstructor
@Getter
public class ResetPasswordRequestDTO {

private String email;

@Builder
public ResetPasswordRequestDTO(String email) {
this.email = email;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.infp.ciat.user.controller.dto.response;

import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@NoArgsConstructor
@Getter
public class ResetPasswordResponse {

private String email;

private String originCode;

@Builder
public ResetPasswordResponse(String email, String originCode) {
this.email = email;
this.originCode = originCode;
}
}

0 comments on commit ddb67bc

Please sign in to comment.