Skip to content

Commit

Permalink
Merge pull request #14 from capstone-maru/refactor/#12-refactoring
Browse files Browse the repository at this point in the history
refactor #12 - 코드 리뷰 진행 후 수정할 부분 리팩토링
  • Loading branch information
leejh7 authored Mar 15, 2024
2 parents cfa37d5 + a687f40 commit ac85738
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 72 deletions.
14 changes: 10 additions & 4 deletions src/main/java/org/capstone/maru/config/JpaConfig.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
package org.capstone.maru.config;

import java.util.Optional;
import org.capstone.maru.security.principal.SharedPostPrincipal;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.domain.AuditorAware;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;

/**
* login 구현시 tester -> name 변경
*/
@EnableJpaAuditing
@Configuration
public class JpaConfig {

@Bean
public AuditorAware<String> auditorAware() {
return () -> Optional.of("tester");
return () -> Optional.ofNullable(SecurityContextHolder.getContext())
.map(SecurityContext::getAuthentication)
.filter(Authentication::isAuthenticated)
.map(Authentication::getPrincipal)
.map(SharedPostPrincipal.class::cast)
.map(SharedPostPrincipal::getUsername);
}
}
56 changes: 11 additions & 45 deletions src/main/java/org/capstone/maru/dto/MemberAccountDto.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package org.capstone.maru.dto;

import java.time.LocalDateTime;
import lombok.Builder;
import org.capstone.maru.domain.MemberAccount;

@Builder
public record MemberAccountDto(
String memberId,
String email,
Expand All @@ -13,52 +15,16 @@ public record MemberAccountDto(
String modifiedBy
) {

public static MemberAccountDto of(
String memberId,
String email,
String nickname
) {
return new MemberAccountDto(
memberId,
email,
nickname,
null,
null,
null,
null
);
}

public static MemberAccountDto of(
String memberId,
String email,
String nickname,
LocalDateTime createdAt,
String createdBy,
LocalDateTime modifiedAt,
String modifiedBy
) {
return new MemberAccountDto(
memberId,
email,
nickname,
createdAt,
createdBy,
modifiedAt,
modifiedBy
);
}

public static MemberAccountDto from(MemberAccount entity) {
return new MemberAccountDto(
entity.getMemberId(),
entity.getEmail(),
entity.getNickname(),
entity.getCreatedAt(),
entity.getCreatedBy(),
entity.getModifiedAt(),
entity.getModifiedBy()
);
return MemberAccountDto.builder()
.memberId(entity.getMemberId())
.email(entity.getEmail())
.nickname(entity.getNickname())
.createdAt(entity.getCreatedAt())
.createdBy(entity.getCreatedBy())
.modifiedAt(entity.getModifiedAt())
.modifiedBy(entity.getModifiedBy())
.build();
}

public MemberAccount toEntity() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ public static SharedPostPrincipal from(MemberAccountDto dto) {
* @return
*/
public MemberAccountDto toDto() {
return MemberAccountDto.of(
memberId,
email,
nickname
);
return MemberAccountDto.builder()
.memberId(memberId)
.email(email)
.nickname(nickname)
.build();
}

// -- OAuth2User -- //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Map;
import lombok.Builder;
import lombok.Getter;

/**
Expand All @@ -19,6 +20,7 @@ public class KakaoOAuth2Response extends OAuth2Response {
private final Map<String, Object> properties;
private final KakaoAccount kakaoAccount;

@Builder
public record KakaoAccount(
Boolean profileNicknameNeedsAgreement,
Profile profile,
Expand All @@ -37,15 +39,28 @@ public static Profile from(Map<String, Object> attributes) {
}

public static KakaoAccount from(Map<String, Object> attributes) {
return new KakaoAccount(
Boolean.valueOf(String.valueOf(attributes.get("profile_nickname_needs_agreement"))),
Profile.from((Map<String, Object>) attributes.get("profile")),
Boolean.valueOf(String.valueOf(attributes.get("has_email"))),
Boolean.valueOf(String.valueOf(attributes.get("email_needs_agreement"))),
Boolean.valueOf(String.valueOf(attributes.get("is_email_valid"))),
Boolean.valueOf(String.valueOf(attributes.get("is_email_verified"))),
String.valueOf(attributes.get("email"))
);
return KakaoAccount.builder()
.profileNicknameNeedsAgreement(
Boolean.valueOf(String.valueOf(
attributes.get("profile_nickname_needs_agreement")))
)
.profile(
Profile.from((Map<String, Object>) attributes.get("profile"))
)
.hasEmail(
Boolean.valueOf(String.valueOf(attributes.get("has_email")))
)
.emailNeedsAgreement(Boolean.valueOf(
String.valueOf(attributes.get("email_needs_agreement")))
)
.isEmailValid(Boolean.valueOf(
String.valueOf(attributes.get("is_email_valid")))
)
.isEmailVerified(Boolean.valueOf(
String.valueOf(attributes.get("is_email_verified")))
)
.email(String.valueOf(attributes.get("email")))
.build();
}

public String nickname() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.capstone.maru.security.response;

import java.util.Map;
import lombok.Builder;
import lombok.Getter;

@SuppressWarnings("unchecked")
Expand All @@ -9,18 +10,19 @@ public class NaverOAuth2Response extends OAuth2Response {

private final Response response;

@Builder
private record Response(
String id,
String email,
String name
) {

public static Response from(Map<String, Object> attributes) {
return new Response(
String.valueOf(attributes.get("id")),
String.valueOf(attributes.get("email")),
String.valueOf(attributes.get("name"))
);
return Response.builder()
.id(String.valueOf(attributes.get("id")))
.email(String.valueOf(attributes.get("email")))
.name(String.valueOf(attributes.get("name")))
.build();
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ spring:
client:
registration:
kakao:
client-id: 0d7c948d8eaadc0e08f3e42636bba3bd
clientSecret: bIH2tF6jMbrRsdAp9DPL7yh97LP8pPXk
client-id: ${KAKAO_OAUTH_CLIENT_ID}
client-secret: ${KAKAO_OAUTH_CLIENT_SECRET}
authorization-grant-type: authorization_code
redirect-uri: "{baseUrl}/login/oauth2/code/kakao"
client-authentication-method: client_secret_post
naver:
client-id: OvLyXvoT1NlEr8Wfiu2u
client-secret: U6Or_rMFOQ
client-id: ${NAVER_OAUTH_CLIENT_ID}
client-secret: ${NAVER_OAUTH_CLIENT_SECRET}
authorization-grant-type: authorization_code
redirect-uri: "{baseUrl}/login/oauth2/code/naver"
provider:
Expand Down

0 comments on commit ac85738

Please sign in to comment.