-
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.
Merge pull request #3 from mash-up-kr/feature/login
feat: 카카오 로그인 구현
- Loading branch information
Showing
33 changed files
with
769 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
jjwtVersion=0.11.5 | ||
mysqlConnectorVersion=8.0.33 |
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
46 changes: 46 additions & 0 deletions
46
pic-api/src/main/kotlin/com/mashup/pic/auth/applicationService/AuthApplicationService.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,46 @@ | ||
package com.mashup.pic.auth.applicationService | ||
|
||
import com.mashup.pic.auth.applicationService.dto.LoginServiceRequest | ||
import com.mashup.pic.auth.controller.dto.LoginResponse | ||
import com.mashup.pic.domain.user.User | ||
import com.mashup.pic.security.jwt.JwtManager | ||
import com.mashup.pic.domain.user.UserService | ||
import com.mashup.pic.security.authentication.UserInfo | ||
import com.mashup.pic.security.oidc.KakaoIdTokenValidator | ||
import org.springframework.stereotype.Service | ||
import org.springframework.transaction.annotation.Transactional | ||
|
||
@Service | ||
@Transactional(readOnly = true) | ||
class AuthApplicationService( | ||
private val userService: UserService, | ||
private val jwtTokenUtil: JwtManager, | ||
private val idTokenValidator: KakaoIdTokenValidator | ||
) { | ||
|
||
@Transactional | ||
fun login(request: LoginServiceRequest): LoginResponse { | ||
val oAuthId = idTokenValidator.validateAndGetId(request.idToken, request.nickname) | ||
val user = userService.findUserByOAuthIdOrNull(oAuthId)?: createUser(oAuthId, request) | ||
|
||
val authToken = jwtTokenUtil.generateAuthToken(user.toUserInfo()) | ||
return LoginResponse.from(user, authToken) | ||
} | ||
|
||
private fun createUser(oAuthId: Long, request: LoginServiceRequest) : User { | ||
return userService.create( | ||
oAuthId = oAuthId, | ||
nickname = request.nickname, | ||
profileImage = request.profileImage | ||
) | ||
} | ||
|
||
fun User.toUserInfo(): UserInfo { | ||
return UserInfo( | ||
id = this.id, | ||
nickname = this.nickname, | ||
roles = this.roles | ||
) | ||
} | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
pic-api/src/main/kotlin/com/mashup/pic/auth/applicationService/dto/LoginServiceRequest.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,12 @@ | ||
package com.mashup.pic.auth.applicationService.dto | ||
|
||
data class LoginServiceRequest( | ||
val idToken: String, | ||
val provider: LoginProvider, | ||
val nickname: String, | ||
val profileImage: String | ||
) | ||
|
||
enum class LoginProvider { | ||
KAKAO, NAVER, GOOGLE | ||
} |
26 changes: 26 additions & 0 deletions
26
pic-api/src/main/kotlin/com/mashup/pic/auth/controller/AuthController.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,26 @@ | ||
package com.mashup.pic.auth.controller | ||
|
||
import org.springframework.web.bind.annotation.PostMapping | ||
import org.springframework.web.bind.annotation.RequestBody | ||
import org.springframework.web.bind.annotation.RequestMapping | ||
import org.springframework.web.bind.annotation.RestController | ||
import com.mashup.pic.auth.applicationService.AuthApplicationService | ||
import com.mashup.pic.auth.controller.dto.LoginRequest | ||
import com.mashup.pic.auth.controller.dto.LoginResponse | ||
import com.mashup.pic.common.ApiResponse | ||
import jakarta.validation.Valid | ||
|
||
@RestController | ||
@RequestMapping("/api/v1/auth") | ||
class AuthController( | ||
private val authApplicationService: AuthApplicationService, | ||
) { | ||
|
||
@PostMapping("/login") | ||
fun login( | ||
@Valid @RequestBody loginRequest: LoginRequest | ||
): ApiResponse<LoginResponse> { | ||
return ApiResponse.success(authApplicationService.login(loginRequest.toServiceRequest())) | ||
} | ||
|
||
} |
22 changes: 22 additions & 0 deletions
22
pic-api/src/main/kotlin/com/mashup/pic/auth/controller/dto/LoginRequest.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,22 @@ | ||
package com.mashup.pic.auth.controller.dto | ||
|
||
import com.mashup.pic.auth.applicationService.dto.LoginProvider | ||
import com.mashup.pic.auth.applicationService.dto.LoginServiceRequest | ||
import jakarta.validation.constraints.NotBlank | ||
|
||
data class LoginRequest( | ||
@NotBlank val idToken: String, | ||
@NotBlank val provider: LoginProvider, | ||
@NotBlank val nickname: String, | ||
@NotBlank val profileImage: String | ||
) { | ||
|
||
fun toServiceRequest(): LoginServiceRequest { | ||
return LoginServiceRequest( | ||
idToken = idToken, | ||
provider = provider, | ||
nickname = nickname, | ||
profileImage = profileImage | ||
) | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
pic-api/src/main/kotlin/com/mashup/pic/auth/controller/dto/LoginResponse.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,23 @@ | ||
package com.mashup.pic.auth.controller.dto | ||
|
||
import com.mashup.pic.domain.user.User | ||
import com.mashup.pic.security.authentication.AuthToken | ||
|
||
|
||
data class LoginResponse( | ||
val userId: Long, | ||
val nickname: String, | ||
val accessToken: String, | ||
val refreshToken: String | ||
) { | ||
companion object { | ||
fun from(user: User, authToken: AuthToken): LoginResponse { | ||
return LoginResponse( | ||
userId = user.id, | ||
nickname = user.nickname, | ||
accessToken = authToken.accessToken, | ||
refreshToken = authToken.refreshToken | ||
) | ||
} | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
pic-api/src/main/kotlin/com/mashup/pic/config/SecurityConfig.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,53 @@ | ||
package com.mashup.pic.config | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import com.mashup.pic.security.handler.HttpStatusAccessDeniedHandler | ||
import com.mashup.pic.security.handler.HttpStatusAuthenticationEntryPoint | ||
import com.mashup.pic.security.jwt.JwtFilter | ||
import com.mashup.pic.security.jwt.JwtManager | ||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.Configuration | ||
import org.springframework.security.config.annotation.web.builders.HttpSecurity | ||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity | ||
import org.springframework.security.web.SecurityFilterChain | ||
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter | ||
|
||
@Configuration | ||
@EnableWebSecurity | ||
class SecurityConfig( | ||
private val jwtTokenUtil: JwtManager, | ||
private val objectMapper: ObjectMapper, | ||
) { | ||
|
||
@Bean | ||
fun securityFilterChain(http: HttpSecurity): SecurityFilterChain { | ||
return http | ||
.cors { it.disable() } | ||
.csrf { it.disable() } | ||
.httpBasic { it.disable() } | ||
.formLogin { it.disable() } | ||
.authorizeHttpRequests { authorization -> | ||
authorization | ||
.requestMatchers(*WHITELIST_ENDPOINTS).permitAll() | ||
.requestMatchers(ADMIN_ENDPOINT_PATTERN).hasRole(ADMIN_ROLE) | ||
.anyRequest().hasRole(MEMBER_ROLE) | ||
} | ||
.addFilterBefore(JwtFilter(jwtTokenUtil, objectMapper), UsernamePasswordAuthenticationFilter::class.java) | ||
.exceptionHandling { | ||
it.authenticationEntryPoint(HttpStatusAuthenticationEntryPoint()) | ||
it.accessDeniedHandler(HttpStatusAccessDeniedHandler()) | ||
} | ||
.build() | ||
} | ||
|
||
companion object { | ||
private const val ADMIN_ENDPOINT_PATTERN = "/api/v1/admin/**" | ||
private const val ADMIN_ROLE = "ADMIN" | ||
private const val MEMBER_ROLE = "MEMBER" | ||
private val WHITELIST_ENDPOINTS = arrayOf( | ||
"/api/v1/auth/login", | ||
"/api/v1/auth/token" | ||
) | ||
} | ||
|
||
} |
6 changes: 6 additions & 0 deletions
6
pic-api/src/main/kotlin/com/mashup/pic/security/authentication/AuthToken.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,6 @@ | ||
package com.mashup.pic.security.authentication | ||
|
||
data class AuthToken( | ||
val accessToken: String, | ||
val refreshToken: String | ||
) |
42 changes: 42 additions & 0 deletions
42
pic-api/src/main/kotlin/com/mashup/pic/security/authentication/JwtAuthentication.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,42 @@ | ||
package com.mashup.pic.security.authentication | ||
|
||
import com.mashup.pic.domain.user.UserRole | ||
import org.springframework.security.core.Authentication | ||
import org.springframework.security.core.GrantedAuthority | ||
|
||
class JwtAuthentication(private val userInfo: UserInfo) : Authentication { | ||
|
||
private var authenticated: Boolean = false | ||
|
||
override fun getName(): String { | ||
return userInfo.nickname | ||
} | ||
|
||
override fun getAuthorities(): Collection<GrantedAuthority> { | ||
return userInfo.roles.map(this::convertUserRoleToGrantedAuthority) | ||
} | ||
|
||
override fun getCredentials(): Any { | ||
return userInfo | ||
} | ||
|
||
override fun getDetails(): Any { | ||
return userInfo | ||
} | ||
|
||
override fun getPrincipal(): Any { | ||
return userInfo | ||
} | ||
|
||
override fun isAuthenticated(): Boolean { | ||
return authenticated | ||
} | ||
|
||
override fun setAuthenticated(isAuthenticated: Boolean) { | ||
this.authenticated = isAuthenticated | ||
} | ||
|
||
private fun convertUserRoleToGrantedAuthority(userRole: UserRole): GrantedAuthority { | ||
return GrantedAuthority { userRole.role } | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
pic-api/src/main/kotlin/com/mashup/pic/security/authentication/UserInfo.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,11 @@ | ||
package com.mashup.pic.security.authentication | ||
|
||
import com.mashup.pic.domain.user.User | ||
import com.mashup.pic.domain.user.UserRole | ||
|
||
|
||
data class UserInfo( | ||
val id: Long, | ||
val nickname: String, | ||
val roles: Set<UserRole> | ||
) |
23 changes: 23 additions & 0 deletions
23
pic-api/src/main/kotlin/com/mashup/pic/security/handler/HttpStatusAccessDeniedHandler.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,23 @@ | ||
package com.mashup.pic.security.handler | ||
|
||
import jakarta.servlet.http.HttpServletRequest | ||
import jakarta.servlet.http.HttpServletResponse | ||
import org.springframework.http.HttpStatus | ||
import org.springframework.security.web.access.AccessDeniedHandler | ||
|
||
import org.slf4j.Logger | ||
import org.slf4j.LoggerFactory | ||
import org.springframework.security.access.AccessDeniedException | ||
|
||
class HttpStatusAccessDeniedHandler : AccessDeniedHandler { | ||
private val logger: Logger = LoggerFactory.getLogger(HttpStatusAccessDeniedHandler::class.java) | ||
|
||
override fun handle( | ||
request: HttpServletRequest, | ||
response: HttpServletResponse, | ||
accessDeniedException: AccessDeniedException | ||
) { | ||
logger.warn("Access denied: {}", accessDeniedException.message) | ||
response.status = HttpStatus.FORBIDDEN.value() | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...api/src/main/kotlin/com/mashup/pic/security/handler/HttpStatusAuthenticationEntryPoint.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,17 @@ | ||
package com.mashup.pic.security.handler | ||
|
||
import jakarta.servlet.http.HttpServletRequest | ||
import jakarta.servlet.http.HttpServletResponse | ||
import org.springframework.http.HttpStatus | ||
import org.springframework.security.core.AuthenticationException | ||
import org.springframework.security.web.AuthenticationEntryPoint | ||
|
||
class HttpStatusAuthenticationEntryPoint : AuthenticationEntryPoint { | ||
override fun commence( | ||
request: HttpServletRequest, | ||
response: HttpServletResponse, | ||
authException: AuthenticationException | ||
) { | ||
response.status = HttpStatus.UNAUTHORIZED.value() | ||
} | ||
} |
Oops, something went wrong.