Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: email 전송 api 변경 #205

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ okhttpVersion=4.10.0
awsJavaSdkVersion=1.12.281
jasyptVersion=3.0.4
sonarqubeVersion=3.4.0.2513
jacocoVersion=0.8.8
jacocoVersion=0.8.8
awsSesVersion=1.12.188
1 change: 1 addition & 0 deletions module-api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ dependencies {
implementation("io.springfox:springfox-swagger2:2.9.2")

implementation "com.amazonaws:aws-java-sdk-s3:${awsJavaSdkVersion}"
implementation "com.amazonaws:aws-java-sdk-ses:${awsSesVersion}"

implementation 'com.github.siyoon210:ogparser4j:1.0.1'
implementation "org.jsoup:jsoup:${jsoupVersion}"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,40 @@
package inspiration.domain.emailauth;

import inspiration.email.AwsSesService;
import inspiration.email.GoogleService;
import inspiration.enumeration.ExceptionType;
import inspiration.exception.PostNotFoundException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import java.util.List;

@Service
@RequiredArgsConstructor
@Slf4j
public class ResetPasswordEmailSendService implements EmailSendService {

private final JavaMailSender mailSender;
private final static String SUBJECT = "비빌번호 초기화";
private final static String CREATED_RESET_PASSWORD = "임시 비밀번호 발급: ";
private final AwsSesService awsSesService;
private final GoogleService googleService;

@Override
public void send(String email, String restPassword) {

try {
SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
simpleMailMessage.setTo(email);
simpleMailMessage.setSubject(SUBJECT);
simpleMailMessage.setText(CREATED_RESET_PASSWORD + restPassword);
awsSesService.send(SUBJECT, CREATED_RESET_PASSWORD + restPassword, List.of(email));

mailSender.send(simpleMailMessage);
} catch (Exception e) {
log.debug(ExceptionType.FAILED_TO_SEND_MAIL.getMessage(), e.getMessage());
throw new PostNotFoundException(ExceptionType.FAILED_TO_SEND_MAIL.getMessage());
try {
googleService.send(SUBJECT, CREATED_RESET_PASSWORD + restPassword, List.of(email));
} catch (MessagingException ex) {
log.debug(ExceptionType.FAILED_TO_SEND_MAIL.getMessage(), ex.getMessage());
throw new PostNotFoundException(ExceptionType.FAILED_TO_SEND_MAIL.getMessage());
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,38 +1,44 @@
package inspiration.domain.emailauth;

import inspiration.email.AwsSesService;
import inspiration.email.GoogleService;
import inspiration.enumeration.ExceptionType;
import inspiration.exception.PostNotFoundException;
import inspiration.infrastructure.mail.MailProperties;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import java.util.List;

@Service
@RequiredArgsConstructor
@Slf4j
public class ResetPasswordForAuthSendService implements EmailSendService {

private final JavaMailSender mailSender;
private final MailProperties mailProperties;
private final static String SUBJECT = "비밀번호 초기화를 위한 이메일 인증";
private final static String OPEN_HREF = "<a href=";
private final static String CLOSE_HREF = "></a>";
private final AwsSesService awsSesService;
private final GoogleService googleService;

@Override
public void send(String email, String authToken) {

try {
SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
simpleMailMessage.setTo(email);
simpleMailMessage.setSubject(SUBJECT);
simpleMailMessage.setText(OPEN_HREF + mailProperties.getResetPasswordForAuthSendMail() + email + "&authToken=" + authToken + CLOSE_HREF);

mailSender.send(simpleMailMessage);
awsSesService.send(SUBJECT, OPEN_HREF + mailProperties.getResetPasswordForAuthSendMail() + email + "&authToken=" + authToken + CLOSE_HREF, List.of(email));

} catch (Exception e) {
log.debug(ExceptionType.FAILED_TO_SEND_MAIL.getMessage(), e.getMessage());
throw new PostNotFoundException(ExceptionType.FAILED_TO_SEND_MAIL.getMessage());
try {
googleService.send(SUBJECT, OPEN_HREF + mailProperties.getResetPasswordForAuthSendMail() + email + "&authToken=" + authToken + CLOSE_HREF, List.of(email));
} catch (MessagingException ex) {
log.debug(ExceptionType.FAILED_TO_SEND_MAIL.getMessage(), ex.getMessage());
throw new PostNotFoundException(ExceptionType.FAILED_TO_SEND_MAIL.getMessage());
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package inspiration.domain.emailauth;

import inspiration.email.AwsSesService;
import inspiration.email.GoogleService;
import inspiration.enumeration.ExceptionType;
import inspiration.exception.PostNotFoundException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.springframework.web.util.UriComponentsBuilder;

import javax.mail.internet.MimeMessage;
import javax.mail.MessagingException;
import java.util.List;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

Expand All @@ -18,8 +20,10 @@
@Slf4j
public class SignUpEmailSendService implements EmailSendService {

private final JavaMailSender mailSender;

private final static String SUBJECT = "이메일 인증";
private final AwsSesService awsSesService;
private final GoogleService googleService;

@Value("${ygtang.server.scheme}")
private String scheme;
Expand All @@ -28,6 +32,7 @@ public class SignUpEmailSendService implements EmailSendService {
@Value("${ygtang.server.port}")
private String port;


@Override
public void send(String email, String authToken) {

Expand All @@ -42,16 +47,15 @@ public void send(String email, String authToken) {
.toUriString();

try {
MimeMessage simpleMailMessage = mailSender.createMimeMessage();

simpleMailMessage.addRecipients(MimeMessage.RecipientType.TO, email);
simpleMailMessage.setSubject(SUBJECT);
simpleMailMessage.setText(setHtml(link), "utf-8", "html");

mailSender.send(simpleMailMessage);
awsSesService.send(SUBJECT, setHtml(link), List.of(email));
} catch (Exception e) {
log.debug(ExceptionType.FAILED_TO_SEND_MAIL.getMessage(), e.getMessage());
throw new PostNotFoundException(ExceptionType.FAILED_TO_SEND_MAIL.getMessage());
try {
googleService.send(SUBJECT, setHtml(link), List.of(email));
} catch (MessagingException ex) {
log.debug(ExceptionType.FAILED_TO_SEND_MAIL.getMessage(), ex.getMessage());
throw new PostNotFoundException(ExceptionType.FAILED_TO_SEND_MAIL.getMessage());
}
}
}

Expand Down
31 changes: 31 additions & 0 deletions module-api/src/main/java/inspiration/email/AwsSesService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package inspiration.email;

import com.amazonaws.services.simpleemail.AmazonSimpleEmailService;
import com.amazonaws.services.simpleemail.model.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
@Slf4j
public class AwsSesService implements EmailService {

private final AmazonSimpleEmailService amazonSimpleEmailService;

private static final String FROM_EMAIL = "yeonggamt@gmail.com";

//이메일 전송하기
public void send(String subject, String content, List<String> receivers) {

Destination destination = new Destination().withToAddresses(receivers);
Message message = new Message().withSubject(createContent(subject)).withBody(new Body().withHtml(createContent(content)));
amazonSimpleEmailService.sendEmail(new SendEmailRequest().withSource(FROM_EMAIL).withDestination(destination).withMessage(message));
}

private Content createContent(String text) {
return new Content().withCharset("UTF-8").withData(text);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package inspiration.email;

import javax.mail.MessagingException;
import java.util.List;

public interface EmailService {

public void send(String subject, String content, List<String> receivers) throws MessagingException;
}
37 changes: 37 additions & 0 deletions module-api/src/main/java/inspiration/email/GoogleService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package inspiration.email;

import inspiration.enumeration.ExceptionType;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.List;

@Service
@RequiredArgsConstructor
@Slf4j
public class GoogleService implements EmailService {

private final JavaMailSender mailSender;

@Override
public void send(String subject, String content, List<String> receivers) throws MessagingException {

MimeMessage simpleMailMessage = mailSender.createMimeMessage();

for (String emailRecipient : receivers) {
Address toAddress=new InternetAddress(emailRecipient);
simpleMailMessage.addRecipient(Message.RecipientType.TO,toAddress);
}
simpleMailMessage.setSubject(subject);
simpleMailMessage.setText(content, "utf-8", "html");

mailSender.send(simpleMailMessage);
}
}
2 changes: 2 additions & 0 deletions module-domain/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ dependencies {
annotationProcessor "com.querydsl:querydsl-apt:${dependencyManagement.importedProperties['querydsl.version']}:jpa"
annotationProcessor 'jakarta.persistence:jakarta.persistence-api'
annotationProcessor 'jakarta.annotation:jakarta.annotation-api'

implementation "com.amazonaws:aws-java-sdk-ses:${awsSesVersion}"
}
29 changes: 29 additions & 0 deletions module-domain/src/main/java/inspiration/aws/AwsSesConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package inspiration.aws;


import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.simpleemail.AmazonSimpleEmailService;
import com.amazonaws.services.simpleemail.AmazonSimpleEmailServiceClientBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AwsSesConfig {

@Value("${cloud.aws.ses.access-key}")
private String accessKey;

@Value("${cloud.aws.ses.secret-key}")
private String secretKey;

@Bean
public AmazonSimpleEmailService amazonSimpleEmailService() {
final BasicAWSCredentials basicAWSCredentials = new BasicAWSCredentials(accessKey, secretKey);

final AWSStaticCredentialsProvider awsStaticCredentialsProvider = new AWSStaticCredentialsProvider(basicAWSCredentials);

return AmazonSimpleEmailServiceClientBuilder.standard().withCredentials(awsStaticCredentialsProvider).withRegion("ap-northeast-2").build();
}
}
3 changes: 3 additions & 0 deletions module-domain/src/main/resources/application-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ cloud:
secret-key: ENC(YiKVUKqdW6IeY+zcvhSzhb65e1rVU5MFn174HDKFBb0xinizK9EwCsdj06ZAeEh7sAuWJ/DnI/w5/gC5bE4cRjO82NRIAT9aSVG2VVsemmc=)
s3:
bucket: ENC(hrPVDZuG83TgagLFUG7MchbMlA30RYJJU1sW8ToTYyUguoCcZiLVgtALJwQq6JRPZzfa2/WX6k0I2o/FiTurHA==)
ses:
access-key: ENC(ONeXLvOC69T9yj0Z1aECuoxIpTHA4rR7JKdYMtaio7rsvTWdYOIZoZcxRGDDFcDHv8MOH5gNapl+AfWnuLdxww==)
secret-key: ENC(5mbWA0xUqs/kKMOXT88QRvCh43GO3ECyzmXRxDcVCX/5XajI9Xvk6Mz1qW2oNc3CfC5p6GzxzjhSXmJ7muzHPon3rALCs93hYXNIJiYzUVo=)

ygtang:
domain:
Expand Down
5 changes: 5 additions & 0 deletions module-domain/src/main/resources/application-local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ spring:
properties:
hibernate:
format_sql: true
hibernate:
ddl-auto: update
flyway:
url: ENC(Qagc87PeOwmR8Zh73hU/bii+ZjzTPI7shAmw3pPxdLqXHF5MXACQparai0qWTUb6rBMXGPuqohNDhKfKx/wc21ysVuOCel9WEf2R0Le/RJhI8oVlaHZMmp6eq/yR2lsbW1ZB+n7zIi2gKgOTu2U9g4zRcA8EH3KTLK5+ps998vZRyCOIg8gW9/IJab5A8uSIgnUjwjJGdxAYcNd6+PMoceEhDfeW6wVldT7KNQeHhjofmi565uyWGQtHTfOQHbjcaBjH5CtI7/tWGaYVxdpLlC+d6kZ2wm/7H4rB0oGa9DZw7dy1JE4ymFcL67b6p2mEsi6ysBMZ+fTypM90Snb8RA==)
user: ENC(pD8lmKr4PXMRCUto6pMF9Ub/iJCxKveTODoqXQm1fDEB4rVN1Nzs6sftmidgYWOP)
Expand All @@ -18,6 +20,9 @@ cloud:
secret-key: ENC(YiKVUKqdW6IeY+zcvhSzhb65e1rVU5MFn174HDKFBb0xinizK9EwCsdj06ZAeEh7sAuWJ/DnI/w5/gC5bE4cRjO82NRIAT9aSVG2VVsemmc=)
s3:
bucket: ENC(hrPVDZuG83TgagLFUG7MchbMlA30RYJJU1sW8ToTYyUguoCcZiLVgtALJwQq6JRPZzfa2/WX6k0I2o/FiTurHA==)
ses:
access-key: ENC(ONeXLvOC69T9yj0Z1aECuoxIpTHA4rR7JKdYMtaio7rsvTWdYOIZoZcxRGDDFcDHv8MOH5gNapl+AfWnuLdxww==)
secret-key: ENC(5mbWA0xUqs/kKMOXT88QRvCh43GO3ECyzmXRxDcVCX/5XajI9Xvk6Mz1qW2oNc3CfC5p6GzxzjhSXmJ7muzHPon3rALCs93hYXNIJiYzUVo=)

ygtang:
domain:
Expand Down
3 changes: 3 additions & 0 deletions module-domain/src/main/resources/application-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ cloud:
secret-key: ENC(Ex+M59/bYV59b9OQy+ZTwHgdhsw/lzVBidiBzr5nyu8uXb6GrM1k+KfLusS0dTt38KzK8wKd9r5kvFU60ML1i5y061xyeRWunnBuvUWwXrQ=)
s3:
bucket: ENC(R9pmaGtILn87+LKOpZHuj7rrEMm+4zV7cUUWs08/kQyZqKI6dMKYTg6/ZpZZ2SkExhq+inG0Ar/9CUEIQmhvmQ==)
ses:
access-key: ENC(zx9Al4s2xHV7KK+NiRnrg/Vh0Fzf+yOySkKoK3Bnu+ub1+6qXUrZkKQEE8BsfMusAJu68GlKdB1PH3/PBoyooQ==)
secret-key: ENC(QRM7fc2nEakhcdw0eio2Mg1rvaCT+spuF4Si7VnuQvBFg4FjB/Pm4N0euTmHK3nu+/B1PhCKdt4YXLH13yVOIOMN7awJjYYmm5t3uLDrfOY=)

logging:
file:
Expand Down
2 changes: 2 additions & 0 deletions module-domain/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,7 @@ cloud:
s3:
connection-timeout: 1000
request-timeout: 3000


uri:
webhook:
3 changes: 3 additions & 0 deletions module-domain/src/test/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ cloud:
secret-key:
region:
static:
ses:
access-key:
secret-key:

logging:
pattern:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,19 @@
import inspiration.domain.emailauth.request.SendEmailRequest;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.view.RedirectView;

import javax.validation.Valid;
import java.time.LocalDateTime;

@RestController
@RequiredArgsConstructor
@RequestMapping("api/v1/auth")
@Slf4j
@SuppressWarnings("ClassCanBeRecord")
public class AuthController {

Expand All @@ -45,7 +48,9 @@ public ResultResponse<TokenResponse> login(
@ApiOperation(value = "회원가입을 위해 이메일 인증 링크 요청", notes = "회원가입을 위해 이메일에 인증 링크를 요청한다")
public void sendEmailForSignup(@RequestBody @Valid SendEmailRequest request) {

log.warn("Request Time {} ", LocalDateTime.now());
emailAuthService.signUpEmailSend(request.getEmail());
log.warn("Response Time {} ", LocalDateTime.now());
}

@PostMapping("/sends-email/passwords/reset")
Expand Down
3 changes: 3 additions & 0 deletions module-web/src/test/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ cloud:
static: ap-northeast-2
s3:
bucket:
ses:
access-key:
secret-key:

ygtang:
server:
Expand Down