feat: 회원가입 유효성검사

This commit is contained in:
박정민 2024-07-23 12:58:49 +09:00
parent 6f907e4ec5
commit 5f678beccc
9 changed files with 152 additions and 2 deletions

View File

@ -30,6 +30,10 @@ dependencies {
runtimeOnly group: 'io.jsonwebtoken', name: 'jjwt-jackson', version: '0.11.5' runtimeOnly group: 'io.jsonwebtoken', name: 'jjwt-jackson', version: '0.11.5'
runtimeOnly group: 'io.jsonwebtoken', name: 'jjwt-impl', version: '0.11.5' runtimeOnly group: 'io.jsonwebtoken', name: 'jjwt-impl', version: '0.11.5'
implementation group: 'io.jsonwebtoken', name: 'jjwt-api', version: '0.11.5' implementation group: 'io.jsonwebtoken', name: 'jjwt-api', version: '0.11.5'
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-mail', version: '3.2.5'
implementation 'org.springframework.boot:spring-boot-starter-mail'
implementation group: 'org.glassfish.jaxb', name: 'jaxb-runtime', version: '4.0.5' implementation group: 'org.glassfish.jaxb', name: 'jaxb-runtime', version: '4.0.5'

View File

@ -34,7 +34,13 @@ public class UserController {
userService.join(user); userService.join(user);
return ResponseEntity.ok("User registered successfully"); return ResponseEntity.ok("User registered successfully");
} }
@PostMapping("/findpassword/{user_id}")
public ResponseEntity<String> findpassword(@PathVariable("user_id") Long user_id) throws Exception {
userService.userCheck(user_id);
return ResponseEntity.ok("임시 비밀번호가 이메일로 전송되었습니다.");
}
// //
// @PostMapping("/login") // @PostMapping("/login")
// public ResponseEntity<User> login(@RequestBody User user) { // public ResponseEntity<User> login(@RequestBody User user) {

View File

@ -0,0 +1,20 @@
package com.edufocus.edufocus.user.model.entity;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
@Getter
@Setter
@NoArgsConstructor
@ToString
@Data
public class MailDto {
private String address;
private String title;
private String message;
}

View File

@ -0,0 +1,19 @@
package com.edufocus.edufocus.user.model.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
@Data
public class MemberChangeDto {
Long id;
String password;
}

View File

@ -2,6 +2,8 @@ package com.edufocus.edufocus.user.model.entity;
import com.edufocus.edufocus.qna.entity.Qna; import com.edufocus.edufocus.qna.entity.Qna;
import jakarta.persistence.*; import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Pattern;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
@ -25,7 +27,12 @@ public class User {
@Column(name = "user_id", unique = true, nullable = false) @Column(name = "user_id", unique = true, nullable = false)
@Pattern(regexp = "(?=.*[0-9])(?=.*[a-zA-Z])(?=.*\\W)(?=\\S+$).{8,16}", message = "비밀번호는 8~16자 영문 대 소문자, 숫자, 특수문자를 사용하세요.")
@NotBlank(message = "아이디는 필수 입력 값입니다.")
private String userId; private String userId;
@Pattern(regexp = "^(?:\\w+\\.?)*\\w+@(?:\\w+\\.)+\\w+$", message = "이메일 형식이 올바르지 않습니다.")
private String email; private String email;
private String password; private String password;
@Enumerated(EnumType.STRING) // 혹은 EnumType.ORDINAL @Enumerated(EnumType.STRING) // 혹은 EnumType.ORDINAL

View File

@ -23,6 +23,12 @@ public interface UserRepository extends JpaRepository<User,Long> {
@Query("UPDATE User u SET u.refreshToken = NULL WHERE u.id = :id") @Query("UPDATE User u SET u.refreshToken = NULL WHERE u.id = :id")
void deleteRefreshToken(@Param("id") Long id); void deleteRefreshToken(@Param("id") Long id);
@Transactional
@Modifying
@Query("UPDATE User u set u.password = :password where u.id= :id")
void updatePassword(@Param("id") Long id , @Param("password") String password);
Optional<User> findByUserId(String userId); Optional<User> findByUserId(String userId);
} }

View File

