Skip to content

Commit

Permalink
feat kookmin-sw#2 - naver login
Browse files Browse the repository at this point in the history
OAuth2Response에 맞춰 NaverResponse 구현
  • Loading branch information
cheesecrust committed Mar 10, 2024
1 parent ff8c17b commit bc0a6ea
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 67 deletions.
3 changes: 3 additions & 0 deletions src/main/java/org/capstone/maru/config/JpaConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import org.springframework.data.domain.AuditorAware;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;

/**
* login 구현시 tester -> name 변경
*/
@EnableJpaAuditing
@Configuration
public class JpaConfig {
Expand Down
52 changes: 2 additions & 50 deletions src/main/java/org/capstone/maru/config/SecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,14 @@


import lombok.extern.slf4j.Slf4j;
import org.capstone.maru.security.CustomOAuth2UserService;
import org.capstone.maru.security.KakaoOAuth2Response;
import org.capstone.maru.security.SharedPostPrincipal;
import org.capstone.maru.service.MemberAccountService;
import org.capstone.maru.security.service.CustomOAuth2UserService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.security.servlet.PathRequest;
import org.springframework.context.annotation.Bean;
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.WebSecurityCustomizer;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.client.userinfo.DefaultOAuth2UserService;
import org.springframework.security.oauth2.client.userinfo.OAuth2UserRequest;
import org.springframework.security.oauth2.client.userinfo.OAuth2UserService;
import org.springframework.security.oauth2.core.user.OAuth2User;
import org.springframework.security.web.SecurityFilterChain;

@Slf4j
Expand All @@ -29,7 +20,7 @@ public class SecurityConfig {
@ConditionalOnProperty(name = "spring.h2.console.enabled", havingValue = "true")
public WebSecurityCustomizer configureH2ConsoleEnable() {
return web -> web.ignoring()
.requestMatchers(PathRequest.toH2Console());
.requestMatchers(PathRequest.toH2Console());
}

@Bean
Expand Down Expand Up @@ -58,43 +49,4 @@ public SecurityFilterChain securityFilterChain(
)
.build();
}

@Bean
public OAuth2UserService<OAuth2UserRequest, OAuth2User> oAuth2UserService(
MemberAccountService memberAccountService,
PasswordEncoder passwordEncoder
) {
final DefaultOAuth2UserService delegate = new DefaultOAuth2UserService();

return userRequest -> {
OAuth2User oAuth2User = delegate.loadUser(userRequest);

KakaoOAuth2Response kakaoOAuthResponse = KakaoOAuth2Response.from(
oAuth2User.getAttributes());

String registrationId = userRequest.getClientRegistration()
.getRegistrationId(); // "kakao"

String providerId = String.valueOf(kakaoOAuthResponse.id());
String memberId = registrationId + "_" + providerId;

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

@Bean
public PasswordEncoder passwordEncoder() {
return PasswordEncoderFactories.createDelegatingPasswordEncoder();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.capstone.maru.controller;

import org.capstone.maru.security.SharedPostPrincipal;
import org.capstone.maru.security.principal.SharedPostPrincipal;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
Expand All @@ -10,7 +10,7 @@ public class MainController {

@GetMapping("/")
public String root() {
return "home";
return "health check";
}

@GetMapping("/test")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,11 @@ public enum SocialType {
private final String key;

public static SocialType of(String registrationId) {
switch (registrationId) {
case "kakao":
return SocialType.KAKAO;
case "naver":
return SocialType.NAVER;
default:
throw new IllegalArgumentException(
"Unsupported social registrationId: " + registrationId);
}
return switch (registrationId) {
case "kakao" -> SocialType.KAKAO;
case "naver" -> SocialType.NAVER;
default -> throw new IllegalArgumentException(
"Unsupported social registrationId: " + registrationId);
};
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.capstone.maru.security;
package org.capstone.maru.security.principal;

import java.util.Collection;
import java.util.Map;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.capstone.maru.security;
package org.capstone.maru.security.response;

import java.time.LocalDateTime;
import java.time.ZoneId;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.capstone.maru.security;
package org.capstone.maru.security.response;

import java.util.Map;
import lombok.Getter;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.capstone.maru.security;
package org.capstone.maru.security.response;

import java.util.Map;
import lombok.Getter;
Expand All @@ -11,7 +11,7 @@ public abstract class OAuth2Response {
/**
* SocialType에 맞는 메소드 호출하여 OAuthAttributes 객체 반환 파라미터 : userNameAttributeName -> OAuth2 로그인 시
* 키(PK)가 되는 값 / attributes : OAuth 서비스의 유저 정보들 소셜별 of 메소드(ofGoogle, ofKaKao, ofNaver)들은 각각 소셜
* 로그인 API에서 제공하는 회원의 식별값(id), attributes, nameAttributeKey를 저장 후 build
* 로그인 API에서 제공하는 회원의 식별값(id), attributes, nameAttributeKey를 저장 후 build 필드가 들어갈 수 있을거 같은데
*/
public static OAuth2Response of(
SocialType socialType,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.capstone.maru.security;
package org.capstone.maru.security.service;

import lombok.RequiredArgsConstructor;
import org.capstone.maru.security.response.OAuth2Response;
import org.capstone.maru.security.principal.SharedPostPrincipal;
import org.capstone.maru.security.constant.SocialType;
import org.capstone.maru.service.MemberAccountService;
import org.springframework.security.oauth2.client.userinfo.DefaultOAuth2UserService;
Expand Down

0 comments on commit bc0a6ea

Please sign in to comment.