Skip to content
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

Feat/user id optimize #19

Merged
merged 2 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2Authentic
try {
Member member = saveOrUpdate(oAuthAttributes);



List<SimpleGrantedAuthority> simpleGrantedAuthorityList = new ArrayList<>();
for (MemberAuthority memberAuthority : member.getAuthorities()) {
simpleGrantedAuthorityList.add(new SimpleGrantedAuthority(memberAuthority.getAuthority().getAuthorityName()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

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;
import lombok.*;
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;
Expand Down Expand Up @@ -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;
Expand All @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions src/main/resources/log/log4j2-dev.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
<PatternLayout disableAnsi="false" pattern="%d${LOG_DATEFORMAT_PATTERN} ${LOG_REQUEST_ID_PATTERN} %highlight${LOG_LEVEL_PATTERN}"/>
</Console>

<Socket name="socket" host="${env:LOGSTASH_HOST_NAME:-localhost}" port="${env:LOGSTASH_HOST_PORT:-50000}" reconnectionDelayMillis="5000">
<JsonLayout compact="true" eventEol="true" properties="true" >
<KeyValuePair key="app_name" value="art-dev-backend" />
</JsonLayout>
<PatternLayout pattern="${LOGSTASH_PATTERN}" />
</Socket>
<!-- <Socket name="socket" host="${env:LOGSTASH_HOST_NAME:-localhost}" port="${env:LOGSTASH_HOST_PORT:-50000}" reconnectionDelayMillis="5000">-->
<!-- <JsonLayout compact="true" eventEol="true" properties="true" >-->
<!-- <KeyValuePair key="app_name" value="art-dev-backend" />-->
<!-- </JsonLayout>-->
<!-- <PatternLayout pattern="${LOGSTASH_PATTERN}" />-->
<!-- </Socket>-->

<!-- FileAppender -->
<RollingFile name="file_appender"
Expand Down
Loading