-
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
* refactor: toAuthorities 메서드 분리 * refactor: 상속을 위한 TokenUserDetails open 클래스로 변경 * feat: UserArgument 구현 * refactor: UserArgument 적용 * refactor: AuthorityUtils 적용 * refactor: UserArgumentHandlerMethodArgumentResolver 반환 구체화 * feat: UserArgumentHandlerMethodArgumentResolver 설정에 등록
- Loading branch information
1 parent
2188971
commit 6aa3191
Showing
10 changed files
with
124 additions
and
55 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
api/src/main/kotlin/com/few/api/security/authentication/authority/AuthorityUtils.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 com.few.api.security.authentication.authority | ||
|
||
import org.apache.commons.lang3.StringUtils | ||
import org.springframework.security.core.GrantedAuthority | ||
|
||
object AuthorityUtils { | ||
|
||
@Throws(IllegalArgumentException::class) | ||
fun toAuthorities(roles: String): List<GrantedAuthority> { | ||
val tokens = StringUtils.splitPreserveAllTokens(roles, "[,]") | ||
val rtn: MutableList<GrantedAuthority> = ArrayList() | ||
for (token in tokens) { | ||
if (token != "") { | ||
val role = token.trim { it <= ' ' } | ||
rtn.add(Roles.valueOf(role).authority) | ||
} | ||
} | ||
return rtn | ||
} | ||
} |
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
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
5 changes: 5 additions & 0 deletions
5
api/src/main/kotlin/com/few/api/web/support/method/UserArgument.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,5 @@ | ||
package com.few.api.web.support.method | ||
|
||
@Target(AnnotationTarget.VALUE_PARAMETER) | ||
@Retention(AnnotationRetention.RUNTIME) | ||
annotation class UserArgument |
15 changes: 15 additions & 0 deletions
15
api/src/main/kotlin/com/few/api/web/support/method/UserArgumentDetails.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,15 @@ | ||
package com.few.api.web.support.method | ||
|
||
import com.few.api.security.authentication.token.TokenUserDetails | ||
import org.springframework.security.core.GrantedAuthority | ||
|
||
class UserArgumentDetails( | ||
val isAuth: Boolean, | ||
authorities: List<GrantedAuthority>, | ||
id: String, | ||
email: String, | ||
) : TokenUserDetails( | ||
authorities = authorities, | ||
id = id, | ||
email = email | ||
) |
59 changes: 59 additions & 0 deletions
59
...c/main/kotlin/com/few/api/web/support/method/UserArgumentHandlerMethodArgumentResolver.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,59 @@ | ||
package com.few.api.web.support.method | ||
|
||
import com.few.api.security.authentication.authority.AuthorityUtils | ||
import com.few.api.security.filter.token.AccessTokenResolver | ||
import com.few.api.security.token.TokenResolver | ||
import io.github.oshai.kotlinlogging.KotlinLogging | ||
import org.springframework.core.MethodParameter | ||
import org.springframework.stereotype.Component | ||
import org.springframework.web.bind.support.WebDataBinderFactory | ||
import org.springframework.web.context.request.NativeWebRequest | ||
import org.springframework.web.method.support.HandlerMethodArgumentResolver | ||
import org.springframework.web.method.support.ModelAndViewContainer | ||
|
||
@Component | ||
class UserArgumentHandlerMethodArgumentResolver( | ||
private val tokenResolver: TokenResolver, | ||
) : HandlerMethodArgumentResolver { | ||
val log = KotlinLogging.logger {} | ||
|
||
override fun supportsParameter(parameter: MethodParameter): Boolean { | ||
return parameter.hasParameterAnnotation(UserArgument::class.java) | ||
} | ||
|
||
override fun resolveArgument( | ||
parameter: MethodParameter, | ||
mavContainer: ModelAndViewContainer?, | ||
webRequest: NativeWebRequest, | ||
binderFactory: WebDataBinderFactory?, | ||
): UserArgumentDetails { | ||
val authorization: String? = webRequest.getHeader("Authorization") | ||
|
||
val memberId = authorization?.let { | ||
AccessTokenResolver.resolve(it) | ||
}.let { | ||
tokenResolver.resolveId(it) | ||
} ?: 0L | ||
|
||
val email = authorization?.let { | ||
AccessTokenResolver.resolve(it) | ||
}.let { | ||
tokenResolver.resolveEmail(it) | ||
} ?: "" | ||
|
||
val authorities = authorization?.let { | ||
AccessTokenResolver.resolve(it) | ||
}?.let { | ||
tokenResolver.resolveRole(it) | ||
}?.let { | ||
AuthorityUtils.toAuthorities(it) | ||
} ?: emptyList() | ||
|
||
return UserArgumentDetails( | ||
isAuth = authorization != null, | ||
id = memberId.toString(), | ||
email = email, | ||
authorities = authorities | ||
) | ||
} | ||
} |