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

refactor #17 - 코드 리뷰 진행 후 로그인 로직 리팩토링 #19

Merged
merged 7 commits into from
Mar 19, 2024
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
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,8 @@

프로젝트를 진행하면서 서비스의 성능을 향상시킬 수 있는 부분에 대해서 고민하고 테스팅한 후 성능 향상 전/후의 측정값들을 기록하고 성능 향상 원인에 대해 공부한 것을
정리하였습니다.
<br>

## 9. 설계 다이어그램

### [시퀀스 다이어그램] (https://viewer.diagrams.net/?tags=%7B%7D&highlight=0000ff&edit=_blank&layers=1&nav=1#G1BFeex-2cN2aTEROFH5kdMMmAsgxf-xuQ)
<br>
14 changes: 0 additions & 14 deletions src/main/java/org/capstone/maru/controller/LoginController.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,4 @@
@RequestMapping
public class LoginController {

@GetMapping("/login")
public String socialLogin() {
return "카카오 로그인 url: login-kakao | 네이버 로그인 url: login-naver";
}

@GetMapping("/login-kakao")
public void loginKakao(HttpServletResponse response) throws IOException {
response.sendRedirect("oauth2/authorization/kakao");
}

@GetMapping(value = "/login-naver")
public void loginNaver(HttpServletResponse response) throws IOException {
response.sendRedirect("oauth2/authorization/naver");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import lombok.RequiredArgsConstructor;
import org.capstone.maru.security.principal.SharedPostPrincipal;
import org.capstone.maru.service.MemberAccountService;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
Expand All @@ -12,8 +11,6 @@
@RestController
public class MainController {

private final MemberAccountService memberAccountService;

@GetMapping("/")
public String root() {
return "health check";
Expand Down
15 changes: 12 additions & 3 deletions src/main/java/org/capstone/maru/domain/MemberAccount.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import org.springframework.data.domain.Persistable;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
Expand All @@ -22,17 +23,15 @@
@Index(columnList = "createdBy")
})
@Entity
public class MemberAccount extends AuditingFields {
public class MemberAccount extends AuditingFields implements Persistable<String> {
cheesecrust marked this conversation as resolved.
Show resolved Hide resolved

@Id
@Column(nullable = false, length = 50)
private String memberId;

@Setter
@Column(length = 100)
private String email;

@Setter
@Column(length = 100)
private String nickname;

Expand Down Expand Up @@ -81,4 +80,14 @@ public boolean equals(Object o) {
public int hashCode() {
return Objects.hash(this.getMemberId());
}

@Override
public String getId() {
return memberId;
}

@Override
public boolean isNew() {
return getCreatedAt() == null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,13 @@
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;

@Slf4j
@Configuration
@EnableWebSecurity
public class SecurityConfig {

private final AuthenticationEntryPoint authEntryPoint;
Expand Down Expand Up @@ -50,8 +48,7 @@ public SecurityFilterChain securityFilterChain(
.requestMatchers(PathRequest.toStaticResources().atCommonLocations()).permitAll()
.requestMatchers(
HttpMethod.GET,
"/", "/login", "login-kakao", "login-naver", "/oauth2/**", "/login/oauth2/**",
"/errorTest"
"/"
).permitAll()
.requestMatchers(
HttpMethod.POST,
Expand All @@ -68,6 +65,10 @@ public SecurityFilterChain securityFilterChain(
.exceptionHandling(hc -> hc
.authenticationEntryPoint(authEntryPoint)
)
.logout(logout -> logout
cheesecrust marked this conversation as resolved.
Show resolved Hide resolved
.deleteCookies("JSESSIONID")
.logoutSuccessUrl("/").permitAll()
)
.csrf(
csrf -> csrf
.ignoringRequestMatchers("/api/**")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,7 @@ public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2Authentic
oAuth2User.getAttributes()
);

String memberId = getMemberId(registrationId, extractAttributes);

return memberAccountService
.searchMember(memberId)
.map(SharedPostPrincipal::from)
.orElseGet(() ->
SharedPostPrincipal.from(
memberAccountService.saveUser(
memberId,
extractAttributes.email(),
extractAttributes.nickname()
)
)
);
return createSharedPostPrincipal(registrationId, extractAttributes);
}

private SocialType getSocialType(String registrationId) {
Expand All @@ -55,5 +42,18 @@ private String getMemberId(String registrationId, OAuth2Response oAuth2Response)
return registrationId + "_" + oAuth2Response.id();
}

private SharedPostPrincipal createSharedPostPrincipal(
String registrationId,
OAuth2Response extractAttributes
) {
String memberId = getMemberId(registrationId, extractAttributes);

return SharedPostPrincipal.from(
memberAccountService.login(
memberId,
extractAttributes.email(),
extractAttributes.nickname()
)
);
}
}
23 changes: 15 additions & 8 deletions src/main/java/org/capstone/maru/service/MemberAccountService.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,25 @@ public Optional<MemberAccountDto> searchMember(String memberId) {
.map(MemberAccountDto::from);
}

@Transactional
public MemberAccountDto saveUser(
public MemberAccountDto login(String memberId, String email, String nickname) {
Optional<MemberAccount> memberAccount = memberAccountRepository.findByEmail(email);

if (memberAccount.isEmpty()) {
return saveMember(memberId, email, nickname);
}

if (memberAccount.get().getMemberId().equals(memberId)) {
return MemberAccountDto.from(memberAccount.get());
}

throw new MemberAccountExistentException(RestErrorCode.DUPLICATE_VALUE);
}

private MemberAccountDto saveMember(
String memberId,
String email,
String nickname
) {
if (memberAccountRepository.findByEmail(email).isPresent()) {
throw new MemberAccountExistentException(
RestErrorCode.DUPLICATE_VALUE
);
}

return MemberAccountDto.from(
memberAccountRepository.save(
MemberAccount.of(
Expand Down