From 589ffbb96c33c3253808df96ab278f19063debf0 Mon Sep 17 00:00:00 2001 From: cabbage16 Date: Wed, 27 Nov 2024 22:41:57 +0900 Subject: [PATCH] =?UTF-8?q?feat(#30):=20User=20Entity=20=EB=B9=84=EB=B0=80?= =?UTF-8?q?=EB=B2=88=ED=98=B8=20=EC=B6=94=EA=B0=80=20-=20=EA=B8=B0?= =?UTF-8?q?=EC=A1=B4=EC=97=90=EB=8A=94=20Google=20OAuth=EB=A5=BC=20?= =?UTF-8?q?=EC=9D=B4=EC=9A=A9=ED=95=B4=EC=84=9C=20=EB=B9=84=EB=B0=80?= =?UTF-8?q?=EB=B2=88=ED=98=B8=EB=A5=BC=20=EC=82=AC=EC=9A=A9=ED=95=98?= =?UTF-8?q?=EC=A7=80=20=EC=95=8A=EB=8B=A4=EA=B0=80,=20=EC=8B=9C=EB=82=98?= =?UTF-8?q?=EB=B8=8C=EB=A1=9C=20=EB=A1=9C=EA=B7=B8=EC=9D=B8/=ED=9A=8C?= =?UTF-8?q?=EC=9B=90=EA=B0=80=EC=9E=85=EC=9D=84=20=EC=9C=84=ED=95=B4?= =?UTF-8?q?=EC=84=9C=20=EC=9C=A0=EC=A0=80=20=EB=B9=84=EB=B0=80=EB=B2=88?= =?UTF-8?q?=ED=98=B8=20=EC=BB=AC=EB=9F=BC=EC=9D=84=20=EC=B6=94=EA=B0=80?= =?UTF-8?q?=ED=96=88=EC=96=B4=EC=9A=94.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../auth/service/GoogleAuthService.java | 6 ++++- .../sinabro/domain/user/domain/User.java | 10 +++++++- .../domain/user/domain/value/Password.java | 23 +++++++++++++++++++ .../sinabro/shared/util/PasswordUtil.java | 19 +++++++++++++++ 4 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/bamdoliro/sinabro/domain/user/domain/value/Password.java create mode 100644 src/main/java/com/bamdoliro/sinabro/shared/util/PasswordUtil.java diff --git a/src/main/java/com/bamdoliro/sinabro/domain/auth/service/GoogleAuthService.java b/src/main/java/com/bamdoliro/sinabro/domain/auth/service/GoogleAuthService.java index 3f6dd7a..8f190ee 100644 --- a/src/main/java/com/bamdoliro/sinabro/domain/auth/service/GoogleAuthService.java +++ b/src/main/java/com/bamdoliro/sinabro/domain/auth/service/GoogleAuthService.java @@ -23,7 +23,11 @@ public TokenResponse execute(GoogleInformation information) { if (user.isEmpty()) { userRepository.save( - new User(email, information.getName(), Authority.USER) + User.builder() + .email(email) + .name(information.getName()) + .authority(Authority.USER) + .build() ); } diff --git a/src/main/java/com/bamdoliro/sinabro/domain/user/domain/User.java b/src/main/java/com/bamdoliro/sinabro/domain/user/domain/User.java index e53384b..9cd9b1b 100644 --- a/src/main/java/com/bamdoliro/sinabro/domain/user/domain/User.java +++ b/src/main/java/com/bamdoliro/sinabro/domain/user/domain/User.java @@ -1,10 +1,13 @@ package com.bamdoliro.sinabro.domain.user.domain; import com.bamdoliro.sinabro.domain.user.domain.type.Authority; +import com.bamdoliro.sinabro.domain.user.domain.value.Password; import com.bamdoliro.sinabro.shared.entity.BaseTimeEntity; +import com.bamdoliro.sinabro.shared.util.PasswordUtil; import jakarta.persistence.*; import jakarta.validation.constraints.Email; import lombok.AccessLevel; +import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; @@ -26,13 +29,18 @@ public class User extends BaseTimeEntity { @Column(nullable = false) private String name; + @Embedded + private Password password; + @Enumerated(EnumType.STRING) @Column(nullable = false, length = 10) private Authority authority; - public User(String email, String name, Authority authority) { + @Builder + public User(String email, String name, String password, Authority authority) { this.email = email; this.name = name; + this.password = new Password(PasswordUtil.encode(password)); this.authority = authority; } } diff --git a/src/main/java/com/bamdoliro/sinabro/domain/user/domain/value/Password.java b/src/main/java/com/bamdoliro/sinabro/domain/user/domain/value/Password.java new file mode 100644 index 0000000..9aaf518 --- /dev/null +++ b/src/main/java/com/bamdoliro/sinabro/domain/user/domain/value/Password.java @@ -0,0 +1,23 @@ +package com.bamdoliro.sinabro.domain.user.domain.value; + +import com.bamdoliro.sinabro.shared.util.PasswordUtil; +import jakarta.persistence.Column; +import jakarta.persistence.Embeddable; +import lombok.AccessLevel; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Getter +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@AllArgsConstructor +@Embeddable +public class Password { + + @Column(name = "password", length = 60) + private String value; + + public boolean match(String password) { + return PasswordUtil.matches(value, password); + } +} diff --git a/src/main/java/com/bamdoliro/sinabro/shared/util/PasswordUtil.java b/src/main/java/com/bamdoliro/sinabro/shared/util/PasswordUtil.java new file mode 100644 index 0000000..166e69d --- /dev/null +++ b/src/main/java/com/bamdoliro/sinabro/shared/util/PasswordUtil.java @@ -0,0 +1,19 @@ +package com.bamdoliro.sinabro.shared.util; + +import lombok.experimental.UtilityClass; +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; +import org.springframework.security.crypto.password.PasswordEncoder; + +@UtilityClass +public class PasswordUtil { + + private static final PasswordEncoder passwordEncoder = new BCryptPasswordEncoder(); + + public boolean matches(String actual, String expected) { + return passwordEncoder.matches(expected, actual); + } + + public String encode(String password) { + return passwordEncoder.encode(password); + } +}