rest controller practice : send mail

This commit is contained in:
haerong22
2021-03-21 20:31:56 +09:00
parent cad04d75ee
commit 3213d8e24f
10 changed files with 6161 additions and 2 deletions

View File

@@ -4,6 +4,7 @@ build/
!gradle/wrapper/gradle-wrapper.jar
!**/src/main/**/build/
!**/src/test/**/build/
src/main/resources/application.yml
### STS ###
.apt_generated

View File

@@ -27,6 +27,7 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-mail'
implementation("org.springframework.boot:spring-boot-starter-validation")

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,46 @@
package com.example.restcontroller.common;
import com.example.restcontroller.common.exception.BizException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.stereotype.Component;
import javax.mail.internet.InternetAddress;
@Slf4j
@RequiredArgsConstructor
@Component
public class MailComponent {
private final JavaMailSender javaMailSender;
public boolean send(String fromEmail, String fromName, String toEmail, String toName, String title, String contents) {
MimeMessagePreparator mimeMessagePreparator = mimeMessage -> {
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
InternetAddress from = new InternetAddress();
from.setAddress(fromEmail);
from.setPersonal(fromName);
InternetAddress to = new InternetAddress();
to.setAddress(toEmail);
to.setPersonal(toName);
mimeMessageHelper.setFrom(from);
mimeMessageHelper.setTo(to);
mimeMessageHelper.setSubject(title);
mimeMessageHelper.setText(contents, true);
};
try {
javaMailSender.send(mimeMessagePreparator);
} catch (Exception e) {
log.info(e.getMessage());
throw new BizException("이메일 전송 실패");
}
return true;
}
}

View File

@@ -10,6 +10,8 @@ public class WebMvcConfiguration implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new CommonInterceptor());
registry.addInterceptor(new CommonInterceptor())
.addPathPatterns("/api/*")
.excludePathPatterns("/api/public/*");
}
}

View File

@@ -23,6 +23,7 @@ import com.example.restcontroller.user.exception.UserNotFoundException;
import com.example.restcontroller.user.model.*;
import com.example.restcontroller.user.repository.UserRepository;
import com.example.restcontroller.user.service.UserPointService;
import com.example.restcontroller.user.service.UserService;
import com.example.restcontroller.util.JWTUtils;
import com.example.restcontroller.util.PasswordUtils;
import lombok.RequiredArgsConstructor;
@@ -51,6 +52,7 @@ public class ApiUserController {
private final BoardService boardService;
private final UserPointService userPointService;
private final UserService userService;
@PostMapping("/api/user")
public ResponseEntity<?> chapter2_1(@RequestBody @Valid UserInput userInput, BindingResult bindingResult) {
@@ -403,4 +405,11 @@ public class ApiUserController {
return ResponseResult.result(result);
}
@PostMapping("/api/public/user")
public ResponseEntity<?> chapter5_2(@RequestBody UserInput userInput) {
ServiceResult result = userService.addUser(userInput);
return ResponseResult.result(result);
}
}

View File

@@ -23,4 +23,6 @@ public interface UserService {
ServiceResult deleteInterestUser(Long id, String email);
User login(UserLogin userLogin);
ServiceResult addUser(UserInput userInput);
}

View File

@@ -2,8 +2,8 @@ package com.example.restcontroller.user.service;
import com.example.restcontroller.board.model.ServiceResult;
import com.example.restcontroller.common.MailComponent;
import com.example.restcontroller.common.exception.BizException;
import com.example.restcontroller.logs.service.LogService;
import com.example.restcontroller.user.entity.User;
import com.example.restcontroller.user.entity.UserInterest;
import com.example.restcontroller.user.entity.UserStatus;
@@ -32,6 +32,8 @@ public class UserServiceImpl implements UserService {
private final UserCustomRepository userCustomRepository;
private final UserInterestRepository userInterestRepository;
private final MailComponent mailComponent;
@Override
public UserSummary getUserStatusCount() {
Long usingUserCount = userRepository.countByStatus(UserStatus.USING);
@@ -144,4 +146,38 @@ public class UserServiceImpl implements UserService {
return userEntity;
}
@Transactional
@Override
public ServiceResult addUser(UserInput userInput) {
Optional<User> optionalUserEntity = userRepository.findByEmail(userInput.getEmail());
if (optionalUserEntity.isPresent()) {
throw new BizException("이미 가입된 이메일 입니다");
}
String encryptPassword = PasswordUtils.encryptedPassword(userInput.getPassword());
User user = User.builder()
.email(userInput.getEmail())
.userName(userInput.getUserName())
.regDate(LocalDateTime.now())
.password(encryptPassword)
.phone(userInput.getPhone())
.status(UserStatus.USING)
.build();
userRepository.save(user);
// 이메일 전송
String fromEmail = "test.email.12588";
String fromName = "관리자";
String toEmail = userInput.getEmail();
String toName = userInput.getUserName();
String title = "회원가입을 축하드립니다.";
String contents = "회원가입을 축하드립니다.";
mailComponent.send(fromEmail, fromName, toEmail, toName, title, contents);
return ServiceResult.success();
}
}

View File

@@ -6,6 +6,10 @@ import org.springframework.security.crypto.bcrypt.BCrypt;
@UtilityClass
public class PasswordUtils {
public static String encryptedPassword(String password) {
return BCrypt.hashpw(password, BCrypt.gensalt());
}
public static boolean equalPassword(String password, String encryptedPassword) {
return BCrypt.checkpw(password, encryptedPassword);
}

View File

@@ -33,6 +33,18 @@ spring:
mustache:
suffix: .html
mail:
username: test.email.12588
password: jqvozceuwvacbxwa
host: smtp.gmail.com
port: 587
properties:
mail:
smtp:
auth: true
starttls:
enable: true
logging:
level:
org.hibernate.SQL: trace