-
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 #184 from MaeumGaGym/BAC-520-Exception-관련--필터를-Sec…
…urityFilterChain에서-분리 PR :: Exception 관련 필터를 SecurityFilterChain에서 분리
- Loading branch information
Showing
14 changed files
with
328 additions
and
122 deletions.
There are no files selected for viewing
89 changes: 0 additions & 89 deletions
89
...gym-infrastructure/src/main/kotlin/com/info/maeumgagym/config/filter/FilterChainConfig.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
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
34 changes: 34 additions & 0 deletions
34
...c/main/kotlin/com/info/maeumgagym/error/filter/filterchain/ExceptionChainedFilterChain.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,34 @@ | ||
package com.info.maeumgagym.error.filter.filterchain | ||
|
||
import com.info.maeumgagym.filter.chained.ChainedFilterChain | ||
import javax.servlet.Filter | ||
import javax.servlet.FilterChain | ||
|
||
class ExceptionChainedFilterChain( | ||
private val filters: Map<String, Filter> | ||
) : ChainedFilterChain() { | ||
|
||
private var calledFilterChain: ThreadLocal<FilterChain>? = null | ||
|
||
private var currentFilterIndex: ThreadLocal<Int> = ThreadLocal.withInitial { -1 } | ||
|
||
override fun getFilters(): Map<String, Filter> = filters | ||
|
||
override fun getCalledFilterChain(): FilterChain? = calledFilterChain?.get() | ||
|
||
override fun getCurrentFilterIndex(): Int = this.currentFilterIndex.get() | ||
|
||
override fun plusCurrentFilterIndex(): Int { | ||
this.currentFilterIndex.set(this.getCurrentFilterIndex() + 1) | ||
return this.currentFilterIndex.get() | ||
} | ||
|
||
override fun resetFilterChain() { | ||
this.calledFilterChain = null | ||
this.currentFilterIndex = ThreadLocal.withInitial { -1 } | ||
} | ||
|
||
override fun setCalledFilterChain(filterChain: FilterChain) { | ||
this.calledFilterChain = ThreadLocal.withInitial { filterChain } | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...n/kotlin/com/info/maeumgagym/error/filter/filterchain/ExceptionChainedFilterChainProxy.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,10 @@ | ||
package com.info.maeumgagym.error.filter.filterchain | ||
|
||
import com.info.maeumgagym.filter.chained.ChainedFilterChainProxy | ||
|
||
class ExceptionChainedFilterChainProxy( | ||
exceptionChainedFilterChain: ExceptionChainedFilterChain | ||
) : ChainedFilterChainProxy<ExceptionChainedFilterChain>() { | ||
|
||
override val filterChain: ExceptionChainedFilterChain = exceptionChainedFilterChain | ||
} |
104 changes: 104 additions & 0 deletions
104
...m-infrastructure/src/main/kotlin/com/info/maeumgagym/filter/chained/ChainedFilterChain.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,104 @@ | ||
package com.info.maeumgagym.filter.chained | ||
|
||
import com.info.maeumgagym.common.exception.CriticalException | ||
import com.info.maeumgagym.filter.global.GlobalFilterChain | ||
import javax.servlet.Filter | ||
import javax.servlet.FilterChain | ||
import javax.servlet.ServletRequest | ||
import javax.servlet.ServletResponse | ||
|
||
/** | ||
* 필터 안에서 다음 필터를 호출해야하는 FilerChain을 위한 클래스 | ||
* | ||
* [org.apache.catalina.core.ApplicationFilterChain] 안에서 다른 FilterChain이 호출 되는 경우, 해당 FilterChain의 Filter들은 다음 필터(FilterChain 밖)를 자신의 안에서 호출하는 것이 아닌, FilterChain 바깥의 임의의 객체가 호출하게 됨 | ||
* | ||
* 예를 들어, FilterA -> FilterChain -> FilterB 의 구조를 가지고 있다고 할 때, 논리적으로 아래의 코드를 실행한 것과 같이 작동 | ||
* ``` | ||
* FilterA.doFilter() { | ||
* FilterChain.doFilter() { | ||
* FilterChainInnerFilterA.doFilter() | ||
* FilterChainInnerFilterB.doFilter() | ||
* } | ||
* FilterB.doFilter() | ||
* } | ||
* ``` | ||
* 하지만, try ~ catch문으로 감싸 다음 Filter를 실행해야하는 경우와 같이, 위와 같은 형태로 실행되면 안되는 경우가 존재 | ||
* | ||
* 이를 해결하기 위해, FilterChain에게 현재 자신을 호출한 FilterChain을 넘겨, FilterChain의 마지막에서 자신을 호출한 FilterChain을 호출하게 해 연계되어 작동되도록 구현함 | ||
* | ||
* 따라서, 앞서 언급한 구조에서 논리적으로 아래의 코드를 실행한 것과 같이 작동 | ||
* ``` | ||
* FilterA.doFilter() { | ||
* FilterChain.doFilter() { | ||
* FilterChainInnerFilterA.doFilter() | ||
* FilterChainInnerFilterB.doFilter() { | ||
* FilterB.doFilter() | ||
* } | ||
* } | ||
* } | ||
* ``` | ||
* | ||
* @see GlobalFilterChain | ||
* @see ChainedFilterChainProxy | ||
* | ||
* @author Daybreak312 | ||
* @since 21-03-2024 | ||
*/ | ||
abstract class ChainedFilterChain : GlobalFilterChain { | ||
|
||
/** | ||
* @return 이 FilterChain을 호출한 FilterChain을 반환 | ||
*/ | ||
protected abstract fun getCalledFilterChain(): FilterChain? | ||
|
||
/** | ||
* FilterChain을 시작하기 전에, 이 FilterChain을 호출한 FilterChain 정보를 설정하는 메소드 | ||
*/ | ||
protected abstract fun setCalledFilterChain(filterChain: FilterChain) | ||
|
||
/** | ||
* FilterChain이 끝난 후, 다시 초기 상태로 되돌리는 메소드 | ||
*/ | ||
protected abstract fun resetFilterChain() | ||
|
||
/** | ||
* FilterChain의 다음 Filter로 넘어가기 위해, 실행한 Filter의 Index를 담은 변수에 1 추가 | ||
*/ | ||
protected abstract fun plusCurrentFilterIndex(): Int | ||
|
||
/** | ||
* @return [getCurrentFilterIndex]를 기반으로 현재 실행한 Filter를 반환 | ||
*/ | ||
protected fun getCurrentFilter(): Filter = | ||
this.getFilters()[this.getFilters().keys.toList()[this.getCurrentFilterIndex()]]!! | ||
|
||
/** | ||
* FilterChain 내부의 Filter들이 호출하는 메소드 | ||
*/ | ||
override fun doFilter(request: ServletRequest, response: ServletResponse) { | ||
if (this.getCalledFilterChain() == null) { | ||
throw CriticalException(500, "ChainedFilterChain called with doesn't init CalledFilterChain") | ||
} | ||
|
||
val filters = this.getFilters() | ||
|
||
if (this.getCurrentFilterIndex() == filters.size - 2) { | ||
this.plusCurrentFilterIndex() | ||
this.getCurrentFilter().doFilter(request, response, this.getCalledFilterChain()) | ||
} else { | ||
this.plusCurrentFilterIndex() | ||
this.getCurrentFilter().doFilter(request, response, this) | ||
} | ||
} | ||
|
||
/** | ||
* 외부에서 이 FilterChain을 동작시킬 때 사용하는 메소드 | ||
* | ||
* @param calledFilterChain 이 FilterChain을 호출한 FilterChain | ||
*/ | ||
fun doFilterChained(request: ServletRequest, response: ServletResponse, calledFilterChain: FilterChain) { | ||
this.setCalledFilterChain(calledFilterChain) | ||
this.doFilter(request, response) | ||
this.resetFilterChain() | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...rastructure/src/main/kotlin/com/info/maeumgagym/filter/chained/ChainedFilterChainProxy.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,8 @@ | ||
package com.info.maeumgagym.filter.chained | ||
|
||
import com.info.maeumgagym.filter.global.GlobalFilterChainProxy | ||
|
||
abstract class ChainedFilterChainProxy<out T : ChainedFilterChain> : GlobalFilterChainProxy<T>() { | ||
|
||
abstract override val filterChain: T | ||
} |
Oops, something went wrong.