-
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.
- Loading branch information
Showing
4 changed files
with
169 additions
and
30 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
43 changes: 43 additions & 0 deletions
43
api/src/main/java/com/teamnexters/lazy/api/external/kakao/KakaoClient.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,43 @@ | ||
package com.teamnexters.lazy.api.external.kakao; | ||
|
||
import com.teamnexters.lazy.api.domain.oauth.kakao.KakaoProfileDto; | ||
import com.teamnexters.lazy.api.exception.ExternalApiException; | ||
import com.teamnexters.lazy.api.exception.TokenValidException; | ||
import com.teamnexters.lazy.common.domain.member.Member; | ||
import com.teamnexters.lazy.common.domain.member.Provider; | ||
import com.teamnexters.lazy.common.domain.member.Role; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.reactive.function.client.WebClient; | ||
import reactor.core.publisher.Mono; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class KakaoClient { | ||
|
||
private final WebClient webClient; | ||
|
||
// Kakao User 정보 받아오는 URI | ||
private static final String KAKAO_USER_INFO_URI = "https://kapi.kakao.com/v2/user/me"; | ||
|
||
public Member getUserInfoByKakaoApi(String accessToken) { | ||
KakaoProfileDto kakaoUserResponse = webClient.get() | ||
.uri(KAKAO_USER_INFO_URI) | ||
.headers(h -> h.setBearerAuth(accessToken)) // Bearer 설정 | ||
.retrieve() | ||
.onStatus(HttpStatus::is4xxClientError, response -> Mono.error(new TokenValidException())) | ||
.onStatus(HttpStatus::is5xxServerError, response -> Mono.error(new ExternalApiException("GET : " + KAKAO_USER_INFO_URI))) | ||
.bodyToMono(KakaoProfileDto.class) | ||
.block(); | ||
|
||
return Member.builder() | ||
.oauthId(String.valueOf(kakaoUserResponse.getId())) | ||
.name(kakaoUserResponse.getProperties().getNickname()) | ||
.email(kakaoUserResponse.getAccount().getEmail()) | ||
.provider(Provider.KAKAO) | ||
.role(Role.USER) | ||
.picture(kakaoUserResponse.getProperties().getProfileImage()) | ||
.build(); | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
api/src/main/java/com/teamnexters/lazy/api/service/AuthService.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,83 @@ | ||
package com.teamnexters.lazy.api.service; | ||
|
||
import com.teamnexters.lazy.api.config.auth.jwt.JwtTokenProvider; | ||
import com.teamnexters.lazy.api.config.auth.jwt.Token; | ||
import com.teamnexters.lazy.api.external.kakao.KakaoClient; | ||
import com.teamnexters.lazy.common.domain.member.Member; | ||
import com.teamnexters.lazy.common.domain.member.MemberRepository; | ||
import com.teamnexters.lazy.common.domain.member.Provider; | ||
import com.teamnexters.lazy.common.domain.member.Role; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.PropertySource; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Slf4j | ||
@Service | ||
@RequiredArgsConstructor | ||
@PropertySource("classpath:application-oauth.yml") | ||
public class AuthService { | ||
|
||
@Value("${spring.security.oauth2.client.registration.kakao.auth-url}") | ||
private String kakaoAuthUrl; // Auth URL | ||
|
||
@Value("${spring.security.oauth2.client.registration.kakao.redirect-uri}") | ||
private String kakaoRedirectUri; // Redirect URI | ||
|
||
@Value("${spring.security.oauth2.client.registration.kakao.client-id}") | ||
private String kakaoRestApiKey; // REST API Key | ||
|
||
@Value("${spring.security.oauth2.client.registration.kakao.scope}") | ||
private String kakaoScope; // 추가 동의 항목 | ||
|
||
private final KakaoClient kakaoClient; | ||
private final MemberRepository memberRepository; | ||
private final JwtTokenProvider jwtTokenProvider; | ||
|
||
/** | ||
* Kakao landing URL 생성 | ||
* @return Kakao landing URL | ||
*/ | ||
public String makeKakaoLandingUrl() { | ||
StringBuffer url = new StringBuffer(); | ||
url.append(kakaoAuthUrl) | ||
.append("/oauth/authorize?") | ||
.append("client_id=") | ||
.append(kakaoRestApiKey) | ||
.append("&redirect_uri=") | ||
.append(kakaoRedirectUri) | ||
.append("&scope=") | ||
.append(kakaoScope) | ||
.append("&response_type=") | ||
.append("code"); // code 고정 | ||
|
||
log.info("Kakao Login Landing URL : {}", url); | ||
|
||
return url.toString(); | ||
} | ||
|
||
/** | ||
* 카카오 API 통해 회원 정보 가져와서 생성 후 Token 발행 | ||
* | ||
* @param kakaoToken Kakao Token | ||
* @return App Server Token | ||
*/ | ||
@Transactional | ||
public Token getTokenAndSaveMemberAfterKakaoUserApi(String kakaoToken) { | ||
// Kakao External Client API 호출 | ||
Member kakaoUserInfo = kakaoClient.getUserInfoByKakaoApi(kakaoToken); | ||
String oauthId = kakaoUserInfo.getOauthId(); | ||
// Member | ||
Member member = memberRepository.findByOauthIdAndProvider(oauthId, Provider.KAKAO).orElseGet( | ||
() -> memberRepository.save(kakaoUserInfo) | ||
); | ||
|
||
// Token 생성 | ||
Token token = jwtTokenProvider.createToken(member.getEmail(), Role.USER); | ||
log.info("### Token : [ {} ]", token); | ||
return token; | ||
} | ||
|
||
} |