@ -9,4 +9,7 @@ public interface UserService {
String getRefreshToken(Long id) throws Exception; String getRefreshToken(Long id) throws Exception;
void deleteRefreshToken(Long id) throws Exception; void deleteRefreshToken(Long id) throws Exception;
User userInfo(Long id) throws Exception; User userInfo(Long id) throws Exception;
void sendEamail(User user) throws Exception;
void userCheck(Long id) throws Exception;
} }

View File

@ -1,12 +1,16 @@
package com.edufocus.edufocus.user.model.service; package com.edufocus.edufocus.user.model.service;
import com.edufocus.edufocus.user.model.entity.MailDto;
import com.edufocus.edufocus.user.model.entity.MemberChangeDto;
import com.edufocus.edufocus.user.model.entity.User; import com.edufocus.edufocus.user.model.entity.User;
import com.edufocus.edufocus.user.model.exception.UserException; import com.edufocus.edufocus.user.model.exception.UserException;
import com.edufocus.edufocus.user.model.repository.UserRepository; import com.edufocus.edufocus.user.model.repository.UserRepository;
import jakarta.transaction.Transactional; import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.sql.SQLException; import java.sql.SQLException;
@ -19,8 +23,10 @@ import java.util.Optional;
@RequiredArgsConstructor @RequiredArgsConstructor
public class UserServiceImpl implements UserService{ public class UserServiceImpl implements UserService{
@Autowired
private final UserRepository userRepository; private final UserRepository userRepository;
private final JavaMailSender mailSender;
public void join(User user) public void join(User user)
@ -74,6 +80,74 @@ public class UserServiceImpl implements UserService{
} }
} }
@Override
public void sendEamail(User user) throws Exception {
MailDto mailDto = createMailAndChargePassword(user);
System.out.println("이메일 전송 완료");
SimpleMailMessage message = new SimpleMailMessage();
message.setTo(mailDto.getAddress());
message.setFrom("passfinder111@gmail.com");
message.setSubject(mailDto.getTitle());
message.setText(mailDto.getMessage());
System.out.println("!!!!!!!!!!!!!!!!!!"+ message);
mailSender.send(message);
}
public MailDto createMailAndChargePassword(User user) throws SQLException {
String str = getTempPassword();
MailDto dto = new MailDto();
dto.setAddress(user.getEmail());
dto.setTitle(user.getUserId()+"님의 임시비밀번호 안내 이메일 입니다.");
dto.setMessage("안녕하세요. EduFoucs 입니다. "+ "\n"+ "임시비밀번호 안내 관련 메일 입니다." + "\n[" + user.getName() + "]" + "님의 임시 비밀번호는 "
+ str + " 입니다.");
System.out.println(dto);
MemberChangeDto memberChangeDto = new MemberChangeDto(user.getId(),str);
System.out.println(memberChangeDto);
userRepository.updatePassword(memberChangeDto.getId(),memberChangeDto.getPassword());
System.out.println();
return dto;
}
@Override
public void userCheck(Long id) throws Exception {
User user = userRepository.findById(id).orElse(null);
if(user == null)
{
System.out.println("불가");
throw new RuntimeException("유효하지 않은 아이디입니다. 다시 입력하세요");
}
else {
sendEamail(user);
}
}
public String getTempPassword() {
char[] charSet = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
String str = "";
int idx = 0;
for (int i=0; i<10; i++) {
idx = (int) (charSet.length * Math.random());
str += charSet[idx];
}
return str;
}
@Override @Override
public void saveRefreshToken(Long id, String refreshToken) throws Exception { public void saveRefreshToken(Long id, String refreshToken) throws Exception {
userRepository.saveRefreshToken(id, refreshToken); userRepository.saveRefreshToken(id, refreshToken);

View File

@ -24,3 +24,14 @@ spring.mvc.pathmatch.matching-strategy=ant_path_matcher
spring.jpa.database=mysql spring.jpa.database=mysql
spring.jpa.hibernate.ddl-auto=create spring.jpa.hibernate.ddl-auto=create
spring.jpa.show-sql=true spring.jpa.show-sql=true
spring.mail.host=smtp.gmail.com
spring.mail.port=587
#spring.mail.username=ssafytestpjt
#spring.mail.password=trpjbxqialufuzih
spring.mail.username=passfinder111@gmail.com
spring.mail.password=mnlyfkiprltjlsmw
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true