Skip to content

Commit

Permalink
update :: jwt 사용으로 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
4mjeo committed Mar 9, 2024
1 parent e9e1afb commit 1749bf9
Showing 1 changed file with 8 additions and 65 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.example.v1oauthauthorizationservice.infrastructure.configuration

import com.example.v1oauthauthorizationservice.global.config.jwt.JwtTokenResolver
import com.example.v1oauthauthorizationservice.global.config.jwt.TokenProvider
import com.example.v1oauthauthorizationservice.infrastructure.configuration.oauth2.authorization.exceptions.AccessTokenNotFoundException
import com.example.v1oauthauthorizationservice.infrastructure.configuration.oauth2.authorization.exceptions.AccessTokenNotFoundException.Companion.ACCESS_TOKEN_NOT_FOUND
import com.example.v1oauthauthorizationservice.infrastructure.configuration.oauth2.authorization.exceptions.AuthorizationNotFoundException
Expand All @@ -26,78 +28,19 @@ import org.springframework.web.filter.OncePerRequestFilter
@Component
@Order(-100)
class AuthenticationFilter(
private val accessTokenEntityRepository: AccessTokenEntityRepository,
private val authenticationEntityRepository: AuthorizationEntityRepository,
private val authorizationAttributeEntityRepository: AuthorizationAttributeEntityRepository
private val tokenResolver: JwtTokenResolver,
private val tokenProvider: TokenProvider
) : OncePerRequestFilter() {

override fun doFilterInternal(
request: HttpServletRequest,
response: HttpServletResponse,
filterChain: FilterChain
) {
request.getParameter("access_token")?.let { accessToken ->
setAuthenticationByAccessToken(accessToken)
filterChain.doFilter(request, response)
return
}
setAuthenticationByHeader(request)
tokenResolver.resolveToken(request)
?.let {
SecurityContextHolder.getContext().authentication = tokenProvider.getAuthentication(it)
}
filterChain.doFilter(request, response)
}

private fun setAuthenticationByHeader(request: HttpServletRequest) {

val requestUserId = request.getHeader("Request-User-Id")
val requestUserAuthority = request.getHeader("Request-User-Authorities")
val requestUserRole = request.getHeader("Request-User-Role")

val simpleGrantedAuthorities = requestUserAuthority?.toList()?.let { authorities ->
buildRequestAuthoritiesAndRole(requestUserRole, authorities)
.map { SimpleGrantedAuthority(it) }
}

if (!simpleGrantedAuthorities.isNullOrEmpty()) {
val user = User(
requestUserId,
"",
simpleGrantedAuthorities
)
val authentication = UsernamePasswordAuthenticationToken(user, "", user.authorities)

SecurityContextHolder.getContext().authentication = authentication
}
}


private fun setAuthenticationByAccessToken(accessToken: String) {
val tokenEntity = accessTokenEntityRepository.findByTokenValue(accessToken)
?: throw AccessTokenNotFoundException(ACCESS_TOKEN_NOT_FOUND)

val authorization = authenticationEntityRepository.findByIdOrNull(tokenEntity.authorization.id)
?: throw AuthorizationNotFoundException(AUTHORIZATION_NOT_FOUND)

val attributes = authorizationAttributeEntityRepository.findByAuthorizationId(authorization.id!!).associate { it.attributeKey to it.attributeValue }
val authorities = listOf<GrantedAuthority>()

SecurityContextHolder.getContext().authentication = BearerTokenAuthentication(
DefaultOAuth2AuthenticatedPrincipal(attributes, authorities),
tokenEntity.toOAuth2AccessToken(),
authorities
)
}

private fun buildRequestAuthoritiesAndRole(
requestUserRole: String,
requestUserAuthorities: List<String>
): List<String> {
val authoritiesAndRoles = mutableListOf("ROLE_$requestUserRole")
requestUserAuthorities.forEach { authoritiesAndRoles.add(it) }
return requestUserAuthorities
}

private fun String.toList(): List<String> {
return this.removeSurrounding("[", "]")
.replace(" ", "")
.split(",")
}
}

0 comments on commit 1749bf9

Please sign in to comment.