-
Notifications
You must be signed in to change notification settings - Fork 1
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
카카오 로그인 API 구현
- Loading branch information
Showing
32 changed files
with
1,070 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
22 changes: 22 additions & 0 deletions
22
src/main/java/com/ajou/hertz/common/auth/dto/request/KakaoLoginRequest.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,22 @@ | ||
package com.ajou.hertz.common.auth.dto.request; | ||
|
||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import jakarta.validation.constraints.NotBlank; | ||
import lombok.AccessLevel; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@AllArgsConstructor(access = AccessLevel.PRIVATE) | ||
@NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
@Getter | ||
public class KakaoLoginRequest { | ||
|
||
@Schema(description = "카카오 '인가 코드 받기'를 통해 얻은 인가 코드", example = "v9rZPkB2nyKTFX26E8ByNzUojqi6w9bQRyfBQCFDzRFhxcoUUSVpI_3HgPIKlwzSAAABjbztS6bkNSpXBP-m7E") | ||
@NotBlank | ||
private String authorizationCode; | ||
|
||
@Schema(description = "인가 코드가 리다이렉트된 uri", example = "https://hertz.com/kakao-login-redirection") | ||
@NotBlank | ||
private String redirectUri; | ||
} |
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
9 changes: 9 additions & 0 deletions
9
src/main/java/com/ajou/hertz/common/config/feign/FeignConfig.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,9 @@ | ||
package com.ajou.hertz.common.config.feign; | ||
|
||
import org.springframework.cloud.openfeign.EnableFeignClients; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@EnableFeignClients(basePackages = "com.ajou.hertz") | ||
@Configuration | ||
public class FeignConfig { | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/com/ajou/hertz/common/config/feign/KakaoFeignConfig.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,24 @@ | ||
package com.ajou.hertz.common.config.feign; | ||
|
||
import org.springframework.beans.factory.ObjectFactory; | ||
import org.springframework.boot.autoconfigure.http.HttpMessageConverters; | ||
import org.springframework.cloud.openfeign.support.SpringEncoder; | ||
import org.springframework.context.annotation.Bean; | ||
|
||
import feign.Logger; | ||
import feign.codec.Encoder; | ||
import feign.codec.ErrorDecoder; | ||
import feign.form.spring.SpringFormEncoder; | ||
|
||
public class KakaoFeignConfig { | ||
|
||
@Bean | ||
public Encoder encoder(ObjectFactory<HttpMessageConverters> converters) { | ||
return new SpringFormEncoder(new SpringEncoder(converters)); | ||
} | ||
|
||
@Bean | ||
public ErrorDecoder errorDecoder() { | ||
return new KakaoFeignErrorDecoder(); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/main/java/com/ajou/hertz/common/config/feign/KakaoFeignErrorDecoder.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,35 @@ | ||
package com.ajou.hertz.common.config.feign; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
import org.springframework.http.HttpStatus; | ||
|
||
import com.ajou.hertz.common.exception.IOProcessingException; | ||
import com.ajou.hertz.common.kakao.dto.response.KakaoErrorResponse; | ||
import com.ajou.hertz.common.kakao.exception.KakaoClientException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
import feign.Response; | ||
import feign.codec.ErrorDecoder; | ||
|
||
public class KakaoFeignErrorDecoder implements ErrorDecoder { | ||
|
||
private final ObjectMapper mapper; | ||
|
||
public KakaoFeignErrorDecoder() { | ||
this.mapper = new ObjectMapper(); | ||
} | ||
|
||
@Override | ||
public Exception decode(String methodKey, Response response) { | ||
try (InputStream responseBody = response.body().asInputStream()) { | ||
return new KakaoClientException( | ||
HttpStatus.valueOf(response.status()), | ||
mapper.readValue(responseBody, KakaoErrorResponse.class) | ||
); | ||
} catch (IOException e) { | ||
return new IOProcessingException(e); | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/com/ajou/hertz/common/exception/IOProcessingException.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,18 @@ | ||
package com.ajou.hertz.common.exception; | ||
|
||
import java.io.IOException; | ||
|
||
import com.ajou.hertz.common.exception.constant.CustomExceptionType; | ||
|
||
/** | ||
* <p>I/O 작업에서 문제가 발생했을 때 사용하는 일반적인 exception class. | ||
* <p>Checked exception을 {@link IOException}을 감싸 처리하는 용도로도 사용한다. | ||
* | ||
* @see IOException | ||
*/ | ||
public class IOProcessingException extends InternalServerException { | ||
|
||
public IOProcessingException(Throwable cause) { | ||
super(CustomExceptionType.IO_PROCESSING, cause); | ||
} | ||
} |
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
23 changes: 23 additions & 0 deletions
23
src/main/java/com/ajou/hertz/common/kakao/client/KakaoAuthClient.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,23 @@ | ||
package com.ajou.hertz.common.kakao.client; | ||
|
||
import org.springframework.cloud.openfeign.FeignClient; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
|
||
import com.ajou.hertz.common.config.feign.KakaoFeignConfig; | ||
import com.ajou.hertz.common.kakao.dto.request.IssueKakaoAccessTokenRequest; | ||
import com.ajou.hertz.common.kakao.dto.response.KakaoTokenResponse; | ||
|
||
@FeignClient( | ||
name = "kakaoAuthClient", | ||
url = "https://kauth.kakao.com", | ||
configuration = KakaoFeignConfig.class | ||
) | ||
public interface KakaoAuthClient { | ||
|
||
@PostMapping( | ||
value = "/oauth/token", | ||
headers = "Content-type=application/x-www-form-urlencoded;charset=utf-8" | ||
) | ||
KakaoTokenResponse issueAccessToken(@RequestBody IssueKakaoAccessTokenRequest issueTokenRequest); | ||
} |
26 changes: 26 additions & 0 deletions
26
src/main/java/com/ajou/hertz/common/kakao/client/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,26 @@ | ||
package com.ajou.hertz.common.kakao.client; | ||
|
||
import org.springframework.cloud.openfeign.FeignClient; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestHeader; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
|
||
import com.ajou.hertz.common.config.feign.KakaoFeignConfig; | ||
import com.ajou.hertz.common.kakao.dto.response.KakaoUserInfoResponse; | ||
|
||
@FeignClient( | ||
name = "kakaoClient", | ||
url = "https://kapi.kakao.com", | ||
configuration = KakaoFeignConfig.class | ||
) | ||
public interface KakaoClient { | ||
|
||
@GetMapping( | ||
value = "/v2/user/me", | ||
headers = "Content-type=application/x-www-form-urlencoded;charset=utf-8" | ||
) | ||
KakaoUserInfoResponse getUserInfo( | ||
@RequestHeader("Authorization") String authorizationHeader, | ||
@RequestParam("secure_resource") Boolean secureResource | ||
); | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/com/ajou/hertz/common/kakao/dto/request/IssueKakaoAccessTokenRequest.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,18 @@ | ||
package com.ajou.hertz.common.kakao.dto.request; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@AllArgsConstructor | ||
@NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
@Getter | ||
public class IssueKakaoAccessTokenRequest { | ||
|
||
private String grant_type; | ||
private String client_id; | ||
private String redirect_uri; | ||
private String code; | ||
private String client_secret; | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/ajou/hertz/common/kakao/dto/response/KakaoErrorResponse.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,10 @@ | ||
package com.ajou.hertz.common.kakao.dto.response; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
public record KakaoErrorResponse( | ||
String error, | ||
@JsonProperty("error_description") String errorDescription, | ||
@JsonProperty("error_code") String errorCode | ||
) { | ||
} |
29 changes: 29 additions & 0 deletions
29
src/main/java/com/ajou/hertz/common/kakao/dto/response/KakaoTokenResponse.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,29 @@ | ||
package com.ajou.hertz.common.kakao.dto.response; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@AllArgsConstructor(access = AccessLevel.PRIVATE) | ||
@NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
@Getter | ||
public class KakaoTokenResponse { | ||
|
||
@JsonProperty("token_type") | ||
private String tokenType; | ||
|
||
@JsonProperty("access_token") | ||
private String accessToken; | ||
|
||
@JsonProperty("expires_in") | ||
private Integer expiresIn; | ||
|
||
@JsonProperty("refresh_token") | ||
private String refreshToken; | ||
|
||
@JsonProperty("refresh_token_expires_in") | ||
private Integer refreshTokenExpiresIn; | ||
} |
Oops, something went wrong.