-
Notifications
You must be signed in to change notification settings - Fork 3
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 #14 from AlongTheBlue/develop
[Feat] 소셜 로그인 - 카카오 로그인 구현
- Loading branch information
Showing
7 changed files
with
178 additions
and
3 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
src/main/java/org/alongtheblue/alongtheblue_server/domain/oauth/api/OauthController.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,39 @@ | ||
package org.alongtheblue.alongtheblue_server.domain.oauth.api; | ||
|
||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import lombok.RequiredArgsConstructor; | ||
import org.alongtheblue.alongtheblue_server.domain.oauth.application.KakaoService; | ||
import org.alongtheblue.alongtheblue_server.domain.userInfo.application.UserInfoService; | ||
import org.alongtheblue.alongtheblue_server.domain.userInfo.domain.UserInfo; | ||
import org.alongtheblue.alongtheblue_server.global.common.response.ApiResponse; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.util.Map; | ||
|
||
@Tag(name = "회원가입/로그인 API", description = "회원가입, 로그인, 사용자 정보 조회 API") | ||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/oauth") | ||
public class OauthController { | ||
private final KakaoService kakaoService; | ||
private final UserInfoService userinfoService; | ||
|
||
@Operation(summary="리다이렉션 url 반환 API") | ||
@GetMapping("/login") | ||
public ApiResponse<String> getKakaoLoginUrl() { | ||
return kakaoService.getKakaoLoginUrl(); | ||
} | ||
|
||
@Operation(summary="로그인 후 콜백 메소드") | ||
@GetMapping("/callback") | ||
public ApiResponse<String> kakaoLogin(@RequestParam("code") String code) { | ||
System.out.println("0000000000000000000000000"); | ||
String accessToken = kakaoService.getAccessToken(code); | ||
Map<String, Object> userInfo = kakaoService.getUserInfo(accessToken); | ||
return ApiResponse.ok("사용자의 user ID를 성공적으로 조회했습니다.", userinfoService.retrieveOrCreateUser(userInfo).getData().getUid()); | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
...main/java/org/alongtheblue/alongtheblue_server/domain/oauth/application/KakaoService.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,80 @@ | ||
package org.alongtheblue.alongtheblue_server.domain.oauth.application; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.alongtheblue.alongtheblue_server.global.common.response.ApiResponse; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.core.ParameterizedTypeReference; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import org.springframework.web.reactive.function.client.WebClient; | ||
|
||
import java.util.Map; | ||
|
||
@Service | ||
@Transactional | ||
@RequiredArgsConstructor | ||
public class KakaoService { | ||
|
||
@Value("${security.oauth2.client.registration.kakao.client-id}") | ||
private String clientId; | ||
|
||
@Value("${security.oauth2.client.registration.kakao.client-secret}") | ||
private String clientSecret; | ||
|
||
@Value("${security.oauth2.client.registration.kakao.redirect-uri}") | ||
private String redirectUri; | ||
|
||
@Value("${security.oauth2.client.provider.kakao.authorization-uri}") | ||
private String authUrl; | ||
|
||
@Value("${security.oauth2.client.provider.kakao.token-uri}") | ||
private String tokenUri; | ||
|
||
@Value("${security.oauth2.client.provider.kakao.user-info-uri}") | ||
private String userInfoUri; | ||
|
||
private final WebClient webClient; | ||
|
||
@Autowired | ||
public KakaoService(WebClient.Builder webClientBuilder) { | ||
this.webClient = webClientBuilder.baseUrl("https://kauth.kakao.com").build(); | ||
} | ||
|
||
public ApiResponse<String> getKakaoLoginUrl(){ | ||
String kakaoAuthUrl = String.format("%s?client_id=%s&redirect_uri=%s&response_type=code", | ||
authUrl, clientId, redirectUri); | ||
return ApiResponse.ok("Redirect to Kakao", kakaoAuthUrl); | ||
} | ||
|
||
public String getAccessToken(String code) { | ||
return webClient.post() | ||
.uri(uriBuilder -> uriBuilder.path("/oauth/token") | ||
.queryParam("grant_type", "authorization_code") | ||
.queryParam("client_id", clientId) | ||
.queryParam("client_secret", clientSecret) | ||
.queryParam("redirect_uri", redirectUri) | ||
.queryParam("code", code) | ||
.build()) | ||
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED_VALUE) | ||
.retrieve() | ||
.bodyToMono(Map.class) | ||
.map(response -> (String) response.get("access_token")) | ||
.block(); // 비동기 처리 시 block() 사용 자제, 여기서는 예제 단순화를 위해 사용 | ||
} | ||
|
||
public Map<String, Object> getUserInfo(String accessToken) { | ||
return webClient.mutate() | ||
.baseUrl("https://kapi.kakao.com") | ||
.build() | ||
.get() | ||
.uri("/v2/user/me") | ||
// .uri("https://kapi.kakao.com/v2/user/me") | ||
.header(HttpHeaders.AUTHORIZATION, "Bearer " + accessToken) | ||
.retrieve() | ||
.bodyToMono(new ParameterizedTypeReference<Map<String, Object>>() {}) | ||
.block(); // 비동기 처리 시 block() 사용 자제, 여기서는 예제 단순화를 위해 사용 | ||
} | ||
} |
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