-
Notifications
You must be signed in to change notification settings - Fork 6
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
[BE] 회원가입 기능을 구현한다. #774
Merged
Merged
[BE] 회원가입 기능을 구현한다. #774
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
f898fbb
feat: PasswordEncoder 구현
tkdgur0906 633fa50
test: e2e 테스트 헤더 로직 통일
tkdgur0906 06c633c
feat: User에 Password 필드 추가 및 형식 검증
tkdgur0906 6e5fda6
feat: password 컬럼 sql 설정에 추가
tkdgur0906 fe6ab1c
feat: email 클래스 생성하여 역할 분리
tkdgur0906 47bc872
feat: 회원가입 기능 구현
tkdgur0906 8380e95
feat: User의 email, loginType 유니크 제약조건 추가
tkdgur0906 1493ad1
feat: 동일한 이메일로 회원가입 하지 못하도록 구현
tkdgur0906 8e08cb3
feat: salt 랜덤으로 생성하도록 변경
tkdgur0906 28135cf
refactor: 회원가입 엔드포인트 변경
tkdgur0906 b00c448
style: 이메일, 비밀번호 정책 주석 추가
tkdgur0906 935ee5b
feat: data.sql 비밀번호 암호화하여 삽입
tkdgur0906 bc0484b
test: 중복 테스트 삭제
tkdgur0906 d900f0f
refactor: Email, Password 검증 메서드명 명확히 수정
tkdgur0906 233ed59
feat: PasswordEncoder 커스텀 예외 처리 적용
tkdgur0906 fc5ff92
Merge remote-tracking branch 'origin/dev-be' into feat/729-register
tkdgur0906 4a412db
refactor: 미사용 db 메서드 제거
tkdgur0906 e09660c
refactor: PasswordEncoder encode시 외부에서 salt 주입받도록 변경
tkdgur0906 4f10903
feat: 기본 salt로 encode하는 메서드 추가
tkdgur0906 9b29556
refactor: PasswordEncoder 정적 클래스로 변경
tkdgur0906 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
backend/bang-ggood/src/main/java/com/bang_ggood/auth/dto/request/RegisterRequestV1.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.bang_ggood.auth.dto.request; | ||
|
||
import com.bang_ggood.user.domain.LoginType; | ||
import com.bang_ggood.user.domain.User; | ||
import com.bang_ggood.user.domain.UserType; | ||
import jakarta.validation.constraints.NotBlank; | ||
|
||
public record RegisterRequestV1(@NotBlank(message = "이름이 존재하지 않습니다.") String name, | ||
@NotBlank(message = "이메일이 존재하지 않습니다.") String email, | ||
@NotBlank(message = "비밀번호가 존재하지 않습니다.") String password) { | ||
|
||
public User toUserEntity() { | ||
return new User(name(), email(), password(), UserType.USER, LoginType.LOCAL); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
backend/bang-ggood/src/main/java/com/bang_ggood/auth/service/PasswordEncoder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.bang_ggood.auth.service; | ||
|
||
import com.bang_ggood.global.exception.BangggoodException; | ||
import com.bang_ggood.global.exception.ExceptionCode; | ||
import javax.crypto.SecretKeyFactory; | ||
import javax.crypto.spec.PBEKeySpec; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.security.SecureRandom; | ||
import java.security.spec.InvalidKeySpecException; | ||
import java.security.spec.KeySpec; | ||
import java.util.Base64; | ||
|
||
public class PasswordEncoder { | ||
|
||
private static final String DELIMITER = ":"; | ||
|
||
public static String encodeWithGeneralSalt(String password) { | ||
return encode(password, getSalt()); | ||
} | ||
|
||
public static String encode(String password, byte[] salt) { | ||
try { | ||
KeySpec spec = new PBEKeySpec(password.toCharArray(), salt, 65536, 512); | ||
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA512"); | ||
byte[] hash = factory.generateSecret(spec).getEncoded(); | ||
String encodedPassword = Base64.getEncoder().encodeToString(hash); | ||
String encodedSalt = Base64.getEncoder().encodeToString(salt); | ||
return encodedPassword + DELIMITER + encodedSalt; | ||
} catch (NoSuchAlgorithmException | InvalidKeySpecException e) { | ||
throw new BangggoodException(ExceptionCode.PASSWORD_HASHING_ERROR); | ||
} | ||
} | ||
|
||
public static byte[] getSalt() { | ||
SecureRandom secureRandom = new SecureRandom(); | ||
byte[] salt = new byte[64]; | ||
secureRandom.nextBytes(salt); | ||
return salt; | ||
} | ||
|
||
private PasswordEncoder() { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
backend/bang-ggood/src/main/java/com/bang_ggood/user/domain/Email.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.bang_ggood.user.domain; | ||
|
||
import com.bang_ggood.global.exception.BangggoodException; | ||
import com.bang_ggood.global.exception.ExceptionCode; | ||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Embeddable; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import java.util.Objects; | ||
import java.util.regex.Pattern; | ||
|
||
import static lombok.AccessLevel.PROTECTED; | ||
|
||
@Embeddable | ||
@Getter | ||
@NoArgsConstructor(access = PROTECTED) | ||
public class Email { | ||
|
||
//이메일은 영문 대소문자, 숫자, 점, 하이픈, 언더스코어, 플러스 기호를 포함할 수 있으며, | ||
// "@" 기호 뒤에 도메인 이름이 필요하고, | ||
// 마지막에는 최소 2글자의 영문자로 이루어진 최상위 도메인이 포함되어야 한다.. | ||
private static final Pattern EMAIL_PATTERN = | ||
Pattern.compile("^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$"); | ||
|
||
@Column(name = "email") | ||
private String value; | ||
|
||
public Email(String value) { | ||
validateEmailPattern(value); | ||
this.value = value; | ||
} | ||
|
||
public void validateEmailPattern(String email) { | ||
if(!EMAIL_PATTERN.matcher(email).matches()) { | ||
throw new BangggoodException(ExceptionCode.EMAIL_INVALID_FORMAT); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
Email email = (Email) o; | ||
return Objects.equals(value, email.value); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hashCode(value); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
backend/bang-ggood/src/main/java/com/bang_ggood/user/domain/Password.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.bang_ggood.user.domain; | ||
|
||
import com.bang_ggood.auth.service.PasswordEncoder; | ||
import com.bang_ggood.global.exception.BangggoodException; | ||
import com.bang_ggood.global.exception.ExceptionCode; | ||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Embeddable; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import java.util.Objects; | ||
import java.util.regex.Pattern; | ||
|
||
import static lombok.AccessLevel.PROTECTED; | ||
|
||
@Getter | ||
@Embeddable | ||
@NoArgsConstructor(access = PROTECTED) | ||
public class Password { | ||
|
||
//비밀번호는 최소 6자 이상이어야 하며, 영어 문자와 숫자를 각각 1개 이상 포함해야 한다. | ||
private static final Pattern PASSWORD_PATTERN = | ||
Pattern.compile("^(?=.*[A-Za-z])(?=.*\\d)[A-Za-z\\d]{6,}$"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 사용한 PASSWORD 패턴 전략도 본문에 적어 주시면 좋을 거서 같아요! |
||
|
||
@Column(name = "password") | ||
private String value; | ||
|
||
public Password(String value) { | ||
validatePasswordPattern(value); | ||
this.value = PasswordEncoder.encodeWithGeneralSalt(value); | ||
} | ||
|
||
public void validatePasswordPattern(String password) { | ||
if (!PASSWORD_PATTERN.matcher(password).matches()) { | ||
throw new BangggoodException(ExceptionCode.PASSWORD_INVALID_FORMAT); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
Password password1 = (Password) o; | ||
return Objects.equals(value, password1.value); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hashCode(value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
INSERT INTO users(name, email, user_type, login_type, created_at, modified_at, deleted) | ||
VALUES ('방방이', '[email protected]', 'USER', 'LOCAL', '2024-07-22 07:56:42', '2024-07-22 07:56:42', false); | ||
-- 비밀번호 : password1234 | ||
INSERT INTO users(name, email, password, user_type, login_type, created_at, modified_at, deleted) | ||
VALUES ('방방이', '[email protected]', 'xDNYKEJqE/36U0Dt3nXRMFPNEMEgjCYM7R/A4B29baOsv4KYQ9MGgcO3HUa11sNKCFb9ZXyYBqJqxNglvBzFvg==:7yejAszEpxBb7AyZNKvAqpmMEJiKFXIa8JKwAx3n4loB2DRcAC2pfwkgo/dzKzRvBX4RbrATWaIlPYrgAhbHZQ==', | ||
'USER', 'LOCAL', '2024-07-22 07:56:42', '2024-07-22 07:56:42', false); | ||
|
||
INSERT INTO custom_checklist_question(user_id, question, created_at, modified_at, deleted) | ||
VALUES (1, 'ROOM_CONDITION_1', '2024-07-22 07:56:42', '2024-07-22 07:56:42', false), | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,11 +33,13 @@ CREATE TABLE users | |
id BIGINT AUTO_INCREMENT PRIMARY KEY, | ||
name VARCHAR(255), | ||
email VARCHAR(255) NOT NULL, | ||
user_type VARCHAR(255) NOT NULL, | ||
login_type VARCHAR(255) NOT NULL, | ||
password VARCHAR(255), | ||
user_type VARCHAR(255) NOT NULL, | ||
login_type VARCHAR(255) NOT NULL, | ||
created_at TIMESTAMP(6), | ||
modified_at TIMESTAMP(6), | ||
deleted BOOLEAN | ||
deleted BOOLEAN, | ||
CONSTRAINT unique_email_login_type UNIQUE (email, login_type) | ||
Comment on lines
+36
to
+42
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. prod 환경에 배포할 때 데이터 정합성 일치하도록 주의해주세요! |
||
); | ||
|
||
CREATE TABLE checklist | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
주석 사용을 지양하긴 하지만, 빠르게 이해하기 쉽게 주석으로 간단히 설명 적어주면 좋을 것 같아요~