-
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.
Merge pull request #49 from Kakaotech-18-Ecommerce/SCRUM-69-SocicalLo…
…gout [Feat] Scrum 69 socical logout
- Loading branch information
Showing
10 changed files
with
149 additions
and
28 deletions.
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
15 changes: 14 additions & 1 deletion
15
src/main/java/com/kakaoteck/golagola/domain/auth/Repository/UserRepository.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
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
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
47 changes: 47 additions & 0 deletions
47
...ain/java/com/kakaoteck/golagola/security/handler/signout/CustomSignOutProcessHandler.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,47 @@ | ||
package com.kakaoteck.golagola.security.handler.signout; | ||
|
||
|
||
import com.kakaoteck.golagola.domain.auth.Repository.UserRepository; | ||
import com.kakaoteck.golagola.domain.auth.dto.CustomOAuth2User; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import jakarta.transaction.Transactional; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.web.authentication.logout.LogoutHandler; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class CustomSignOutProcessHandler implements LogoutHandler { | ||
private final UserRepository userRepository; | ||
|
||
@Override | ||
@Transactional | ||
public void logout(HttpServletRequest request, HttpServletResponse response, Authentication authentication) { | ||
if (authentication == null) { | ||
return; | ||
} | ||
|
||
// 1. 테리코드 | ||
// CustomUserDetails userPrincipal = (CustomUserDetails) authentication.getPrincipal(); | ||
// userRepository.updateRefreshTokenAndLoginStatus(userPrincipal.getId(), null, false); | ||
|
||
// 2. 용우코드 | ||
CustomOAuth2User userPrincipal = (CustomOAuth2User) authentication.getPrincipal(); // CustomOAuth2User 사용 | ||
System.out.println("로그아웃 정보 확인"+ userPrincipal + userPrincipal.getUsername()); | ||
userRepository.updateRefreshTokenAndLoginStatus(userPrincipal.getUsername(), null, false); // UserEntity에서 해당 유저를 찾아서 리프레시 토큰과 로그인 상태를 업데이트 | ||
|
||
} | ||
|
||
|
||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,12 +9,13 @@ | |
import org.springframework.security.oauth2.core.user.OAuth2User; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.Optional; | ||
|
||
// DefaultOAuth2UserService: OAuth2에서 기본적으로 유저를 저장하는 메서드를 가지고 있다. | ||
// super로 상속받아서 사용한다. | ||
// OAuth2UserRequest: 리소스 서버에서 제공되는 유저정보 | ||
@Service | ||
public class CustomOAuth2UserService extends DefaultOAuth2UserService { | ||
|
||
private final UserRepository userRepository; | ||
|
||
public CustomOAuth2UserService(UserRepository userRepository) { | ||
|
@@ -41,15 +42,18 @@ public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2Authentic | |
|
||
//리소스 서버에서 발급 받은 정보로 사용자를 특정할 아이디값을 만듬 | ||
String username = oAuth2Response.getProvider() + " " + oAuth2Response.getProviderId(); | ||
UserEntity existData = userRepository.findByUsername(username); | ||
Optional<UserEntity> optionalUserEntity = userRepository.findByUsername(username); | ||
|
||
if (existData == null) { | ||
// 1. 새로운 유저라면 | ||
if (optionalUserEntity.isEmpty()) { | ||
|
||
UserEntity userEntity = new UserEntity(); | ||
userEntity.setUsername(username); | ||
userEntity.setEmail(oAuth2Response.getEmail()); | ||
userEntity.setName(oAuth2Response.getName()); | ||
userEntity.setRole("ROLE_USER"); | ||
userEntity.setUsername(username); // ex) kakao 3664463254 | ||
userEntity.setEmail(oAuth2Response.getEmail()); // ex) [email protected] | ||
userEntity.setName(oAuth2Response.getName()); // ex) 이용우 | ||
userEntity.setRole("ROLE_USER"); // ex) ROLE_USER | ||
|
||
// 리프레시 토큰 넣기 | ||
|
||
userRepository.save(userEntity); | ||
|
||
|
@@ -59,7 +63,11 @@ public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2Authentic | |
userDTO.setRole("ROLE_USER"); | ||
|
||
return new CustomOAuth2User(userDTO); | ||
} else { | ||
} | ||
|
||
// 2. 기존 유러라면 | ||
else { | ||
UserEntity existData = optionalUserEntity.get(); | ||
existData.setEmail(oAuth2Response.getEmail()); | ||
existData.setName(oAuth2Response.getName()); | ||
|
||
|
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