-
Notifications
You must be signed in to change notification settings - Fork 3
인증서버 내 kakao social login을 통한 회원가입 #86
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ba7ca66
#27 feat(be) : user엔티티 구현
bo-ram-bo-ram 0ccaffc
#27 feat(be) : jwt를 활용한 kakao social 로그인 구현
bo-ram-bo-ram a5a816f
#27 feat(be) : swagger를 통한 user Controller 문서화 구현
bo-ram-bo-ram 03c0736
#27 fix(be) : swagger 관련 security whitelist 경로 수정
bo-ram-bo-ram dd3d437
#27 fix(be) : swagger 관련 request example 수정
bo-ram-bo-ram File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
7 changes: 7 additions & 0 deletions
7
src/backend/auth_server/src/main/java/com/jootalkpia/auth_server/AuthServerApplication.java
This file contains hidden or 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
20 changes: 20 additions & 0 deletions
20
...end/auth_server/src/main/java/com/jootalkpia/auth_server/client/dto/UserInfoResponse.java
This file contains hidden or 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,20 @@ | ||
| package com.jootalkpia.auth_server.client.dto; | ||
|
|
||
|
|
||
| import com.jootalkpia.auth_server.user.domain.SocialType; | ||
|
|
||
| public record UserInfoResponse( | ||
| Long socialId, | ||
| SocialType socialType, | ||
| String email, | ||
| String socialNickname | ||
| ) { | ||
| public static UserInfoResponse of( | ||
| final Long socialId, | ||
| final SocialType socialType, | ||
| final String email, | ||
| final String socialNickname | ||
| ) { | ||
| return new UserInfoResponse(socialId, socialType, email, socialNickname); | ||
| } | ||
| } |
17 changes: 17 additions & 0 deletions
17
...end/auth_server/src/main/java/com/jootalkpia/auth_server/client/dto/UserLoginRequest.java
This file contains hidden or 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,17 @@ | ||
| package com.jootalkpia.auth_server.client.dto; | ||
|
|
||
| import com.jootalkpia.auth_server.user.domain.SocialType; | ||
| import io.swagger.v3.oas.annotations.media.Schema; | ||
| import jakarta.validation.constraints.NotBlank; | ||
| import jakarta.validation.constraints.NotNull; | ||
|
|
||
| public record UserLoginRequest( | ||
| @NotNull(message = "소셜 로그인 종류가 입력되지 않았습니다.") | ||
| @Schema(description = "소셜로그인 타입", example = "KAKAO") | ||
| SocialType socialType, | ||
|
|
||
| @NotBlank(message = "redirectUri가 입력되지 않았습니다.") | ||
| @Schema(description = "리다이텍트 uri 값", example = "http://localhost:5173/kakao/redirection") | ||
| String redirectUri | ||
| ) { | ||
| } |
15 changes: 15 additions & 0 deletions
15
...end/auth_server/src/main/java/com/jootalkpia/auth_server/client/kakao/KakaoApiClient.java
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 경우에는 게이트웨이에서 passport를 주지 않고 헤더를 직접 확인하는 방식으로 하는걸까요?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. passport는 토큰을 기반으로 만들어지게 되는데요. 회원가입 부분은 토큰이 없는 상태로 응답해야하는 api 입니당! |
This file contains hidden or 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,15 @@ | ||
| package com.jootalkpia.auth_server.client.kakao; | ||
|
|
||
|
|
||
| import com.jootalkpia.auth_server.client.kakao.response.KakaoUserResponse; | ||
| import org.springframework.cloud.openfeign.FeignClient; | ||
| import org.springframework.http.HttpHeaders; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
| import org.springframework.web.bind.annotation.RequestHeader; | ||
|
|
||
| @FeignClient(name = "kakaoApiClient", url = "https://kapi.kakao.com") | ||
| public interface KakaoApiClient { | ||
|
|
||
| @GetMapping(value = "/v2/user/me") | ||
| KakaoUserResponse getUserInformation(@RequestHeader(HttpHeaders.AUTHORIZATION) String accessToken); | ||
| } |
19 changes: 19 additions & 0 deletions
19
...auth_server/src/main/java/com/jootalkpia/auth_server/client/kakao/KakaoAuthApiClient.java
This file contains hidden or 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,19 @@ | ||
| package com.jootalkpia.auth_server.client.kakao; | ||
|
|
||
|
|
||
| import com.jootalkpia.auth_server.client.kakao.response.KakaoAccessTokenResponse; | ||
| import org.springframework.cloud.openfeign.FeignClient; | ||
| import org.springframework.http.MediaType; | ||
| import org.springframework.web.bind.annotation.PostMapping; | ||
| import org.springframework.web.bind.annotation.RequestParam; | ||
|
|
||
| @FeignClient(name = "kakaoAuthApiClient", url = "https://kauth.kakao.com") | ||
| public interface KakaoAuthApiClient { | ||
| @PostMapping(value = "/oauth/token", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE) | ||
| KakaoAccessTokenResponse getOAuth2AccessToken( | ||
| @RequestParam("grant_type") String grantType, | ||
| @RequestParam("client_id") String clientId, | ||
| @RequestParam("redirect_uri") String redirectUri, | ||
| @RequestParam("code") String code | ||
| ); | ||
| } |
77 changes: 77 additions & 0 deletions
77
...auth_server/src/main/java/com/jootalkpia/auth_server/client/kakao/KakaoSocialService.java
This file contains hidden or 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,77 @@ | ||
| package com.jootalkpia.auth_server.client.kakao; | ||
|
|
||
| import com.jootalkpia.auth_server.client.dto.UserInfoResponse; | ||
| import com.jootalkpia.auth_server.client.dto.UserLoginRequest; | ||
| import com.jootalkpia.auth_server.client.kakao.response.KakaoAccessTokenResponse; | ||
| import com.jootalkpia.auth_server.client.kakao.response.KakaoUserResponse; | ||
| import com.jootalkpia.auth_server.client.service.SocialService; | ||
| import com.jootalkpia.auth_server.exception.CustomException; | ||
| import com.jootalkpia.auth_server.response.ErrorCode; | ||
| import com.jootalkpia.auth_server.user.domain.SocialType; | ||
| import feign.FeignException; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| @Service | ||
| @Slf4j | ||
| @RequiredArgsConstructor | ||
| public class KakaoSocialService implements SocialService { | ||
|
|
||
| private static final String AUTH_CODE = "authorization_code"; | ||
|
|
||
| @Value("${kakao.clientId}") | ||
| private String clientId; | ||
| private final KakaoApiClient kakaoApiClient; | ||
| private final KakaoAuthApiClient kakaoAuthApiClient; | ||
|
|
||
| @Transactional | ||
| @Override | ||
| public UserInfoResponse login( | ||
| final String authorizationCode, | ||
| final UserLoginRequest loginRequest | ||
| ) { | ||
| String accessToken; | ||
| try { | ||
| // 인가 코드로 Access Token + Refresh Token 받아오기 | ||
| accessToken = getOAuth2Authentication(authorizationCode, loginRequest.redirectUri()); | ||
| } catch (FeignException e) { | ||
| throw new CustomException(ErrorCode.AUTHENTICATION_CODE_EXPIRED); | ||
| } | ||
| // Access Token으로 유저 정보 불러오기 | ||
| return getLoginDto(loginRequest.socialType(), getUserInfo(accessToken)); | ||
| } | ||
|
|
||
| private String getOAuth2Authentication( | ||
| final String authorizationCode, | ||
| final String redirectUri | ||
| ) { | ||
| KakaoAccessTokenResponse response = kakaoAuthApiClient.getOAuth2AccessToken( | ||
| AUTH_CODE, | ||
| clientId, | ||
| redirectUri, | ||
| authorizationCode | ||
| ); | ||
| return response.accessToken(); | ||
| } | ||
|
|
||
| private KakaoUserResponse getUserInfo( | ||
| final String accessToken | ||
| ) { | ||
| return kakaoApiClient.getUserInformation("Bearer " + accessToken); | ||
| } | ||
|
|
||
| private UserInfoResponse getLoginDto( | ||
| final SocialType socialType, | ||
| final KakaoUserResponse userResponse | ||
| ) { | ||
| return UserInfoResponse.of( | ||
| userResponse.id(), | ||
| socialType, | ||
| userResponse.kakaoAccount().email(), | ||
| userResponse.kakaoAccount().profile().nickname() | ||
| ); | ||
| } | ||
| } |
17 changes: 17 additions & 0 deletions
17
.../main/java/com/jootalkpia/auth_server/client/kakao/response/KakaoAccessTokenResponse.java
This file contains hidden or 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,17 @@ | ||
| package com.jootalkpia.auth_server.client.kakao.response; | ||
|
|
||
| import com.fasterxml.jackson.databind.PropertyNamingStrategies; | ||
| import com.fasterxml.jackson.databind.annotation.JsonNaming; | ||
|
|
||
| @JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class) | ||
| public record KakaoAccessTokenResponse( | ||
| String accessToken | ||
| ) { | ||
| public static KakaoAccessTokenResponse of( | ||
| final String accessToken | ||
| ) { | ||
| return new KakaoAccessTokenResponse( | ||
| accessToken | ||
| ); | ||
| } | ||
| } |
11 changes: 11 additions & 0 deletions
11
...h_server/src/main/java/com/jootalkpia/auth_server/client/kakao/response/KakaoAccount.java
This file contains hidden or 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,11 @@ | ||
| package com.jootalkpia.auth_server.client.kakao.response; | ||
|
|
||
| import com.fasterxml.jackson.databind.PropertyNamingStrategies; | ||
| import com.fasterxml.jackson.databind.annotation.JsonNaming; | ||
|
|
||
| @JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class) | ||
| public record KakaoAccount( | ||
| String email, | ||
| KakaoUserProfile profile | ||
| ) { | ||
| } |
10 changes: 10 additions & 0 deletions
10
...rver/src/main/java/com/jootalkpia/auth_server/client/kakao/response/KakaoUserProfile.java
This file contains hidden or 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,10 @@ | ||
| package com.jootalkpia.auth_server.client.kakao.response; | ||
|
|
||
| import com.fasterxml.jackson.databind.PropertyNamingStrategies; | ||
| import com.fasterxml.jackson.databind.annotation.JsonNaming; | ||
|
|
||
| @JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class) | ||
| public record KakaoUserProfile( | ||
| String nickname | ||
| ) { | ||
| } |
11 changes: 11 additions & 0 deletions
11
...ver/src/main/java/com/jootalkpia/auth_server/client/kakao/response/KakaoUserResponse.java
This file contains hidden or 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,11 @@ | ||
| package com.jootalkpia.auth_server.client.kakao.response; | ||
|
|
||
| import com.fasterxml.jackson.databind.PropertyNamingStrategies; | ||
| import com.fasterxml.jackson.databind.annotation.JsonNaming; | ||
|
|
||
| @JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class) | ||
| public record KakaoUserResponse( | ||
| Long id, | ||
| KakaoAccount kakaoAccount | ||
| ) { | ||
| } |
9 changes: 9 additions & 0 deletions
9
...nd/auth_server/src/main/java/com/jootalkpia/auth_server/client/service/SocialService.java
This file contains hidden or 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,9 @@ | ||
| package com.jootalkpia.auth_server.client.service; | ||
|
|
||
|
|
||
| import com.jootalkpia.auth_server.client.dto.UserInfoResponse; | ||
| import com.jootalkpia.auth_server.client.dto.UserLoginRequest; | ||
|
|
||
| public interface SocialService { | ||
| UserInfoResponse login(final String authorizationToken, final UserLoginRequest loginRequest); | ||
| } |
This file contains hidden or 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
12 changes: 12 additions & 0 deletions
12
src/backend/auth_server/src/main/java/com/jootalkpia/auth_server/jwt/TokenRepository.java
This file contains hidden or 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 com.jootalkpia.auth_server.jwt; | ||
|
|
||
| import com.jootalkpia.auth_server.redis.Token; | ||
| import java.util.Optional; | ||
| import org.springframework.data.repository.CrudRepository; | ||
|
|
||
| public interface TokenRepository extends CrudRepository<Token, Long> { | ||
|
|
||
| Optional<Token> findByRefreshToken(final String refreshToken); | ||
|
|
||
| Optional<Token> findById(final Long id); | ||
| } |
20 changes: 20 additions & 0 deletions
20
src/backend/auth_server/src/main/java/com/jootalkpia/auth_server/jwt/TokenService.java
This file contains hidden or 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,20 @@ | ||
| package com.jootalkpia.auth_server.jwt; | ||
|
|
||
| import com.jootalkpia.auth_server.redis.Token; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| @RequiredArgsConstructor | ||
| @Service | ||
| public class TokenService { | ||
|
|
||
| private final TokenRepository tokenRepository; | ||
|
|
||
| @Transactional | ||
| public void saveRefreshToken(final Long userId, final String refreshToken) { | ||
| tokenRepository.save( | ||
| Token.of(userId, refreshToken) | ||
| ); | ||
| } | ||
| } |
This file contains hidden or 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
27 changes: 27 additions & 0 deletions
27
.../auth_server/src/main/java/com/jootalkpia/auth_server/user/controller/UserController.java
This file contains hidden or 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,27 @@ | ||
| package com.jootalkpia.auth_server.user.controller; | ||
|
|
||
| import com.jootalkpia.auth_server.client.dto.UserLoginRequest; | ||
| import com.jootalkpia.auth_server.user.dto.LoginSuccessResponse; | ||
| import com.jootalkpia.auth_server.user.service.UserService; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.PostMapping; | ||
| import org.springframework.web.bind.annotation.RequestBody; | ||
| import org.springframework.web.bind.annotation.RequestParam; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| @RestController | ||
| @RequiredArgsConstructor | ||
| public class UserController implements UserControllerDocs { | ||
|
|
||
| private final UserService userService; | ||
|
|
||
| @Override | ||
| @PostMapping("api/v1/user/login") | ||
| public ResponseEntity<LoginSuccessResponse> login( | ||
| @RequestParam final String authorizationCode, | ||
| @RequestBody final UserLoginRequest loginRequest | ||
| ) { | ||
| return ResponseEntity.ok().body(userService.create(authorizationCode, loginRequest)); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
localhost로 되어있는 부분 경로를 환경변수처리하면 나중에 배포 환경에 적합하게 바꿀 때 편할 것 같아요!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
오 좋은 부분이에요 감사합니다! 다만 redirectUri를 request에 클라이언트가 보내주시는건데 @Schema를 통해 예시 들어드리는 부분입니당! 저희가 따로 처리하는 부분은 아닌데 어떻게할까요??