-
Notifications
You must be signed in to change notification settings - Fork 0
[Web-Filter] 필터 및 필터체인 개발 #47
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
Changes from all commits
72d2055
43d474e
559e1b5
7157d40
23d010d
a2fa1cb
f565469
e675082
f29192b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| package config; | ||
|
|
||
| import exception.ErrorException; | ||
| import web.filter.ServletFilter; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class FilterConfig extends SingletonContainer { | ||
| private final AppConfig appConfig = new AppConfig(); | ||
| private int callCount = 0; | ||
|
|
||
| public void config(){ | ||
| if(callCount>0) throw new ErrorException("FilterConfig::set: Duplicated call"); | ||
| setFilterChains(); | ||
| callCount++; | ||
| } | ||
|
|
||
| private void setFilterChains(){ | ||
| appConfig.filterChainContainer() | ||
| .addFilterList(FilterType.ALL, getFilterListByAuthorityType(FilterType.ALL)) | ||
| .addFilterList(FilterType.PUBLIC, getFilterListByAuthorityType(FilterType.PUBLIC)) | ||
| .addFilterList(FilterType.AUTHENTICATED, getFilterListByAuthorityType(FilterType.AUTHENTICATED)) | ||
| .addFilterList(FilterType.RESTRICT, getFilterListByAuthorityType(FilterType.RESTRICT)); | ||
| } | ||
|
|
||
| private List<ServletFilter> commonFrontFilter(){ | ||
| return getOrCreate("commonFrontFilter", | ||
| () -> List.of( | ||
| appConfig.accessLogFilter() | ||
| )); | ||
| } | ||
|
|
||
| private List<ServletFilter> commonBackFilter(){ | ||
| return getOrCreate("commonBackFilter", | ||
| () -> List.of()); | ||
|
Comment on lines
32
to
36
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 중복된 key로 인한 캐싱 버그: |
||
| } | ||
|
|
||
| private List<ServletFilter> getFilterListByAuthorityType(FilterType type) { | ||
| List<ServletFilter> servletFilterList = new ArrayList<>(); | ||
| servletFilterList.addAll(commonFrontFilter()); | ||
| servletFilterList.addAll(authorizedFilterList(type)); | ||
| servletFilterList.addAll(commonBackFilter()); | ||
| return servletFilterList; | ||
| } | ||
|
|
||
| private List<ServletFilter> authorizedFilterList(FilterType type) { | ||
| return switch (type) { | ||
| case ALL -> List.of(); | ||
| case PUBLIC -> List.of(); | ||
| case AUTHENTICATED -> List.of(); | ||
| case RESTRICT -> List.of(appConfig.restrictedFilter()); | ||
| case LOG_IN -> List.of(); | ||
| }; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package config; | ||
|
|
||
| public enum FilterType { | ||
| ALL, | ||
| PUBLIC, | ||
| AUTHENTICATED, | ||
| RESTRICT, | ||
| LOG_IN | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package config; | ||
|
|
||
| import exception.ErrorException; | ||
|
|
||
| public class SecurityConfig extends SingletonContainer { | ||
| private final AppConfig appConfig = new AppConfig(); | ||
| private int callCount; | ||
|
|
||
| public void config(){ | ||
| if(callCount>0) throw new ErrorException("SecurityConfig::setPaths: Duplicated call"); | ||
| setPaths(); | ||
| callCount++; | ||
| } | ||
|
|
||
| public void setPaths(){ | ||
| appConfig.filterChainContainer() | ||
| .addPath(FilterType.AUTHENTICATED, "/mypage/**") | ||
| .addPath(FilterType.ALL, "/user/**") | ||
| .addPath(FilterType.PUBLIC, "/**"); | ||
| } | ||
|
|
||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,8 @@ | ||
| package http; | ||
|
|
||
| public enum HttpStatus { | ||
| NONE(0), | ||
|
|
||
| OK(200), | ||
| CREATED(201), | ||
| ACCEPTED(202), | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,22 +5,23 @@ | |
| import http.request.HttpRequestConverter; | ||
| import http.request.HttpRequest; | ||
| import http.response.HttpResponse; | ||
| import web.filter.FilterChainContainer; | ||
|
|
||
| import java.net.Socket; | ||
|
|
||
| public class ConnectionHandler implements Runnable{ | ||
| private final Socket connection; | ||
| private final FilterChainContainer filterChainContainer; | ||
| private final HttpRequestConverter requestConverter; | ||
| private final HttpResponseConverter responseConverter; | ||
| private final ExceptionHandlerMapping exceptionHandlerMapping; | ||
| private final Dispatcher dispatcher; | ||
|
|
||
| public ConnectionHandler(Dispatcher dispatcher, | ||
| public ConnectionHandler(FilterChainContainer filterChainContainer, | ||
| ExceptionHandlerMapping exceptionHandlerMapping, | ||
| HttpResponseConverter responseConverter, | ||
| HttpRequestConverter requestConverter, | ||
| Socket connection) { | ||
| this.dispatcher = dispatcher; | ||
| this.filterChainContainer = filterChainContainer; | ||
| this.exceptionHandlerMapping = exceptionHandlerMapping; | ||
| this.responseConverter = responseConverter; | ||
| this.requestConverter = requestConverter; | ||
|
|
@@ -30,22 +31,23 @@ public ConnectionHandler(Dispatcher dispatcher, | |
| @Override | ||
| public void run() { | ||
|
|
||
| HttpResponse response = HttpResponse.of(); | ||
| try { | ||
|
|
||
| HttpRequest request = requestConverter.parseRequest(connection); | ||
| HttpResponse response = dispatcher.handle(request); | ||
| filterChainContainer.runFilterChain(request,response); | ||
| responseConverter.sendResponse(response, connection); | ||
|
|
||
| } catch (Exception e){ | ||
| } catch (Throwable t){ | ||
| /** | ||
| * TODO: | ||
| * ExceptionHandler 또한 HttpResponse를 반환하게 하고 | ||
| * finally에 `responseConverter.sendResponse(response, connection);` 를 넣어 | ||
| * socket에 write를 하는 포인트를 단일 포인트로 관리 | ||
| */ | ||
| exceptionHandlerMapping.handle(e, connection); | ||
| exceptionHandlerMapping.handle(t, connection); | ||
| } finally { | ||
|
Comment on lines
33
to
49
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. HttpResponse 상태 관리 모호성: 예외 발생 시 |
||
| try { connection.close(); } catch (Exception ignore) {} | ||
| try { connection.close(); } catch (Throwable ignore) {} | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package web.filter; | ||
|
|
||
| import http.request.HttpRequest; | ||
| import http.response.HttpResponse; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| public class AccessLogFilter implements ServletFilter { | ||
|
|
||
| private static final Logger log = LoggerFactory.getLogger(AccessLogFilter.class); | ||
|
|
||
| @Override | ||
| public void runFilter(HttpRequest request, HttpResponse response, FilterChainContainer.FilterChainEngine chain) { | ||
| chain.doFilter(); | ||
| log.info("rid-{}: {} {} from {}", | ||
| request.getOrGenerateRid(), | ||
| request.getMethod(), | ||
| request.getPath(), | ||
| request.getRequestAddress()); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AppConfig 싱글톤 미보장:
WebServer에서LOADER = new AppConfig(),SecurityConfig,FilterConfig등으로 각각 새로운 인스턴스를 생성하고 있습니다. 동시에FilterConfig와SecurityConfig내부에서도new AppConfig()를 호출하므로 싱글톤이 보장되지 않습니다. PR 설명에서 "DI가 복잡하게 꼬인 느낌"이라는 피드백과 연결됩니다.\n\n수정안: 싱글톤 패턴을 명확히 하거나,SingletonContainer기반으로 통합 관리하도록 리팩토링하세요. 예:AppConfig를 정적 싱글톤으로 만들거나, 별도의 초기화 단계에서 통합 관리."