diff --git a/build.gradle b/build.gradle index 69f9d7a..e587790 100644 --- a/build.gradle +++ b/build.gradle @@ -45,6 +45,12 @@ dependencies { implementation 'com.google.oauth-client:google-oauth-client-jetty:1.23.0' implementation 'com.google.apis:google-api-services-youtube:v3-rev20230816-2.0.0' implementation 'com.google.http-client:google-http-client-jackson2:1.39.2' + + //mail 인증 + implementation 'org.springframework.boot:spring-boot-starter-mail' + + //redis 추가 + implementation 'org.springframework.boot:spring-boot-starter-data-redis' } tasks.named('test') { diff --git a/src/main/java/com/example/beginnerfitbe/email/EmailDto.java b/src/main/java/com/example/beginnerfitbe/email/EmailDto.java new file mode 100644 index 0000000..6d7129b --- /dev/null +++ b/src/main/java/com/example/beginnerfitbe/email/EmailDto.java @@ -0,0 +1,12 @@ +package com.example.beginnerfitbe.email; + +import lombok.AllArgsConstructor; +import lombok.Data; + +@Data +@AllArgsConstructor +public class EmailDto { + private String email; + private String code; +} + diff --git a/src/main/java/com/example/beginnerfitbe/email/config/EmailConfig.java b/src/main/java/com/example/beginnerfitbe/email/config/EmailConfig.java new file mode 100644 index 0000000..1e5680b --- /dev/null +++ b/src/main/java/com/example/beginnerfitbe/email/config/EmailConfig.java @@ -0,0 +1,68 @@ +package com.example.beginnerfitbe.email.config; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.mail.javamail.JavaMailSender; +import org.springframework.mail.javamail.JavaMailSenderImpl; + +import java.util.Properties; + +@Configuration +public class EmailConfig { + + @Value("${spring.mail.host}") + private String host; + + @Value("${spring.mail.port}") + private int port; + + @Value("${spring.mail.username}") + private String username; + + @Value("${spring.mail.password}") + private String password; + + @Value("${spring.mail.properties.mail.smtp.auth}") + private boolean auth; + + @Value("${spring.mail.properties.mail.smtp.starttls.enable}") + private boolean starttlsEnable; + + @Value("${spring.mail.properties.mail.smtp.starttls.required}") + private boolean starttlsRequired; + + @Value("${spring.mail.properties.mail.smtp.connectiontimeout}") + private int connectionTimeout; + + @Value("${spring.mail.properties.mail.smtp.timeout}") + private int timeout; + + @Value("${spring.mail.properties.mail.smtp.writetimeout}") + private int writeTimeout; + + @Bean + public JavaMailSender javaMailSender() { + JavaMailSenderImpl mailSender = new JavaMailSenderImpl(); + mailSender.setHost(host); + mailSender.setPort(port); + mailSender.setUsername(username); + mailSender.setPassword(password); + mailSender.setDefaultEncoding("UTF-8"); + mailSender.setJavaMailProperties(getMailProperties()); + + return mailSender; + } + + private Properties getMailProperties() { + Properties properties = new Properties(); + properties.put("mail.smtp.auth", auth); + properties.put("mail.smtp.starttls.enable", starttlsEnable); + properties.put("mail.smtp.starttls.required", starttlsRequired); + properties.put("mail.smtp.connectiontimeout", connectionTimeout); + properties.put("mail.smtp.timeout", timeout); + properties.put("mail.smtp.writetimeout", writeTimeout); + + return properties; + } +} \ No newline at end of file diff --git a/src/main/java/com/example/beginnerfitbe/email/controller/EmailController.java b/src/main/java/com/example/beginnerfitbe/email/controller/EmailController.java new file mode 100644 index 0000000..5d85a50 --- /dev/null +++ b/src/main/java/com/example/beginnerfitbe/email/controller/EmailController.java @@ -0,0 +1,28 @@ +package com.example.beginnerfitbe.email.controller; + +import com.example.beginnerfitbe.email.EmailDto; +import com.example.beginnerfitbe.email.service.EmailService; +import com.example.beginnerfitbe.user.service.UserService; +import lombok.RequiredArgsConstructor; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +@RestController +@RequiredArgsConstructor +@RequestMapping("/auth") +public class EmailController { + private final EmailService emailService; + private final UserService userService; + @PostMapping("/email-send") + public ResponseEntity> emailSend(@RequestParam String email) throws Exception { + if(userService.emailCheck(email)){ + return ResponseEntity.badRequest().body("이미 존재하는 회원입니다"); + } + return ResponseEntity.ok(emailService.sendEmail(email)); + } + @PostMapping("/email-verify") + public ResponseEntity> codeVerification(@RequestBody EmailDto emailDto){ + return ResponseEntity.ok(emailService.codeVerification(emailDto)); + } + +} diff --git a/src/main/java/com/example/beginnerfitbe/email/service/EmailService.java b/src/main/java/com/example/beginnerfitbe/email/service/EmailService.java new file mode 100644 index 0000000..9f0e49c --- /dev/null +++ b/src/main/java/com/example/beginnerfitbe/email/service/EmailService.java @@ -0,0 +1,102 @@ +package com.example.beginnerfitbe.email.service; + +import java.util.Random; + +import com.example.beginnerfitbe.email.EmailDto; +import com.example.beginnerfitbe.error.StateResponse; +import com.example.beginnerfitbe.redis.service.RedisService; +import jakarta.mail.internet.InternetAddress; +import jakarta.mail.internet.MimeMessage; +import lombok.RequiredArgsConstructor; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.mail.MailException; +import org.springframework.mail.javamail.JavaMailSender; +import org.springframework.stereotype.Service; + +@Service +@RequiredArgsConstructor +public class EmailService { + + private final JavaMailSender emailSender; + private final RedisService redisService; + + @Value("${spring.mail.username}") + private String fromEmail; + + private static final String ePw = createKey(); + + private MimeMessage createMessage(String to) throws Exception { + System.out.println("보내는 대상 : " + to); + System.out.println("인증 번호 : " + ePw); + MimeMessage message = emailSender.createMimeMessage(); + + message.addRecipients(MimeMessage.RecipientType.TO, to); // 보내는 대상 + message.setSubject("beginner fit sign-up"); // 제목 + + String msgg = ""; + msgg += "