-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨practice(#10) : OAuth2를 활용한 카카오 소셜 로그인 구현
- Loading branch information
Showing
5 changed files
with
93 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
spring/src/main/java/umc/spring/config/security/CustomOAuth2UserService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package umc.spring.config.security; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
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.core.OAuth2AuthenticationException; | ||
import org.springframework.security.oauth2.core.user.DefaultOAuth2User; | ||
import org.springframework.security.oauth2.core.user.OAuth2User; | ||
import org.springframework.stereotype.Service; | ||
import umc.spring.domain.Member; | ||
import umc.spring.domain.enums.Gender; | ||
import umc.spring.domain.enums.Role; | ||
import umc.spring.repository.MemberRepository.MemberRepository; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class CustomOAuth2UserService extends DefaultOAuth2UserService { | ||
|
||
private final MemberRepository memberRepository; | ||
private final PasswordEncoder passwordEncoder; | ||
|
||
@Override | ||
public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2AuthenticationException { | ||
OAuth2User oAuth2User = super.loadUser(userRequest); | ||
|
||
Map<String, Object> attributes = oAuth2User.getAttributes(); | ||
Map<String, Object> properties = (Map<String, Object>) attributes.get("properties"); | ||
|
||
String nickname = (String) properties.get("nickname"); | ||
String email = nickname + "@kakao.com"; // 임시 이메일 생성 | ||
|
||
// 사용자 정보 저장 또는 업데이트 | ||
Member member = saveOrUpdateUser(email, nickname); | ||
|
||
// 이메일을 Principal로 사용하기 위해 attributes 수정 | ||
Map<String, Object> modifiedAttributes = new HashMap<>(attributes); | ||
modifiedAttributes.put("email", email); | ||
|
||
return new DefaultOAuth2User( | ||
oAuth2User.getAuthorities(), | ||
modifiedAttributes, | ||
"email" // email Principal로 설정 | ||
); | ||
} | ||
|
||
private Member saveOrUpdateUser(String email, String nickname) { | ||
Member member = memberRepository.findByEmail(email) | ||
.orElse(Member.builder() | ||
.email(email) | ||
.name(nickname) | ||
.password(passwordEncoder.encode("OAUTH_USER_" + UUID.randomUUID())) | ||
.gender(Gender.NONE) // 기본값 설정 | ||
.address("소셜로그인") // 기본값 설정 | ||
.specAddress("소셜로그인") // 기본값 설정 | ||
.role(Role.USER) | ||
.build()); | ||
|
||
return memberRepository.save(member); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters