rest controller practice : send email(reset user password)

This commit is contained in:
haerong22
2021-03-23 16:50:30 +09:00
parent 6b635ca397
commit b45c9f34b1
11 changed files with 784 additions and 939 deletions

File diff suppressed because it is too large Load Diff

View File

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

View File

@@ -31,6 +31,4 @@ public class MailTemplate {
private String sendUserName;
private LocalDateTime regDate;
}

View File

@@ -412,4 +412,15 @@ public class ApiUserController {
ServiceResult result = userService.addUser(userInput);
return ResponseResult.result(result);
}
@PostMapping("/api/public/user/password/reset")
public ResponseEntity<?> chapter5_3(@RequestBody @Valid UserPasswordResetInput userPasswordResetInput,
BindingResult bindingResult) {
if (bindingResult.hasFieldErrors()) {
return ResponseResult.fail("입력값이 정확하지 않습니다.", ResponseError.of(bindingResult.getFieldErrors()));
}
ServiceResult result = userService.resetPassword(userPasswordResetInput);
return ResponseResult.result(result);
}
}

View File

@@ -41,6 +41,10 @@ public class User {
@Column
private boolean lockYn;
private boolean passwordResetYn;
private String passwordResetKey;
private LocalDateTime regDate;
private LocalDateTime updateDate;

View File

@@ -0,0 +1,24 @@
package com.example.restcontroller.user.model;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class UserPasswordResetInput {
@Email(message = "이메일 형식이 아닙니다.")
@NotBlank(message = "이메일은 필수 입력사항입니다.")
private String email;
@NotBlank(message = "이름은 필수 입력사항입니다.")
private String userName;
}

View File

@@ -18,8 +18,8 @@ public interface UserRepository extends JpaRepository<User, Long> {
Optional<User> findByIdAndPassword(Long id, String password);
Optional<User> findByUserNameAndPhone(String userName, String phone);
Optional<User> findByEmail(String email);
Optional<User> findByEmailAndUserName(String email, String userName);
List<User> findByEmailContainsAndUserNameContainsAndPhoneContains(String email, String userName, String phone);

View File

@@ -27,4 +27,6 @@ public interface UserService {
ServiceResult addUser(UserInput userInput);
void sendServiceNotice();
ServiceResult resetPassword(UserPasswordResetInput userPasswordResetInput);
}

View File

@@ -24,6 +24,7 @@ import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;
@Slf4j
@@ -202,4 +203,32 @@ public class UserServiceImpl implements UserService {
});
}
@Transactional
@Override
public ServiceResult resetPassword(UserPasswordResetInput userPasswordResetInput) {
User userEntity = userRepository.findByEmailAndUserName(userPasswordResetInput.getEmail(), userPasswordResetInput.getUserName())
.orElseThrow(() -> new BizException("회원 정보가 존재하지 않습니다."));
String passwordResetKey = UUID.randomUUID().toString();
userEntity.setPasswordResetYn(true);
userEntity.setPasswordResetKey(passwordResetKey);
MailTemplate mailTemplate = mailTemplateRepository.findByTemplateId("USER_RESET_PASSWORD")
.orElseThrow(() -> new BizException("템플릿이 존재하지 않습니다."));
String serverUrl = "http://localhost:8080";
String title = mailTemplate.getTitle().replaceAll("\\{USER_NAME}", userEntity.getUserName());
String contents = mailTemplate.getContents()
.replaceAll("\\{USER_NAME}", userEntity.getUserName())
.replaceAll("\\{SERVER_URL}", serverUrl
.replaceAll("\\{RESET_PASSWORD_KEY}", passwordResetKey));
String fromEmail = mailTemplate.getSendEmail();
String fromUserName = mailTemplate.getSendUserName();
mailComponent.send(fromEmail, fromUserName, userEntity.getEmail(), userEntity.getUserName(), title, contents);
return ServiceResult.success();
}
}

View File

@@ -1,10 +1,10 @@
insert into user(email, password, phone, reg_date, user_name, status, lock_yn)
insert into user(email, password, phone, reg_date, user_name, status, lock_yn, password_reset_yn)
values
('haerong22@gmail.com', '$2a$10$2ElPbt2mwiAc9IYH3rgJz.YZInXlUd363utdyU0TWfne6Y3vKh8h6', '010-1234-1234', '2021-02-20 00:50:11.000000', 'kim', 'USING', 0),
('test22@test.com', '$2a$10$2ElPbt2mwiAc9IYH3rgJz.YZInXlUd363utdyU0TWfne6Y3vKh8h6', '010-4321-1111', '2021-02-25 12:33:16.000000', 'lee', 'USING', 0),
('test33@test.com', '$2a$10$2ElPbt2mwiAc9IYH3rgJz.YZInXlUd363utdyU0TWfne6Y3vKh8h6', '010-5555-3333', now(), 'hong', 'USING', 0),
('test44@test.com', '$2a$10$2ElPbt2mwiAc9IYH3rgJz.YZInXlUd363utdyU0TWfne6Y3vKh8h6', '010-4343-2546', now(), 'park', 'STOP', 0);
('haerong22@gmail.com', '$2a$10$2ElPbt2mwiAc9IYH3rgJz.YZInXlUd363utdyU0TWfne6Y3vKh8h6', '010-1234-1234', '2021-02-20 00:50:11.000000', 'kim', 'USING', 0, 0),
('test22@test.com', '$2a$10$2ElPbt2mwiAc9IYH3rgJz.YZInXlUd363utdyU0TWfne6Y3vKh8h6', '010-4321-1111', '2021-02-25 12:33:16.000000', 'lee', 'USING', 0, 0),
('test33@test.com', '$2a$10$2ElPbt2mwiAc9IYH3rgJz.YZInXlUd363utdyU0TWfne6Y3vKh8h6', '010-5555-3333', now(), 'hong', 'USING', 0, 0),
('test44@test.com', '$2a$10$2ElPbt2mwiAc9IYH3rgJz.YZInXlUd363utdyU0TWfne6Y3vKh8h6', '010-4343-2546', now(), 'park', 'STOP', 0, 0);
insert into notice(contents, reg_date, title, user_id)