diff --git a/src/main/java/com/example/codebase/domain/auth/OAuthAttributes.java b/src/main/java/com/example/codebase/domain/auth/OAuthAttributes.java index 42b8d993..023f1d47 100644 --- a/src/main/java/com/example/codebase/domain/auth/OAuthAttributes.java +++ b/src/main/java/com/example/codebase/domain/auth/OAuthAttributes.java @@ -72,17 +72,4 @@ private static OAuthAttributes ofNaver(String userNameAttributeName, .build(); } - public Member toEntity(PasswordEncoder passwordEncoder) { - return Member.builder() - .username(oAuthProviderId) - .password(passwordEncoder.encode(UUID.randomUUID().toString())) - .name(name) - .email(email) - .picture(picture) - .oauthProvider(registrationId) - .oauthProviderId(oAuthProviderId) - .createdTime(LocalDateTime.now()) - .activated(true) - .build(); - } } diff --git a/src/main/java/com/example/codebase/domain/auth/service/CustomOAuth2UserService.java b/src/main/java/com/example/codebase/domain/auth/service/CustomOAuth2UserService.java index 40a81ee4..0dbb0f2d 100644 --- a/src/main/java/com/example/codebase/domain/auth/service/CustomOAuth2UserService.java +++ b/src/main/java/com/example/codebase/domain/auth/service/CustomOAuth2UserService.java @@ -52,8 +52,6 @@ public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2Authentic try { Member member = saveOrUpdate(oAuthAttributes); - - List simpleGrantedAuthorityList = new ArrayList<>(); for (MemberAuthority memberAuthority : member.getAuthorities()) { simpleGrantedAuthorityList.add(new SimpleGrantedAuthority(memberAuthority.getAuthority().getAuthorityName())); diff --git a/src/main/java/com/example/codebase/domain/member/entity/Member.java b/src/main/java/com/example/codebase/domain/member/entity/Member.java index 66d18d46..65292765 100644 --- a/src/main/java/com/example/codebase/domain/member/entity/Member.java +++ b/src/main/java/com/example/codebase/domain/member/entity/Member.java @@ -2,6 +2,7 @@ import com.example.codebase.domain.artwork.entity.Artwork; import com.example.codebase.domain.artwork.entity.ArtworkLikeMember; +import com.example.codebase.domain.auth.OAuthAttributes; import com.example.codebase.domain.member.dto.CreateArtistMemberDTO; import com.example.codebase.domain.member.dto.UpdateMemberDTO; import com.example.codebase.domain.member.entity.oauth2.oAuthProvider; @@ -9,6 +10,7 @@ import org.hibernate.annotations.GenericGenerator; import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.security.core.userdetails.User; +import org.springframework.security.crypto.password.PasswordEncoder; import javax.persistence.*; import java.time.LocalDateTime; @@ -109,6 +111,34 @@ public static User toUser(Member member) { .collect(Collectors.toList())); } + public static Member from(PasswordEncoder passwordEncoder, OAuthAttributes oAuthAttributes) { + String username = generateUniqueUsername(); + + return Member.builder() + .username(username) + .password(passwordEncoder.encode(UUID.randomUUID().toString())) + .name(oAuthAttributes.getName()) + .email(oAuthAttributes.getEmail()) + .picture(oAuthAttributes.getPicture()) + .oauthProvider(oAuthAttributes.getRegistrationId()) + .oauthProviderId(oAuthAttributes.getOAuthProviderId()) + .createdTime(LocalDateTime.now()) + .activated(true) + .build(); + } + + private static String generateUniqueUsername() { + // UUID 생성 + UUID uuid = UUID.randomUUID(); + + // UUID를 문자열로 변환하고 "-"를 제거하여 username 생성 + String username = uuid.toString().replace("-", ""); + + // "@"를 앞에 추가 + username = username.substring(0, 10); // 예시로 10자리만 사용 + return "user-" + username; + } + public Member update(String name, String picture) { this.name = name; this.picture = picture; @@ -118,8 +148,7 @@ public Member update(String name, String picture) { public Member update(UpdateMemberDTO dto) { if (dto.getUsername() != null) { - this.username = dto.getUsername(); - } + this.username = dto.getUsername(); } if (dto.getName() != null) { this.name = dto.getName(); } diff --git a/src/main/java/com/example/codebase/domain/member/service/MemberService.java b/src/main/java/com/example/codebase/domain/member/service/MemberService.java index cc39d565..9a2280bb 100644 --- a/src/main/java/com/example/codebase/domain/member/service/MemberService.java +++ b/src/main/java/com/example/codebase/domain/member/service/MemberService.java @@ -118,12 +118,11 @@ public Member createOAuthMember(OAuthAttributes oAuthAttributes) { throw new ExistsEmailException(); } - // New Save Authority authority = new Authority(); authority.setAuthorityName("ROLE_USER"); - Member newMember = oAuthAttributes.toEntity(passwordEncoder); + Member newMember = Member.from(passwordEncoder, oAuthAttributes); MemberAuthority memberAuthority = MemberAuthority.builder() .authority(authority) .member(newMember) diff --git a/src/main/resources/log/log4j2-dev.xml b/src/main/resources/log/log4j2-dev.xml index 56e99a5d..84f90f28 100644 --- a/src/main/resources/log/log4j2-dev.xml +++ b/src/main/resources/log/log4j2-dev.xml @@ -16,12 +16,12 @@ - - - - - - + + + + + +