-
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.
- Loading branch information
Showing
45 changed files
with
1,794 additions
and
110 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
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
20 changes: 20 additions & 0 deletions
20
src/main/kotlin/org/store/clothstar/common/config/WebConfig.kt
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,20 @@ | ||
package org.store.clothstar.common.config | ||
|
||
import org.springframework.context.annotation.Configuration | ||
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer | ||
|
||
@Configuration | ||
class WebConfig : WebMvcConfigurer { | ||
|
||
override fun addResourceHandlers(registry: ResourceHandlerRegistry) { | ||
registry.addResourceHandler("/favicon.ico") | ||
.addResourceLocations("classpath:/static/") | ||
registry.addResourceHandler("/js/**") | ||
.addResourceLocations("classpath:/static/js/") | ||
registry.addResourceHandler("/css/**") | ||
.addResourceLocations("classpath:/static/css/") | ||
registry.addResourceHandler("/images/**") | ||
.addResourceLocations("classpath:/static/images/") | ||
} | ||
} |
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
2 changes: 1 addition & 1 deletion
2
src/main/kotlin/org/store/clothstar/common/dto/SaveResponseDTO.kt
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
package org.store.clothstar.common.dto | ||
|
||
class SaveResponseDTO( | ||
val id: Long, | ||
val id: Long? = null, | ||
val statusCode: Int, | ||
val message: String, | ||
) |
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
7 changes: 7 additions & 0 deletions
7
src/main/kotlin/org/store/clothstar/common/error/exception/InvalidSignupMemberRequest.kt
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,7 @@ | ||
package org.store.clothstar.common.error.exception | ||
|
||
import org.store.clothstar.common.error.ErrorCode | ||
|
||
class InvalidSignupMemberRequest( | ||
val errorCode: ErrorCode | ||
) : RuntimeException(errorCode.message) |
7 changes: 7 additions & 0 deletions
7
src/main/kotlin/org/store/clothstar/common/error/exception/InvalidSignupType.kt
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,7 @@ | ||
package org.store.clothstar.common.error.exception | ||
|
||
import org.store.clothstar.common.error.ErrorCode | ||
|
||
class InvalidSignupType( | ||
val errorCode: ErrorCode | ||
) : RuntimeException(errorCode.message) |
18 changes: 18 additions & 0 deletions
18
src/main/kotlin/org/store/clothstar/kakaoLogin/controller/KakaoController.kt
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 org.store.clothstar.kakaoLogin.controller | ||
|
||
import org.springframework.http.ResponseEntity | ||
import org.springframework.web.bind.annotation.GetMapping | ||
import org.springframework.web.bind.annotation.RequestParam | ||
import org.springframework.web.bind.annotation.RestController | ||
import org.store.clothstar.kakaoLogin.service.KakaoLoginService | ||
|
||
@RestController | ||
class KakaoController( | ||
private val kakaoLoginService: KakaoLoginService, | ||
) { | ||
// 인가코드 받기 | ||
@GetMapping("/auth/kakao/callback") | ||
fun kakaoCallback(@RequestParam code: String): ResponseEntity<String> { | ||
return ResponseEntity.ok(code) | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/kotlin/org/store/clothstar/kakaoLogin/controller/KakaoLoginPageController.kt
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,25 @@ | ||
package org.store.clothstar.kakaoLogin.controller | ||
|
||
import org.springframework.beans.factory.annotation.Value | ||
import org.springframework.stereotype.Controller | ||
import org.springframework.ui.Model | ||
import org.springframework.web.bind.annotation.GetMapping | ||
|
||
@Controller | ||
class KakaoLoginPageController { | ||
|
||
@Value("\${spring.security.oauth2.client.registration.kakao.client_id}") | ||
private lateinit var clientId: String | ||
|
||
@Value("\${spring.security.oauth2.client.registration.kakao.redirect_uri}") | ||
private lateinit var redirectUri: String | ||
|
||
@GetMapping("/kakaoLogin/page") | ||
fun loginPage(model: Model): String { | ||
val location = | ||
"https://kauth.kakao.com/oauth/authorize?response_type=code&client_id=$clientId&redirect_uri=$redirectUri" | ||
model.addAttribute("location", location) | ||
|
||
return "kakaoLogin" | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/main/kotlin/org/store/clothstar/kakaoLogin/dto/KakaoTokenResponseDto.kt
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,31 @@ | ||
package org.store.clothstar.kakaoLogin.dto | ||
|
||
import com.fasterxml.jackson.annotation.JsonCreator | ||
import com.fasterxml.jackson.annotation.JsonProperty | ||
|
||
data class KakaoTokenResponseDto @JsonCreator constructor( | ||
|
||
// 토큰 타입, bearer로 고정 | ||
@JsonProperty("token_type") | ||
private val tokenType: String? = null, | ||
|
||
// 사용자 액세스 토큰 값 | ||
@JsonProperty("access_token") | ||
val accessToken: String? = null, | ||
|
||
// 액세스 토큰과 ID 토큰의 만료 시간(초) | ||
@JsonProperty("expires_in") | ||
private val expiresIn: Int? = null, | ||
|
||
// 사용자 리프레시 토큰 값 | ||
@JsonProperty("refresh_token") | ||
private val refreshToken: String? = null, | ||
|
||
// 리프레시 토큰 만료 시간(초) | ||
@JsonProperty("refresh_token_expires_in") | ||
private val refreshTokenExpiresIn: Int? = null, | ||
|
||
// 인증된 사용자의 정보 조회 권한 범위 / 범위가 여러 개일 경우, 공백으로 구분 | ||
@JsonProperty("scope") | ||
private val scope: String? = null, | ||
) |
16 changes: 16 additions & 0 deletions
16
src/main/kotlin/org/store/clothstar/kakaoLogin/dto/KakaoUserInfoResponseDto.kt
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.store.clothstar.kakaoLogin.dto | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty | ||
import org.store.clothstar.kakaoLogin.vo.KakaoAccount | ||
import org.store.clothstar.kakaoLogin.vo.Properties | ||
|
||
class KakaoUserInfoResponseDto( | ||
// 회원번호 | ||
@JsonProperty("id") val id: Long? = null, | ||
// 서비스에 연결 완료된 시각, UTC* | ||
@JsonProperty("connected_at") val connectedAt: String? = null, | ||
// 카카오계정 정보 | ||
@JsonProperty("kakao_account") val kakaoAccount: KakaoAccount? = null, | ||
// 사용자 프로퍼티(Property) | ||
@JsonProperty("properties") val properties: Properties? = null, | ||
) |
87 changes: 87 additions & 0 deletions
87
src/main/kotlin/org/store/clothstar/kakaoLogin/service/KakaoLoginService.kt
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,87 @@ | ||
package org.store.clothstar.kakaoLogin.service | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import io.github.oshai.kotlinlogging.KotlinLogging | ||
import org.springframework.beans.factory.annotation.Value | ||
import org.springframework.scheduling.annotation.EnableScheduling | ||
import org.springframework.stereotype.Service | ||
import org.springframework.util.LinkedMultiValueMap | ||
import org.springframework.util.MultiValueMap | ||
import org.springframework.web.reactive.function.BodyInserters | ||
import org.springframework.web.reactive.function.client.WebClient | ||
import org.store.clothstar.kakaoLogin.dto.KakaoTokenResponseDto | ||
import org.store.clothstar.kakaoLogin.dto.KakaoUserInfoResponseDto | ||
|
||
@EnableScheduling | ||
@Service | ||
class KakaoLoginService { | ||
|
||
private val logger = KotlinLogging.logger {} | ||
|
||
@Value("\${spring.security.oauth2.client.registration.kakao.client_id}") | ||
private lateinit var clientId: String | ||
|
||
@Value("\${spring.security.oauth2.client.registration.kakao.client_secret}") | ||
private lateinit var clientSecret: String | ||
|
||
@Value("\${spring.security.oauth2.client.registration.kakao.redirect_uri}") | ||
private lateinit var redirectUri: String | ||
|
||
@Value("\${spring.security.oauth2.client.provider.kakao.token_uri}") | ||
lateinit var tokenUri: String | ||
|
||
@Value("\${spring.security.oauth2.client.provider.kakao.user_info_uri}") | ||
lateinit var userUri: String | ||
|
||
// 토큰 가져오기 | ||
fun getAccessToken(code: String): KakaoTokenResponseDto { | ||
// 토큰 요청 데이터 | ||
val params: MultiValueMap<String, String> = LinkedMultiValueMap() | ||
params.add("code", code) | ||
params.add("client_secret", clientSecret) | ||
params.add("client_id", clientId) | ||
params.add("grant_type", "authorization_code") | ||
params.add("redirect_url", redirectUri) | ||
|
||
logger.info { "Requesting token with params: $params" } | ||
|
||
// 웹 클라이언트로 요청 보내기 | ||
val response = WebClient.create(tokenUri) | ||
.post() | ||
.body(BodyInserters.fromFormData(params)) | ||
.header("Content-Type", "application/x-www-form-urlencoded;charset=utf-8") | ||
.retrieve() | ||
.bodyToMono(String::class.java) | ||
.block() | ||
|
||
logger.info { "Token response: $response" } | ||
|
||
// json 응답을 객체로 변환 | ||
val objectMapper = ObjectMapper() | ||
val kakaoToken: KakaoTokenResponseDto = objectMapper.readValue(response, KakaoTokenResponseDto::class.java) | ||
|
||
logger.info { "Access Token : ${kakaoToken.accessToken}" } | ||
return kakaoToken | ||
} | ||
|
||
// 사용자 정보 가져오기 | ||
fun getUserInfo(accessToken: String): KakaoUserInfoResponseDto { | ||
// 웹 클라이언트로 요청 보내기 | ||
val response = WebClient.create(userUri) | ||
.get() | ||
.header("Authorization", "Bearer $accessToken") | ||
.header("Content-Type", "application/x-www-form-urlencoded;charset=utf-8") | ||
.retrieve() | ||
.bodyToMono(String::class.java) | ||
.block() | ||
|
||
logger.info { "User info response: $response" } | ||
|
||
// json 응답을 객체로 변환 | ||
val objectMapper = ObjectMapper() | ||
val userInfo = objectMapper.readValue(response, KakaoUserInfoResponseDto::class.java) | ||
|
||
logger.info { "email : ${userInfo.kakaoAccount!!.email}" } | ||
return userInfo | ||
} | ||
} |
Oops, something went wrong.