-
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.
Browse files
Browse the repository at this point in the history
[FEAT] 카카오 로그인 구현
- Loading branch information
Showing
10 changed files
with
225 additions
and
13 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
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
37 changes: 37 additions & 0 deletions
37
src/main/java/org/websoso/WSSServer/oauth2/CustomAuthenticationSuccessHandler.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,37 @@ | ||
package org.websoso.WSSServer.oauth2; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler; | ||
import org.springframework.stereotype.Component; | ||
import org.websoso.WSSServer.config.jwt.JwtProvider; | ||
import org.websoso.WSSServer.config.jwt.UserAuthentication; | ||
import org.websoso.WSSServer.domain.User; | ||
import org.websoso.WSSServer.oauth2.dto.CustomOAuth2User; | ||
import org.websoso.WSSServer.repository.UserRepository; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class CustomAuthenticationSuccessHandler extends SimpleUrlAuthenticationSuccessHandler { | ||
|
||
private final JwtProvider jwtProvider; | ||
private final UserRepository userRepository; | ||
|
||
@Override | ||
public void onAuthenticationSuccess(HttpServletRequest request, | ||
HttpServletResponse response, | ||
Authentication authentication) throws IOException { | ||
CustomOAuth2User customOAuth2UserDetails = (CustomOAuth2User) authentication.getPrincipal(); | ||
String socialId = customOAuth2UserDetails.getName(); | ||
User user = userRepository.findBySocialId(socialId); | ||
UserAuthentication userAuthentication = new UserAuthentication(user.getUserId(), null, null); | ||
String token = jwtProvider.generateToken(userAuthentication); | ||
|
||
response.setContentType("application/json"); | ||
response.setStatus(HttpServletResponse.SC_OK); | ||
response.getWriter().write("{\"authorization\": \"" + token + "\"}"); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/org/websoso/WSSServer/oauth2/dto/CustomOAuth2User.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,32 @@ | ||
package org.websoso.WSSServer.oauth2.dto; | ||
|
||
import java.util.Collection; | ||
import java.util.Map; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.core.GrantedAuthority; | ||
import org.springframework.security.oauth2.core.user.OAuth2User; | ||
|
||
@RequiredArgsConstructor | ||
public class CustomOAuth2User implements OAuth2User { | ||
|
||
private final OAuth2UserDTO OAuth2UserDTO; | ||
|
||
@Override | ||
public Map<String, Object> getAttributes() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public Collection<? extends GrantedAuthority> getAuthorities() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return OAuth2UserDTO.socialId(); | ||
} | ||
|
||
public String getUsername() { | ||
return OAuth2UserDTO.name(); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/org/websoso/WSSServer/oauth2/dto/KakaoOAuth2Response.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,32 @@ | ||
package org.websoso.WSSServer.oauth2.dto; | ||
|
||
import java.util.Map; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@RequiredArgsConstructor | ||
public class KakaoOAuth2Response implements OAuth2Response { | ||
|
||
private final Map<String, Object> attributes; | ||
|
||
@Override | ||
public String getProvider() { | ||
return "kakao"; | ||
} | ||
|
||
@Override | ||
public String getProviderId() { | ||
return attributes.get("id").toString(); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
Map<String, String> properties = (Map<String, String>) attributes.get("properties"); | ||
return properties.get("nickname"); | ||
} | ||
|
||
@Override | ||
public String getEmail() { | ||
Map<String, String> kakaoAccount = (Map<String, String >) attributes.get("kakao_account"); | ||
return kakaoAccount.get("email"); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/org/websoso/WSSServer/oauth2/dto/OAuth2Response.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,12 @@ | ||
package org.websoso.WSSServer.oauth2.dto; | ||
|
||
public interface OAuth2Response { | ||
|
||
String getProvider(); | ||
|
||
String getProviderId(); | ||
|
||
String getName(); | ||
|
||
String getEmail(); | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/org/websoso/WSSServer/oauth2/dto/OAuth2UserDTO.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,16 @@ | ||
package org.websoso.WSSServer.oauth2.dto; | ||
|
||
public record OAuth2UserDTO( | ||
String name, | ||
String email, | ||
String socialId | ||
) { | ||
|
||
public static OAuth2UserDTO of(String name, String email, String socialId) { | ||
return new OAuth2UserDTO( | ||
name, | ||
email, | ||
socialId | ||
); | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
src/main/java/org/websoso/WSSServer/oauth2/service/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,49 @@ | ||
package org.websoso.WSSServer.oauth2.service; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
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.OAuth2User; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import org.websoso.WSSServer.domain.User; | ||
import org.websoso.WSSServer.oauth2.dto.CustomOAuth2User; | ||
import org.websoso.WSSServer.oauth2.dto.KakaoOAuth2Response; | ||
import org.websoso.WSSServer.oauth2.dto.OAuth2Response; | ||
import org.websoso.WSSServer.oauth2.dto.OAuth2UserDTO; | ||
import org.websoso.WSSServer.repository.UserRepository; | ||
|
||
@Service | ||
@Transactional | ||
@RequiredArgsConstructor | ||
public class CustomOAuth2UserService extends DefaultOAuth2UserService { | ||
|
||
private final UserRepository userRepository; | ||
|
||
@Override | ||
public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2AuthenticationException { | ||
OAuth2User oAuth2User = super.loadUser(userRequest); | ||
String registrationId = userRequest.getClientRegistration().getRegistrationId(); | ||
|
||
OAuth2Response oAuth2Response = null; | ||
if (registrationId.equals("kakao")) { | ||
oAuth2Response = new KakaoOAuth2Response(oAuth2User.getAttributes()); | ||
} | ||
|
||
String socialId = oAuth2Response.getProvider() + "_" + oAuth2Response.getProviderId(); | ||
String defaultNickname = | ||
oAuth2Response.getProvider().charAt(0) + "*" + oAuth2Response.getProviderId().substring(2, 10); | ||
User existedUserOrNull = userRepository.findBySocialId(socialId); | ||
if (existedUserOrNull == null) { | ||
userRepository.save(User.createBySocial(socialId, defaultNickname, oAuth2Response.getEmail())); | ||
OAuth2UserDTO oAuth2UserDTO = OAuth2UserDTO.of( | ||
defaultNickname, oAuth2Response.getEmail(), socialId); | ||
return new CustomOAuth2User(oAuth2UserDTO); | ||
} else { | ||
OAuth2UserDTO oAuth2UserDTO = OAuth2UserDTO.of( | ||
existedUserOrNull.getNickname(), existedUserOrNull.getEmail(), existedUserOrNull.getSocialId()); | ||
return new CustomOAuth2User(oAuth2UserDTO); | ||
} | ||
} | ||
} |
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