From 3207e45cb43aa509775c003019cd4d578b0a9388 Mon Sep 17 00:00:00 2001 From: Daybreak312 Date: Thu, 21 Mar 2024 21:24:15 +0900 Subject: [PATCH 01/10] =?UTF-8?q?FEAT=20::=20GlobalFilterChain=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../filter/global/GlobalFilterChain.kt | 34 +++++++++++++++++++ .../filter/global/GlobalFilterChainProxy.kt | 23 +++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/filter/global/GlobalFilterChain.kt create mode 100644 maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/filter/global/GlobalFilterChainProxy.kt diff --git a/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/filter/global/GlobalFilterChain.kt b/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/filter/global/GlobalFilterChain.kt new file mode 100644 index 000000000..e19c2501b --- /dev/null +++ b/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/filter/global/GlobalFilterChain.kt @@ -0,0 +1,34 @@ +package com.info.maeumgagym.filter.global + +import javax.servlet.Filter +import javax.servlet.FilterChain +import javax.servlet.ServletRequest +import javax.servlet.ServletResponse + +/** + * 특정한 역할이 없는, 해당 애플리케이션에서 사용되는 기본 FilterChain + * + * @see GlobalFilterChainProxy + * + * @author Daybreak312 + * @since 21-03-2024 + */ +interface GlobalFilterChain : FilterChain { + + /** + * FilterChain에 포함된 Filter들을 반환 + * + * @return Filter의 BeanName 혹은 ClassName과 Filter 객체의 Map 컬렉션 + */ + fun getFilters(): Map + + /** + * @return 현재 실행중인 Filter의 Index 반환 + */ + fun getCurrentFilterIndex(): Int + + /** + * FilterChain 내부의 Filter들을 순회 작동 + */ + override fun doFilter(request: ServletRequest, response: ServletResponse) +} diff --git a/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/filter/global/GlobalFilterChainProxy.kt b/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/filter/global/GlobalFilterChainProxy.kt new file mode 100644 index 000000000..a8a83675d --- /dev/null +++ b/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/filter/global/GlobalFilterChainProxy.kt @@ -0,0 +1,23 @@ +package com.info.maeumgagym.filter.global + +import org.springframework.web.filter.GenericFilterBean +import javax.servlet.FilterChain +import javax.servlet.ServletRequest +import javax.servlet.ServletResponse + +/** + * [GlobalFilterChain]을 [org.apache.catalina.core.ApplicationFilterChain]에 삽입하기 위한 프록시 + * + * @see GlobalFilterChain + * + * @author Daybreak312 + * @since 21-03-2024 + */ +abstract class GlobalFilterChainProxy : GenericFilterBean() { + + protected abstract val filterChain: GlobalFilterChain + + override fun doFilter(request: ServletRequest, response: ServletResponse, chain: FilterChain) { + filterChain.doFilter(request, response) + } +} From d93e973ae37ad900aa6f9304417c134b5a5ac29e Mon Sep 17 00:00:00 2001 From: Daybreak312 Date: Thu, 21 Mar 2024 21:24:33 +0900 Subject: [PATCH 02/10] =?UTF-8?q?FEAT=20::=20ChainedFilterChain=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../filter/chained/ChainedFilterChain.kt | 97 +++++++++++++++++++ .../filter/chained/ChainedFilterChainProxy.kt | 16 +++ 2 files changed, 113 insertions(+) create mode 100644 maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/filter/chained/ChainedFilterChain.kt create mode 100644 maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/filter/chained/ChainedFilterChainProxy.kt diff --git a/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/filter/chained/ChainedFilterChain.kt b/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/filter/chained/ChainedFilterChain.kt new file mode 100644 index 000000000..37a96f398 --- /dev/null +++ b/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/filter/chained/ChainedFilterChain.kt @@ -0,0 +1,97 @@ +package com.info.maeumgagym.filter.chained + +import com.info.maeumgagym.common.exception.CriticalException +import com.info.maeumgagym.filter.global.GlobalFilterChain +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 removeCalledFilterChain() + + /** + * FilterChain의 다음 Filter로 넘어가기 위해, 실행한 Filter의 Index를 담은 변수에 1 추가 + */ + protected abstract fun plusCurrentFilterIndex(): Int + + /** + * 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() + + filters.forEach { + if (this.getCurrentFilterIndex() == filters.size - 1) { + it.value.doFilter(request, response, this.getCalledFilterChain()) + } else { + it.value.doFilter(request, response, this) + } + this.plusCurrentFilterIndex() + } + } + + /** + * 외부에서 이 FilterChain을 동작시킬 때 사용하는 메소드 + * + * @param calledFilterChain 이 FilterChain을 호출한 FilterChain + */ + fun doFilterChained(request: ServletRequest, response: ServletResponse, calledFilterChain: FilterChain) { + this.setCalledFilterChain(calledFilterChain) + this.doFilter(request, response) + } +} diff --git a/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/filter/chained/ChainedFilterChainProxy.kt b/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/filter/chained/ChainedFilterChainProxy.kt new file mode 100644 index 000000000..c9d8ecf5c --- /dev/null +++ b/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/filter/chained/ChainedFilterChainProxy.kt @@ -0,0 +1,16 @@ +package com.info.maeumgagym.filter.chained + +import org.springframework.web.filter.GenericFilterBean +import javax.servlet.FilterChain +import javax.servlet.ServletRequest +import javax.servlet.ServletResponse + +abstract class ChainedFilterChainProxy : GenericFilterBean() { + + abstract fun getFilterChain(): ChainedFilterChain + + override fun doFilter(request: ServletRequest, response: ServletResponse, chain: FilterChain) { + val filterChain = this.getFilterChain() + filterChain.doFilterChained(request, response, chain) + } +} From eb4fe942111fae54bb613c741c63d075e4317285 Mon Sep 17 00:00:00 2001 From: Daybreak312 Date: Thu, 21 Mar 2024 23:39:05 +0900 Subject: [PATCH 03/10] =?UTF-8?q?REFACT=20::=20Exception=20=EC=B2=98?= =?UTF-8?q?=EB=A6=AC=20=EA=B4=80=EB=A0=A8=20=ED=95=84=ED=84=B0=EB=A5=BC=20?= =?UTF-8?q?SecurityFilterChain=EC=97=90=EC=84=9C=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../filter/ApplicationFilterChainConfig.kt | 84 +++++++++++++++++ .../config/filter/FilterChainConfig.kt | 89 ------------------- .../filter/SecurityFilterChainConfig.kt | 13 +-- .../error/filter/ExceptionConvertFilter.kt | 8 +- .../ExceptionChainedFilterChain.kt | 34 +++++++ .../ExceptionChainedFilterChainProxy.kt | 11 +++ .../filter/chained/ChainedFilterChain.kt | 21 +++-- .../filter/global/GlobalFilterChain.kt | 2 +- .../info/maeumgagym/security/jwt/JwtFilter.kt | 2 - 9 files changed, 154 insertions(+), 110 deletions(-) create mode 100644 maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/config/filter/ApplicationFilterChainConfig.kt delete mode 100644 maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/config/filter/FilterChainConfig.kt create mode 100644 maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/error/filter/filterchain/ExceptionChainedFilterChain.kt create mode 100644 maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/error/filter/filterchain/ExceptionChainedFilterChainProxy.kt diff --git a/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/config/filter/ApplicationFilterChainConfig.kt b/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/config/filter/ApplicationFilterChainConfig.kt new file mode 100644 index 000000000..581118461 --- /dev/null +++ b/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/config/filter/ApplicationFilterChainConfig.kt @@ -0,0 +1,84 @@ +package com.info.maeumgagym.config.filter + +import com.info.maeumgagym.common.exception.CriticalException +import com.info.maeumgagym.error.filter.ErrorLogResponseFilter +import com.info.maeumgagym.error.filter.ExceptionConvertFilter +import com.info.maeumgagym.error.filter.filterchain.ExceptionChainedFilterChain +import com.info.maeumgagym.error.filter.filterchain.ExceptionChainedFilterChainProxy +import com.info.maeumgagym.response.writer.DefaultHttpServletResponseWriter +import com.info.maeumgagym.response.writer.ErrorLogHttpServletResponseWriter +import org.springframework.boot.web.servlet.FilterRegistrationBean +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration +import javax.servlet.Filter +import javax.servlet.ServletContext + +/** + * [org.apache.catalina.core.ApplicationFilterChain] 속 Filter들의 삽입과 순서를 설정 + * + * @author Daybreak312 + * @since 27-02-2024 + */ +@Configuration +class ApplicationFilterChainConfig( + private val servletContext: ServletContext, + private val defaultHttpServletResponseWriter: DefaultHttpServletResponseWriter, + private val errorLogHttpServletResponseWriter: ErrorLogHttpServletResponseWriter +) { + + /** + * FilterChain에 등록할 [Filter]를 명시해두는 list. + * + * 명시된 순서에 따라 [Filter]를 등록할 것. + * + * [getFilterOrder] 메소드를 이용해 정수형 순서 정보를 얻을 수 있음 + */ + private val filtersOrderList = listOf>( + ExceptionChainedFilterChainProxy::class.java + ) + + @Bean + fun exceptionChainedFilterChainProxyConfig(): FilterRegistrationBean { + val filterChain = ExceptionChainedFilterChain( + mapOf( + Pair( + ErrorLogResponseFilter::class.simpleName!!, + ErrorLogResponseFilter(defaultHttpServletResponseWriter, errorLogHttpServletResponseWriter) + ), + Pair( + ExceptionConvertFilter::class.simpleName!!, + ExceptionConvertFilter() + ) + ) + ) + + val bean = FilterRegistrationBean( + ExceptionChainedFilterChainProxy(filterChain) + ) + + bean.addUrlPatterns("/*") + bean.order = getFilterOrder(ExceptionChainedFilterChainProxy::class.java) + + return bean + } + + @Bean + fun asddf() { + println(servletContext.filterRegistrations.toString()) + } + + /** + * [filtersOrderList]에 등록된 [Filter] 순서의 정수형 정보 반환. + * + * 만약 순서 정보를 얻으려는 [Filter]가 [filtersOrderList]에 등록되어 있지 않을 경우 [CriticalException] 발생. + */ + private fun getFilterOrder(`class`: Class): Int { + val index = this.filtersOrderList.indexOf(`class`) + + if (index == -1) { + throw CriticalException(500, "Attempted registration of Unknown filter") + } + + return index + } +} diff --git a/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/config/filter/FilterChainConfig.kt b/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/config/filter/FilterChainConfig.kt deleted file mode 100644 index a390096c0..000000000 --- a/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/config/filter/FilterChainConfig.kt +++ /dev/null @@ -1,89 +0,0 @@ -package com.info.maeumgagym.config.filter - -import com.info.maeumgagym.common.exception.CriticalException -import com.info.maeumgagym.error.filter.ErrorLogResponseFilter -import com.info.maeumgagym.error.filter.ExceptionConvertFilter -import com.info.maeumgagym.response.writer.DefaultHttpServletResponseWriter -import com.info.maeumgagym.response.writer.ErrorLogHttpServletResponseWriter -import org.apache.catalina.core.ApplicationFilterChain -import org.springframework.boot.web.servlet.FilterRegistrationBean -import org.springframework.security.web.SecurityFilterChain -import org.springframework.web.filter.DelegatingFilterProxy -import javax.servlet.Filter - -/** - * > 현재 해당 설정 관련 사항이 기술적 문제로 잠시 반려되었습니다. [ErrorLogResponseFilter]와 [ExceptionConvertFilter]는 [ApplicationFilterChain]이 아닌 [SecurityFilterChain]에 등록되어 있습니다. - */ -class FilterChainConfig( - private val defaultHttpServletResponseWriter: DefaultHttpServletResponseWriter, - private val errorLogHttpServletResponseWriter: ErrorLogHttpServletResponseWriter -) { - - /** - * FilterChain에 등록할 [Filter]를 명시해두는 list. - * - * 명시된 순서에 따라 [Filter]를 등록할 것. - * - * [getFilterOrder] 메소드를 이용해 정수형 순서 정보를 얻을 수 있음 - */ - private val filtersOrderList = listOf>( - ErrorLogResponseFilter::class.java, - ExceptionConvertFilter::class.java, - DelegatingFilterProxy::class.java - ) - - // @Bean - fun errorLogResponseFilterConfig(): FilterRegistrationBean { - val bean = FilterRegistrationBean( - ErrorLogResponseFilter(defaultHttpServletResponseWriter, errorLogHttpServletResponseWriter) - ) - - bean.addUrlPatterns("/*") - bean.order = getFilterOrder(ErrorLogResponseFilter::class.java) - - return bean - } - - // @Bean - fun exceptionConvertFilterConfig(): FilterRegistrationBean { - val bean = FilterRegistrationBean( - ExceptionConvertFilter() - ) - - bean.addUrlPatterns("/*") - bean.order = getFilterOrder(ExceptionConvertFilter::class.java) - - return bean - } - - /** - * [SecurityFilterChain]에 대한 순서 설정. - * - * 보다 정확히는, [SecurityFilterChain]을 감싼 [DelegatingFilterProxy]의 설정. - */ - // @Bean - fun springSecurityFilterChainOrderConfig(): FilterRegistrationBean { - val bean = FilterRegistrationBean( - DelegatingFilterProxy("springSecurityFilterChain") - ) - - bean.order = getFilterOrder(DelegatingFilterProxy::class.java) - - return bean - } - - /** - * [filtersOrderList]에 등록된 [Filter] 순서의 정수형 정보 반환. - * - * 만약 순서 정보를 얻으려는 [Filter]가 [filtersOrderList]에 등록되어 있지 않을 경우 [CriticalException] 발생. - */ - private fun getFilterOrder(`class`: Class): Int { - val index = this.filtersOrderList.indexOf(`class`) - - if (index == -1) { - throw CriticalException(500, "Attempted registration of Unknown filter") - } - - return index - } -} diff --git a/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/config/filter/SecurityFilterChainConfig.kt b/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/config/filter/SecurityFilterChainConfig.kt index 920abf4c7..f488706a9 100644 --- a/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/config/filter/SecurityFilterChainConfig.kt +++ b/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/config/filter/SecurityFilterChainConfig.kt @@ -4,7 +4,10 @@ import com.info.maeumgagym.error.filter.ErrorLogResponseFilter import com.info.maeumgagym.error.filter.ExceptionConvertFilter import com.info.maeumgagym.response.writer.DefaultHttpServletResponseWriter import com.info.maeumgagym.response.writer.ErrorLogHttpServletResponseWriter +import com.info.maeumgagym.security.jwt.AuthenticationProvider import com.info.maeumgagym.security.jwt.JwtFilter +import com.info.maeumgagym.security.jwt.JwtResolver +import com.info.maeumgagym.security.jwt.env.JwtProperties import org.springframework.security.config.annotation.SecurityConfigurerAdapter import org.springframework.security.config.annotation.web.builders.HttpSecurity import org.springframework.security.web.DefaultSecurityFilterChain @@ -14,7 +17,9 @@ import org.springframework.stereotype.Component @Component class SecurityFilterChainConfig( - private val jwtFilter: JwtFilter, + private val jwtResolver: JwtResolver, + private val jwtProperties: JwtProperties, + private val authenticationProvider: AuthenticationProvider, private val defaultHttpServletResponseWriter: DefaultHttpServletResponseWriter, private val errorLogHttpServletResponseWriter: ErrorLogHttpServletResponseWriter ) : SecurityConfigurerAdapter() { @@ -27,11 +32,9 @@ class SecurityFilterChainConfig( */ override fun configure(builder: HttpSecurity) { builder.run { - addFilterBefore(jwtFilter, LogoutFilter::class.java) - addFilterBefore(ExceptionConvertFilter(), SecurityContextHolderFilter::class.java) addFilterBefore( - ErrorLogResponseFilter(defaultHttpServletResponseWriter, errorLogHttpServletResponseWriter), - ExceptionConvertFilter::class.java + JwtFilter(jwtResolver, authenticationProvider, jwtProperties), + LogoutFilter::class.java ) } } diff --git a/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/error/filter/ExceptionConvertFilter.kt b/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/error/filter/ExceptionConvertFilter.kt index 77e00bfbe..7032f8298 100644 --- a/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/error/filter/ExceptionConvertFilter.kt +++ b/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/error/filter/ExceptionConvertFilter.kt @@ -2,10 +2,8 @@ package com.info.maeumgagym.error.filter import com.info.maeumgagym.common.exception.MaeumGaGymException import com.info.maeumgagym.common.exception.PresentationValidationException -import com.info.maeumgagym.config.filter.FilterChainConfig +import com.info.maeumgagym.config.filter.ApplicationFilterChainConfig import com.info.maeumgagym.error.vo.ErrorLog -import org.apache.catalina.core.ApplicationFilterChain -import org.springframework.security.web.SecurityFilterChain import org.springframework.web.bind.MethodArgumentNotValidException import org.springframework.web.bind.MissingServletRequestParameterException import org.springframework.web.filter.GenericFilterBean @@ -31,9 +29,7 @@ import javax.validation.ConstraintViolationException * - Presentation 계층에서 Validation이 실패했을 경우 발생하는 예외 중 하나일 경우 [PresentationValidationException]으로 변환; 변환되는 타입 : [MethodArgumentNotValidException], [ConstraintViolationException], [MissingServletRequestParameterException] * - 그 외에는 그대로 변환 * - * 해당 *Filter*의 순서 설정 정보는 [FilterChainConfig]에 존재 - * - * > 위의 사항이 기술적 문제로 잠시 반려되었습니다. 현재 [ApplicationFilterChain]이 아닌 [SecurityFilterChain]에 등록되어 있습니다. + * 해당 *Filter*의 순서 설정 정보는 [ApplicationFilterChainConfig]에 존재 * * @see ErrorLogResponseFilter */ diff --git a/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/error/filter/filterchain/ExceptionChainedFilterChain.kt b/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/error/filter/filterchain/ExceptionChainedFilterChain.kt new file mode 100644 index 000000000..b822c1543 --- /dev/null +++ b/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/error/filter/filterchain/ExceptionChainedFilterChain.kt @@ -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, +) : ChainedFilterChain() { + + private var calledFilterChain: FilterChain? = null + + private var currentFilterIndex: Int = -1 + + override fun getFilters(): Map = filters + + override fun getCalledFilterChain(): FilterChain? = calledFilterChain + + override fun getCurrentFilterIndex(): Int = this.currentFilterIndex + + override fun plusCurrentFilterIndex(): Int { + this.currentFilterIndex += 1 + return this.currentFilterIndex + } + + override fun resetFilterChain() { + this.calledFilterChain = null + this.currentFilterIndex = -1 + } + + override fun setCalledFilterChain(filterChain: FilterChain) { + this.calledFilterChain = filterChain + } +} diff --git a/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/error/filter/filterchain/ExceptionChainedFilterChainProxy.kt b/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/error/filter/filterchain/ExceptionChainedFilterChainProxy.kt new file mode 100644 index 000000000..6ddce1a89 --- /dev/null +++ b/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/error/filter/filterchain/ExceptionChainedFilterChainProxy.kt @@ -0,0 +1,11 @@ +package com.info.maeumgagym.error.filter.filterchain + +import com.info.maeumgagym.filter.chained.ChainedFilterChain +import com.info.maeumgagym.filter.chained.ChainedFilterChainProxy + +class ExceptionChainedFilterChainProxy( + private val exceptionChainedFilterChain: ExceptionChainedFilterChain +) : ChainedFilterChainProxy() { + + override fun getFilterChain(): ChainedFilterChain = this.exceptionChainedFilterChain +} diff --git a/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/filter/chained/ChainedFilterChain.kt b/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/filter/chained/ChainedFilterChain.kt index 37a96f398..c735cdbda 100644 --- a/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/filter/chained/ChainedFilterChain.kt +++ b/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/filter/chained/ChainedFilterChain.kt @@ -2,6 +2,7 @@ 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 @@ -58,13 +59,19 @@ abstract class ChainedFilterChain : GlobalFilterChain { /** * FilterChain이 끝난 후, 다시 초기 상태로 되돌리는 메소드 */ - protected abstract fun removeCalledFilterChain() + 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들이 호출하는 메소드 */ @@ -75,13 +82,12 @@ abstract class ChainedFilterChain : GlobalFilterChain { val filters = this.getFilters() - filters.forEach { - if (this.getCurrentFilterIndex() == filters.size - 1) { - it.value.doFilter(request, response, this.getCalledFilterChain()) - } else { - it.value.doFilter(request, response, this) - } + if (this.getCurrentFilterIndex() == filters.size - 2) { + this.plusCurrentFilterIndex() + this.getCurrentFilter().doFilter(request, response, this.getCalledFilterChain()) + } else { this.plusCurrentFilterIndex() + this.getCurrentFilter().doFilter(request, response, this) } } @@ -93,5 +99,6 @@ abstract class ChainedFilterChain : GlobalFilterChain { fun doFilterChained(request: ServletRequest, response: ServletResponse, calledFilterChain: FilterChain) { this.setCalledFilterChain(calledFilterChain) this.doFilter(request, response) + this.resetFilterChain() } } diff --git a/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/filter/global/GlobalFilterChain.kt b/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/filter/global/GlobalFilterChain.kt index e19c2501b..5de83882a 100644 --- a/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/filter/global/GlobalFilterChain.kt +++ b/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/filter/global/GlobalFilterChain.kt @@ -23,7 +23,7 @@ interface GlobalFilterChain : FilterChain { fun getFilters(): Map /** - * @return 현재 실행중인 Filter의 Index 반환 + * @return 호출 시점에, 가장 마지막으로 실행한 Filter의 Index 반환 */ fun getCurrentFilterIndex(): Int diff --git a/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/security/jwt/JwtFilter.kt b/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/security/jwt/JwtFilter.kt index 376232a80..d6258a2ef 100644 --- a/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/security/jwt/JwtFilter.kt +++ b/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/security/jwt/JwtFilter.kt @@ -4,7 +4,6 @@ import com.info.maeumgagym.security.config.RequestPermitConfig import com.info.maeumgagym.security.jwt.env.JwtProperties import com.info.maeumgagym.user.model.User import org.springframework.security.core.context.SecurityContextHolder -import org.springframework.stereotype.Component import org.springframework.util.AntPathMatcher import org.springframework.web.filter.OncePerRequestFilter import javax.servlet.FilterChain @@ -18,7 +17,6 @@ import javax.servlet.http.HttpServletResponse * * @author Daybreak312, kanghyuk */ -@Component class JwtFilter( private val jwtResolver: JwtResolver, private val authenticationProvider: AuthenticationProvider, From f8d84c6d454c135c19a74ee16680c1fb01f020bd Mon Sep 17 00:00:00 2001 From: Daybreak312 Date: Thu, 21 Mar 2024 23:44:53 +0900 Subject: [PATCH 04/10] =?UTF-8?q?ANNO=20::=20=EB=AC=B8=EC=84=9C=ED=99=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../error/filter/ErrorLogResponseFilter.kt | 14 ++++++-------- .../error/filter/ExceptionConvertFilter.kt | 14 +++++++------- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/error/filter/ErrorLogResponseFilter.kt b/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/error/filter/ErrorLogResponseFilter.kt index e957965fb..f23d6d811 100644 --- a/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/error/filter/ErrorLogResponseFilter.kt +++ b/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/error/filter/ErrorLogResponseFilter.kt @@ -4,29 +4,27 @@ import com.info.maeumgagym.common.exception.* import com.info.maeumgagym.error.vo.ErrorLog import com.info.maeumgagym.response.writer.DefaultHttpServletResponseWriter import com.info.maeumgagym.response.writer.ErrorLogHttpServletResponseWriter -import org.apache.catalina.core.ApplicationFilterChain -import org.springframework.security.web.SecurityFilterChain import org.springframework.web.filter.OncePerRequestFilter -import org.springframework.web.servlet.DispatcherServlet import org.springframework.web.util.NestedServletException import javax.servlet.FilterChain import javax.servlet.http.HttpServletRequest import javax.servlet.http.HttpServletResponse /** - * [Exception]이 발생했을 때, [ErrorLog] 및 [ErrorLogResponse]를 작성 + * [Exception]이 발생했을 때, 응답 및 로그를 작성 * * [doFilter], 정확히는 [doFilterInternal]를 *try*문으로 감싸 실행. * 이후 발생한 모든 예외를 *catch*해 각 예외에 따라 [ErrorLog]및 그에 대한 Response를 작성. * @see [ErrorLogHttpServletResponseWriter] * - * 원래 [DispatcherServlet] 통과 이후 발생한 예외는 [NestedServletException.cause]로 감싸져 *throw*되지만, [ExceptionConvertFilter]에서 이를 [MaeumGaGymException]의 하위 타입으로 변환함. 자세한 것은 [ExceptionConvertFilter] 참조. + * 원래 [org.springframework.web.servlet.DispatcherServlet] 통과 이후 발생한 예외는 [NestedServletException.cause]로 감싸져 *throw*되지만, [ExceptionConvertFilter]에서 이를 [MaeumGaGymException]의 하위 타입으로 변환함. 자세한 것은 [ExceptionConvertFilter] 참조. * - * 해당 *Filter*의 순서 설정 정보는 [FilterChainConfig]에 존재 - * - * > 위의 사항이 기술적 문제로 잠시 반려되었습니다. 현재 [ApplicationFilterChain]이 아닌 [SecurityFilterChain]에 등록되어 있습니다. + * 해당 *Filter*의 순서 설정 정보는 [com.info.maeumgagym.config.filter.ApplicationFilterChainConfig]에 존재 * * @see ExceptionConvertFilter + * + * @author Daybreak312 + * @since 22.02.2024 */ class ErrorLogResponseFilter( private val defaultHttpServletResponseWriter: DefaultHttpServletResponseWriter, diff --git a/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/error/filter/ExceptionConvertFilter.kt b/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/error/filter/ExceptionConvertFilter.kt index 7032f8298..c7def82c1 100644 --- a/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/error/filter/ExceptionConvertFilter.kt +++ b/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/error/filter/ExceptionConvertFilter.kt @@ -2,12 +2,9 @@ package com.info.maeumgagym.error.filter import com.info.maeumgagym.common.exception.MaeumGaGymException import com.info.maeumgagym.common.exception.PresentationValidationException -import com.info.maeumgagym.config.filter.ApplicationFilterChainConfig -import com.info.maeumgagym.error.vo.ErrorLog import org.springframework.web.bind.MethodArgumentNotValidException import org.springframework.web.bind.MissingServletRequestParameterException import org.springframework.web.filter.GenericFilterBean -import org.springframework.web.servlet.DispatcherServlet import org.springframework.web.util.NestedServletException import javax.servlet.FilterChain import javax.servlet.ServletRequest @@ -17,10 +14,10 @@ import javax.validation.ConstraintViolationException /** * [doFilter]를 *try~catch*문으로 감싼 형태로 실행해 예외를 [MaeumGaGymException]으로 변환하는 작업을 실행 * - * [DispatcherServlet] 이후에 발생한 예외는 [NestedServletException.cause]로 감싸져 전달됨. + * [org.springframework.web.servlet.DispatcherServlet] 이후에 발생한 예외는 [NestedServletException.cause]로 감싸져 전달됨. * 이 예외의 타입을 확인하고 해당 타입에 맞는 다른 예외로 변환해 *throw* * - * 이것의 [ErrorLog]와 [ErrorLogResponse]는 [ErrorLogResponseFilter]에서 처리함 + * 이것에 대한 응답 및 로그 작성의 책임은 [ErrorLogResponseFilter]에서 담당 * * ``` * catch (e: [NestedServletException]) { ... } @@ -29,11 +26,14 @@ import javax.validation.ConstraintViolationException * - Presentation 계층에서 Validation이 실패했을 경우 발생하는 예외 중 하나일 경우 [PresentationValidationException]으로 변환; 변환되는 타입 : [MethodArgumentNotValidException], [ConstraintViolationException], [MissingServletRequestParameterException] * - 그 외에는 그대로 변환 * - * 해당 *Filter*의 순서 설정 정보는 [ApplicationFilterChainConfig]에 존재 + * 해당 *Filter*의 순서 설정 정보는 [com.info.maeumgagym.config.filter.ApplicationFilterChainConfig]에 존재 * * @see ErrorLogResponseFilter + * + * @author Daybreak312 + * @since 22.02.2024 */ -class ExceptionConvertFilter() : GenericFilterBean() { +class ExceptionConvertFilter : GenericFilterBean() { override fun doFilter(request: ServletRequest, response: ServletResponse, chain: FilterChain) { try { From 51c075e5700831c061fece54059d6b1d44b6784a Mon Sep 17 00:00:00 2001 From: Daybreak312 Date: Fri, 22 Mar 2024 13:36:00 +0900 Subject: [PATCH 05/10] FIX :: FIX Test Code Dependency --- .../kotlin/com/info/maeumgagym/domain/auth/JwtTests.kt | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/maeumgagym-infrastructure/src/test/kotlin/com/info/maeumgagym/domain/auth/JwtTests.kt b/maeumgagym-infrastructure/src/test/kotlin/com/info/maeumgagym/domain/auth/JwtTests.kt index e3ef254b2..b78a037e8 100644 --- a/maeumgagym-infrastructure/src/test/kotlin/com/info/maeumgagym/domain/auth/JwtTests.kt +++ b/maeumgagym-infrastructure/src/test/kotlin/com/info/maeumgagym/domain/auth/JwtTests.kt @@ -7,6 +7,8 @@ import com.info.maeumgagym.domain.user.entity.UserJpaEntity import com.info.maeumgagym.domain.user.repository.UserRepository import com.info.maeumgagym.error.TestException import com.info.maeumgagym.security.jwt.JwtFilter +import com.info.maeumgagym.security.jwt.env.JwtProperties +import com.info.maeumgagym.security.jwt.impl.AuthenticationProviderImpl import com.info.maeumgagym.security.jwt.impl.JwtAdapter import com.info.maeumgagym.security.jwt.impl.JwtResolverImpl import com.info.maeumgagym.security.jwt.repository.AccessTokenRepository @@ -24,14 +26,17 @@ import org.springframework.transaction.annotation.Transactional @Transactional @SpringBootTest internal class JwtTests @Autowired constructor( - private val jwtFilter: JwtFilter, private val jwtAdapter: JwtAdapter, private val jwtResolver: JwtResolverImpl, + private val jwtProperties: JwtProperties, + private val authenticationProviderImpl: AuthenticationProviderImpl, private val accessTokenRepository: AccessTokenRepository, private val userRepository: UserRepository, private val refreshTokenRepository: RefreshTokenRepository ) { + private val jwtFilter: JwtFilter = JwtFilter(jwtResolver, authenticationProviderImpl, jwtProperties) + private lateinit var user: UserJpaEntity @BeforeEach From a4c56bfbc63b1884a4011f7a0d03be93a7b47f5c Mon Sep 17 00:00:00 2001 From: Daybreak312 Date: Fri, 22 Mar 2024 13:36:54 +0900 Subject: [PATCH 06/10] REFACT :: ktlintFormat --- .../error/filter/filterchain/ExceptionChainedFilterChain.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/error/filter/filterchain/ExceptionChainedFilterChain.kt b/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/error/filter/filterchain/ExceptionChainedFilterChain.kt index b822c1543..de6cd532e 100644 --- a/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/error/filter/filterchain/ExceptionChainedFilterChain.kt +++ b/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/error/filter/filterchain/ExceptionChainedFilterChain.kt @@ -5,7 +5,7 @@ import javax.servlet.Filter import javax.servlet.FilterChain class ExceptionChainedFilterChain( - private val filters: Map, + private val filters: Map ) : ChainedFilterChain() { private var calledFilterChain: FilterChain? = null From a3b35c60d76704a8942dbcb50b6c53fec1275f2a Mon Sep 17 00:00:00 2001 From: Daybreak312 Date: Fri, 22 Mar 2024 14:21:42 +0900 Subject: [PATCH 07/10] =?UTF-8?q?FIX=20::=20FilterChain=20=EC=9E=84?= =?UTF-8?q?=EA=B3=84=20=EC=98=81=EC=97=AD=EC=9D=84=20ThreadLocal=EB=A1=9C?= =?UTF-8?q?=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../filterchain/ExceptionChainedFilterChain.kt | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/error/filter/filterchain/ExceptionChainedFilterChain.kt b/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/error/filter/filterchain/ExceptionChainedFilterChain.kt index de6cd532e..d1a311b80 100644 --- a/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/error/filter/filterchain/ExceptionChainedFilterChain.kt +++ b/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/error/filter/filterchain/ExceptionChainedFilterChain.kt @@ -8,27 +8,29 @@ class ExceptionChainedFilterChain( private val filters: Map ) : ChainedFilterChain() { - private var calledFilterChain: FilterChain? = null + private var calledFilterChain: ThreadLocal? = null - private var currentFilterIndex: Int = -1 + private var currentFilterIndex: ThreadLocal = ThreadLocal.withInitial { + -1 + } override fun getFilters(): Map = filters - override fun getCalledFilterChain(): FilterChain? = calledFilterChain + override fun getCalledFilterChain(): FilterChain? = calledFilterChain?.get() - override fun getCurrentFilterIndex(): Int = this.currentFilterIndex + override fun getCurrentFilterIndex(): Int = this.currentFilterIndex.get() override fun plusCurrentFilterIndex(): Int { - this.currentFilterIndex += 1 - return this.currentFilterIndex + this.currentFilterIndex.set(this.getCurrentFilterIndex() + 1) + return this.currentFilterIndex.get() } override fun resetFilterChain() { this.calledFilterChain = null - this.currentFilterIndex = -1 + this.currentFilterIndex = ThreadLocal.withInitial { -1 } } override fun setCalledFilterChain(filterChain: FilterChain) { - this.calledFilterChain = filterChain + this.calledFilterChain = ThreadLocal.withInitial { filterChain } } } From 3ec8525000b52b8a2e9a1be3f8b727e9a6f08b2c Mon Sep 17 00:00:00 2001 From: Daybreak312 Date: Fri, 22 Mar 2024 15:09:02 +0900 Subject: [PATCH 08/10] =?UTF-8?q?TRUCK=20::=20Filter=20Configs=20=EC=9D=B4?= =?UTF-8?q?=EB=8F=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../maeumgagym/error/filter/ErrorLogResponseFilter.kt | 2 +- .../maeumgagym/error/filter/ExceptionConvertFilter.kt | 2 +- .../config}/ApplicationFilterChainConfig.kt | 2 +- .../filter => filter/config}/SecurityFilterChainConfig.kt | 8 ++------ .../com/info/maeumgagym/security/config/SecurityConfig.kt | 2 +- 5 files changed, 6 insertions(+), 10 deletions(-) rename maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/{config/filter => filter/config}/ApplicationFilterChainConfig.kt (98%) rename maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/{config/filter => filter/config}/SecurityFilterChainConfig.kt (77%) diff --git a/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/error/filter/ErrorLogResponseFilter.kt b/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/error/filter/ErrorLogResponseFilter.kt index f23d6d811..23f0cf5d4 100644 --- a/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/error/filter/ErrorLogResponseFilter.kt +++ b/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/error/filter/ErrorLogResponseFilter.kt @@ -19,7 +19,7 @@ import javax.servlet.http.HttpServletResponse * * 원래 [org.springframework.web.servlet.DispatcherServlet] 통과 이후 발생한 예외는 [NestedServletException.cause]로 감싸져 *throw*되지만, [ExceptionConvertFilter]에서 이를 [MaeumGaGymException]의 하위 타입으로 변환함. 자세한 것은 [ExceptionConvertFilter] 참조. * - * 해당 *Filter*의 순서 설정 정보는 [com.info.maeumgagym.config.filter.ApplicationFilterChainConfig]에 존재 + * 해당 *Filter*의 순서 설정 정보는 [com.info.maeumgagym.config.config.ApplicationFilterChainConfig]에 존재 * * @see ExceptionConvertFilter * diff --git a/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/error/filter/ExceptionConvertFilter.kt b/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/error/filter/ExceptionConvertFilter.kt index c7def82c1..dda294ee8 100644 --- a/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/error/filter/ExceptionConvertFilter.kt +++ b/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/error/filter/ExceptionConvertFilter.kt @@ -26,7 +26,7 @@ import javax.validation.ConstraintViolationException * - Presentation 계층에서 Validation이 실패했을 경우 발생하는 예외 중 하나일 경우 [PresentationValidationException]으로 변환; 변환되는 타입 : [MethodArgumentNotValidException], [ConstraintViolationException], [MissingServletRequestParameterException] * - 그 외에는 그대로 변환 * - * 해당 *Filter*의 순서 설정 정보는 [com.info.maeumgagym.config.filter.ApplicationFilterChainConfig]에 존재 + * 해당 *Filter*의 순서 설정 정보는 [com.info.maeumgagym.config.config.ApplicationFilterChainConfig]에 존재 * * @see ErrorLogResponseFilter * diff --git a/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/config/filter/ApplicationFilterChainConfig.kt b/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/filter/config/ApplicationFilterChainConfig.kt similarity index 98% rename from maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/config/filter/ApplicationFilterChainConfig.kt rename to maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/filter/config/ApplicationFilterChainConfig.kt index 581118461..1093694d1 100644 --- a/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/config/filter/ApplicationFilterChainConfig.kt +++ b/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/filter/config/ApplicationFilterChainConfig.kt @@ -1,4 +1,4 @@ -package com.info.maeumgagym.config.filter +package com.info.maeumgagym.filter.config import com.info.maeumgagym.common.exception.CriticalException import com.info.maeumgagym.error.filter.ErrorLogResponseFilter diff --git a/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/config/filter/SecurityFilterChainConfig.kt b/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/filter/config/SecurityFilterChainConfig.kt similarity index 77% rename from maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/config/filter/SecurityFilterChainConfig.kt rename to maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/filter/config/SecurityFilterChainConfig.kt index f488706a9..a0aaa7ab2 100644 --- a/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/config/filter/SecurityFilterChainConfig.kt +++ b/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/filter/config/SecurityFilterChainConfig.kt @@ -1,9 +1,7 @@ -package com.info.maeumgagym.config.filter +package com.info.maeumgagym.filter.config import com.info.maeumgagym.error.filter.ErrorLogResponseFilter import com.info.maeumgagym.error.filter.ExceptionConvertFilter -import com.info.maeumgagym.response.writer.DefaultHttpServletResponseWriter -import com.info.maeumgagym.response.writer.ErrorLogHttpServletResponseWriter import com.info.maeumgagym.security.jwt.AuthenticationProvider import com.info.maeumgagym.security.jwt.JwtFilter import com.info.maeumgagym.security.jwt.JwtResolver @@ -19,9 +17,7 @@ import org.springframework.stereotype.Component class SecurityFilterChainConfig( private val jwtResolver: JwtResolver, private val jwtProperties: JwtProperties, - private val authenticationProvider: AuthenticationProvider, - private val defaultHttpServletResponseWriter: DefaultHttpServletResponseWriter, - private val errorLogHttpServletResponseWriter: ErrorLogHttpServletResponseWriter + private val authenticationProvider: AuthenticationProvider ) : SecurityConfigurerAdapter() { /** diff --git a/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/security/config/SecurityConfig.kt b/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/security/config/SecurityConfig.kt index 276ec0ef0..557f41641 100644 --- a/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/security/config/SecurityConfig.kt +++ b/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/security/config/SecurityConfig.kt @@ -1,6 +1,6 @@ package com.info.maeumgagym.security.config -import com.info.maeumgagym.config.filter.SecurityFilterChainConfig +import com.info.maeumgagym.filter.config.SecurityFilterChainConfig import com.info.maeumgagym.error.handler.CustomAccessDeniedHandler import com.info.maeumgagym.error.handler.CustomAuthenticationEntryPoint import com.info.maeumgagym.security.env.CSRFProperties From 3917008d40f14834f949207762ba971104ba4835 Mon Sep 17 00:00:00 2001 From: Daybreak312 Date: Fri, 22 Mar 2024 15:09:45 +0900 Subject: [PATCH 09/10] =?UTF-8?q?REFACT=20::=20FilterChainProxy=20?= =?UTF-8?q?=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ExceptionChainedFilterChainProxy.kt | 7 +++---- .../filter/chained/ChainedFilterChainProxy.kt | 14 +++----------- .../filter/global/GlobalFilterChainProxy.kt | 8 +++++--- 3 files changed, 11 insertions(+), 18 deletions(-) diff --git a/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/error/filter/filterchain/ExceptionChainedFilterChainProxy.kt b/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/error/filter/filterchain/ExceptionChainedFilterChainProxy.kt index 6ddce1a89..e64ca5959 100644 --- a/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/error/filter/filterchain/ExceptionChainedFilterChainProxy.kt +++ b/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/error/filter/filterchain/ExceptionChainedFilterChainProxy.kt @@ -1,11 +1,10 @@ package com.info.maeumgagym.error.filter.filterchain -import com.info.maeumgagym.filter.chained.ChainedFilterChain import com.info.maeumgagym.filter.chained.ChainedFilterChainProxy class ExceptionChainedFilterChainProxy( - private val exceptionChainedFilterChain: ExceptionChainedFilterChain -) : ChainedFilterChainProxy() { + exceptionChainedFilterChain: ExceptionChainedFilterChain +) : ChainedFilterChainProxy() { - override fun getFilterChain(): ChainedFilterChain = this.exceptionChainedFilterChain + override val filterChain: ExceptionChainedFilterChain = exceptionChainedFilterChain } diff --git a/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/filter/chained/ChainedFilterChainProxy.kt b/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/filter/chained/ChainedFilterChainProxy.kt index c9d8ecf5c..709bafd14 100644 --- a/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/filter/chained/ChainedFilterChainProxy.kt +++ b/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/filter/chained/ChainedFilterChainProxy.kt @@ -1,16 +1,8 @@ package com.info.maeumgagym.filter.chained -import org.springframework.web.filter.GenericFilterBean -import javax.servlet.FilterChain -import javax.servlet.ServletRequest -import javax.servlet.ServletResponse +import com.info.maeumgagym.filter.global.GlobalFilterChainProxy -abstract class ChainedFilterChainProxy : GenericFilterBean() { +abstract class ChainedFilterChainProxy : GlobalFilterChainProxy() { - abstract fun getFilterChain(): ChainedFilterChain - - override fun doFilter(request: ServletRequest, response: ServletResponse, chain: FilterChain) { - val filterChain = this.getFilterChain() - filterChain.doFilterChained(request, response, chain) - } + abstract override val filterChain: T } diff --git a/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/filter/global/GlobalFilterChainProxy.kt b/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/filter/global/GlobalFilterChainProxy.kt index a8a83675d..c35acf302 100644 --- a/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/filter/global/GlobalFilterChainProxy.kt +++ b/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/filter/global/GlobalFilterChainProxy.kt @@ -10,14 +10,16 @@ import javax.servlet.ServletResponse * * @see GlobalFilterChain * + * @param T 해당 Proxy 클래스가 대리하고 있는 FilterChain + * * @author Daybreak312 * @since 21-03-2024 */ -abstract class GlobalFilterChainProxy : GenericFilterBean() { +abstract class GlobalFilterChainProxy : GenericFilterBean() { - protected abstract val filterChain: GlobalFilterChain + protected abstract val filterChain: T override fun doFilter(request: ServletRequest, response: ServletResponse, chain: FilterChain) { - filterChain.doFilter(request, response) + this.filterChain.doFilter(request, response) } } From dc4e8a829712540418352f3ecc435b2d347eee80 Mon Sep 17 00:00:00 2001 From: Daybreak312 Date: Fri, 22 Mar 2024 15:39:12 +0900 Subject: [PATCH 10/10] REFACT --- .../error/filter/filterchain/ExceptionChainedFilterChain.kt | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/error/filter/filterchain/ExceptionChainedFilterChain.kt b/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/error/filter/filterchain/ExceptionChainedFilterChain.kt index d1a311b80..2cb03658b 100644 --- a/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/error/filter/filterchain/ExceptionChainedFilterChain.kt +++ b/maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/error/filter/filterchain/ExceptionChainedFilterChain.kt @@ -10,9 +10,7 @@ class ExceptionChainedFilterChain( private var calledFilterChain: ThreadLocal? = null - private var currentFilterIndex: ThreadLocal = ThreadLocal.withInitial { - -1 - } + private var currentFilterIndex: ThreadLocal = ThreadLocal.withInitial { -1 } override fun getFilters(): Map = filters