-
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 #182 from MaeumGaGym/BAC-516-Authentication에서-Auth…
…Details-제거 PR :: Authentication에서 UserDetails 제거
- Loading branch information
Showing
7 changed files
with
100 additions
and
133 deletions.
There are no files selected for viewing
36 changes: 17 additions & 19 deletions
36
maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/auth/ReadCurrentUserAdapter.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,39 +1,37 @@ | ||
package com.info.maeumgagym.auth | ||
|
||
import com.info.maeumgagym.auth.port.out.ReadCurrentUserPort | ||
import com.info.maeumgagym.common.exception.AuthenticationException | ||
import com.info.maeumgagym.security.principle.CustomUserDetails | ||
import com.info.maeumgagym.security.jwt.AuthenticationProvider | ||
import com.info.maeumgagym.security.jwt.JwtFilter | ||
import com.info.maeumgagym.user.model.User | ||
import com.info.maeumgagym.user.port.out.ReadUserPort | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken | ||
import org.springframework.security.core.context.SecurityContextHolder | ||
import org.springframework.stereotype.Component | ||
|
||
@Component | ||
internal class ReadCurrentUserAdapter( | ||
private val readUserPort: ReadUserPort | ||
private val readUserPort: ReadUserPort, | ||
private val authenticationProvider: AuthenticationProvider | ||
) : ReadCurrentUserPort { | ||
|
||
override fun readCurrentUser(): User { | ||
// User를 찾기 위한 정보가 담겨 있는 Authentication 로드 | ||
val authentication = SecurityContextHolder.getContext().authentication | ||
|
||
// jwt filter에서 생성한 authDetail를 context holder에서 불러옴 | ||
val authDetails = authentication.principal as CustomUserDetails | ||
|
||
// Lazy Loading으로 Nullable인 User를 확인하고, null인 경우 User를 Load 및 입력 | ||
if (authDetails.getUser() == null) { | ||
authDetails.fillUser( | ||
readUserPort.readByOAuthId(authDetails.username) | ||
// authDetails에 담긴 username = oauthId는 로직상 무조건 유저가 존재해야하므로 AuthenticationException throw | ||
?: throw AuthenticationException(401, "User Not Found In ReadCurrentUserPort") | ||
) | ||
JwtFilter.run { | ||
// Lazy Loading으로 Nullable인 User를 확인 | ||
if (this.authenticatedUser?.get() == null || | ||
this.authenticatedUser?.get()?.oauthId != authentication!!.principal | ||
) { | ||
// null인 경우 User를 Load 및 SecurityContext, authenticatedUser에 입력 | ||
SecurityContextHolder.getContext().authentication = | ||
authenticationProvider.getAuthentication( | ||
authentication.principal as String | ||
) | ||
} | ||
} | ||
|
||
// Loading된 User를 Authentication에도 반영 | ||
SecurityContextHolder.getContext().authentication = | ||
UsernamePasswordAuthenticationToken(authDetails, null, authDetails.authorities) | ||
|
||
// User 반환 | ||
return authDetails.getUser()!! | ||
return JwtFilter.authenticatedUser!!.get() | ||
} | ||
} |
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
14 changes: 0 additions & 14 deletions
14
...ructure/src/main/kotlin/com/info/maeumgagym/security/principle/CustomUserDetailService.kt
This file was deleted.
Oops, something went wrong.
37 changes: 0 additions & 37 deletions
37
...nfrastructure/src/main/kotlin/com/info/maeumgagym/security/principle/CustomUserDetails.kt
This file was deleted.
Oops, something went wrong.
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