Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

๐Ÿ”€ :: ์ฟ ํ‚ค filter ์— ์ธ๊ฐ€ ๊ธฐ๋Šฅ ์ œ๊ฑฐ #206

Merged
merged 3 commits into from
Aug 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@ import team.msg.sms.global.security.token.JwtParser
class FilterConfig(
private val jwtParser: JwtParser,
private val objectMapper: ObjectMapper,
private val existStudentUseCase: ExistStudentUseCase,
private val queryUserByUserIdUseCase: QueryUserByUserIdUseCase
) : SecurityConfigurerAdapter<DefaultSecurityFilterChain, HttpSecurity>() {
override fun configure(builder: HttpSecurity) {
builder.addFilterBefore(ExceptionFilter(objectMapper), UsernamePasswordAuthenticationFilter::class.java)
builder.addFilterBefore(JwtFilter(jwtParser, queryUserByUserIdUseCase, existStudentUseCase), UsernamePasswordAuthenticationFilter::class.java)
builder.addFilterBefore(JwtFilter(jwtParser), UsernamePasswordAuthenticationFilter::class.java)
builder.addFilterBefore(RequestLogFilter(), UsernamePasswordAuthenticationFilter::class.java)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,56 +3,28 @@ package team.msg.sms.global.filter
import org.springframework.security.core.Authentication
import org.springframework.security.core.context.SecurityContextHolder
import org.springframework.web.filter.OncePerRequestFilter
import team.msg.sms.domain.auth.model.Role
import team.msg.sms.domain.student.usecase.ExistStudentUseCase
import team.msg.sms.domain.user.exception.UserNotFoundException
import team.msg.sms.domain.user.usecase.QueryUserByUserIdUseCase
import team.msg.sms.global.exception.InvalidUrlAccessException
import team.msg.sms.global.security.token.JwtParser
import team.msg.sms.global.security.token.JwtProperties
import java.util.*
import javax.servlet.FilterChain
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

class JwtFilter(
private val jwtParser: JwtParser,
private val queryUserByUserIdUseCase: QueryUserByUserIdUseCase,
private val existStudentUseCase: ExistStudentUseCase
) : OncePerRequestFilter() {
override fun doFilterInternal(
request: HttpServletRequest,
response: HttpServletResponse,
filterChain: FilterChain
) {
val cookieToken = resolvedCookieToken(request)
if(cookieToken == null) {
val token = resolveToken(request)
val token = resolvedCookieToken(request) ?: resolveToken(request)

token
?.let { SecurityContextHolder.getContext().authentication = jwtParser.getAuthentication(token) }

} else {
cookieToken
.let {
val authentication: Authentication = jwtParser.getAuthentication(cookieToken)
SecurityContextHolder.getContext().authentication = authentication

val isAllowStudent = authentication.authorities.any { it.authority == Role.ROLE_STUDENT.name }
val isPostStudentEndpoint = request.requestURI == "/student" && request.method == "POST"

if (isAllowStudent && !isPostStudentEndpoint) {
val userId = UUID.fromString(jwtParser.getClaimsBody(cookieToken))
val user = queryUserByUserIdUseCase.execute(userId) ?: throw UserNotFoundException

if (!existStudentUseCase.execute(user)) {
throw InvalidUrlAccessException
}
}
}
token?.let {
val authentication: Authentication = jwtParser.getAuthentication(it)
SecurityContextHolder.getContext().authentication = authentication
}
filterChain.doFilter(request, response)

filterChain.doFilter(request, response)
}

private fun resolveToken(request: HttpServletRequest): String? =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,13 @@ import org.springframework.security.config.http.SessionCreationPolicy
import org.springframework.security.web.SecurityFilterChain
import org.springframework.security.web.util.matcher.RequestMatcher
import org.springframework.web.cors.CorsUtils
import team.msg.sms.domain.student.usecase.ExistStudentUseCase
import team.msg.sms.domain.user.usecase.QueryUserByUserIdUseCase
import team.msg.sms.global.filter.FilterConfig
import team.msg.sms.global.security.token.JwtParser

@Configuration
class SecurityConfig(
private val jwtParser: JwtParser,
private val objectMapper: ObjectMapper,
private val existStudentUseCase: ExistStudentUseCase,
private val queryUserByUserIdUseCase: QueryUserByUserIdUseCase,
private val customAuthenticationEntryPoint: CustomAuthenticationEntryPoint
) {

Expand Down Expand Up @@ -68,7 +64,7 @@ class SecurityConfig(
.anyRequest().authenticated()

http
.apply(FilterConfig(jwtParser, objectMapper, existStudentUseCase, queryUserByUserIdUseCase))
.apply(FilterConfig(jwtParser, objectMapper))

http
.exceptionHandling()
Expand Down