From 7afe9ada0249ca3e2a6cd7ef3deed7e99e0f45c6 Mon Sep 17 00:00:00 2001 From: wonseok2877 Date: Tue, 12 Sep 2023 22:21:43 +0900 Subject: [PATCH 1/6] =?UTF-8?q?chore:=20IO=20Stream=20=EC=A1=B0=EC=9E=91?= =?UTF-8?q?=20=EC=9C=A0=ED=8B=B8=20=EC=9D=98=EC=A1=B4=EC=84=B1=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 --- build.gradle | 3 +++ 1 file changed, 3 insertions(+) diff --git a/build.gradle b/build.gradle index a74543d..fcd1e64 100644 --- a/build.gradle +++ b/build.gradle @@ -70,6 +70,9 @@ dependencies { // redis implementation 'org.springframework.boot:spring-boot-starter-data-redis' + + // IOUtils + implementation 'commons-io:commons-io:2.7' } tasks.named('test') { From 0526a65d355bf37a9a7ccf788b12fca263ca08d8 Mon Sep 17 00:00:00 2001 From: wonseok2877 Date: Wed, 13 Sep 2023 20:22:09 +0900 Subject: [PATCH 2/6] =?UTF-8?q?feat:=20=EC=9A=94=EC=B2=AD=20API=20?= =?UTF-8?q?=EB=A1=9C=EA=B9=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bokjakserver/web/log/LoggerAspect.java | 51 +++++++ .../web/log/ReadableRequestWrapper.java | 143 ++++++++++++++++++ .../web/log/ReadableRequestWrapperFilter.java | 28 ++++ 3 files changed, 222 insertions(+) create mode 100644 src/main/java/bokjak/bokjakserver/web/log/LoggerAspect.java create mode 100644 src/main/java/bokjak/bokjakserver/web/log/ReadableRequestWrapper.java create mode 100644 src/main/java/bokjak/bokjakserver/web/log/ReadableRequestWrapperFilter.java diff --git a/src/main/java/bokjak/bokjakserver/web/log/LoggerAspect.java b/src/main/java/bokjak/bokjakserver/web/log/LoggerAspect.java new file mode 100644 index 0000000..42f6d4e --- /dev/null +++ b/src/main/java/bokjak/bokjakserver/web/log/LoggerAspect.java @@ -0,0 +1,51 @@ +package bokjak.bokjakserver.web.log; + +import jakarta.servlet.http.HttpServletRequest; +import lombok.extern.slf4j.Slf4j; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Pointcut; +import org.json.simple.JSONObject; +import org.springframework.stereotype.Component; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; + +import java.util.Enumeration; +import java.util.Objects; + +@Component +@Aspect +@Slf4j +public class LoggerAspect { + @Pointcut("execution(* bokjak.bokjakserver..*Controller.*(..)) || execution(* bokjak.bokjakserver..*GlobalExceptionHandler.*(..))") + // 이런 패턴이 실행될 경우 수행 + public void loggerPointCut() { + } + + @Around("loggerPointCut()") + public Object logRequestUri(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { + Object result = proceedingJoinPoint.proceed(); + ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); + + if (Objects.nonNull(requestAttributes)) { + HttpServletRequest request = requestAttributes.getRequest(); // request 정보를 가져온다. + String controllerName = proceedingJoinPoint.getSignature().getDeclaringType().getSimpleName(); + String methodName = proceedingJoinPoint.getSignature().getName(); + log.info("{}.{}: {} {} PARAM={}", controllerName, methodName, request.getMethod(), request.getRequestURI(), extractParams(request)); // param에 담긴 정보들을 한번에 로깅한다. + } + + return result; + } + + private static JSONObject extractParams(HttpServletRequest request) { // request로부터 param 추출, JSONObject로 변환 + JSONObject jsonObject = new JSONObject(); + Enumeration params = request.getParameterNames(); + while (params.hasMoreElements()) { + String param = params.nextElement(); + String replaceParam = param.replaceAll("\\.", "-"); + jsonObject.put(replaceParam, request.getParameter(param)); + } + return jsonObject; + } +} diff --git a/src/main/java/bokjak/bokjakserver/web/log/ReadableRequestWrapper.java b/src/main/java/bokjak/bokjakserver/web/log/ReadableRequestWrapper.java new file mode 100644 index 0000000..eb9d165 --- /dev/null +++ b/src/main/java/bokjak/bokjakserver/web/log/ReadableRequestWrapper.java @@ -0,0 +1,143 @@ +package bokjak.bokjakserver.web.log; + +import jakarta.servlet.ReadListener; +import jakarta.servlet.ServletInputStream; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletRequestWrapper; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.io.IOUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.http.entity.ContentType; +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; +import org.json.simple.parser.JSONParser; + +import java.io.BufferedReader; +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import java.util.Collections; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.Map; +import java.util.stream.Collectors; + +@Slf4j +public class ReadableRequestWrapper extends HttpServletRequestWrapper { + private final Charset encoding; + private byte[] rawData; + private final Map params = new HashMap<>(); + + public ReadableRequestWrapper(HttpServletRequest request) { + super(request); + this.params.putAll(request.getParameterMap()); // 오리지널 요청의 파라미터들 저장 + + String charEncoding = request.getCharacterEncoding(); // 인코딩 설정 + this.encoding = StringUtils.isBlank(charEncoding) ? StandardCharsets.UTF_8 : Charset.forName(charEncoding); + + try { + // 중요: body가 유실되지 않도록 함. getInputStream -> rawData에 저장 -> getReader() 에서 새 스트림으로 생성 + InputStream inputStream = request.getInputStream(); + this.rawData = IOUtils.toByteArray(inputStream); + + // body 파싱 + String collect = this.getReader().lines().collect(Collectors.joining(System.lineSeparator())); + if (StringUtils.isEmpty(collect)) { // body 가 없을경우 로깅 제외 TODO: 이 경우에도 로깅해야 하지 않나? + return; + } + if (request.getContentType() != null && request.getContentType().contains( + ContentType.MULTIPART_FORM_DATA.getMimeType())) { // 파일 업로드시 로깅 제외 TODO: 이 경우에도 로깅해야 하지 않나? + return; + } + + JSONParser jsonParser = new JSONParser(); + Object parse = jsonParser.parse(collect); + if (parse instanceof JSONArray) { + JSONArray jsonArray = (JSONArray) jsonParser.parse(collect); + setParameter("requestBody", jsonArray.toJSONString()); + } else { + JSONObject jsonObject = (JSONObject) jsonParser.parse(collect); + for (Object key : jsonObject.keySet()) { + setParameter(key.toString(), jsonObject.get(key.toString()).toString().replace("\"", "\\\"")); + } + } + } catch (Exception e) { + log.error("ReadableRequestWrapper init error", e); + } + } + + @Override + public String getParameter(String name) { + String[] paramArray = getParameterValues(name); + if (paramArray != null && paramArray.length > 0) { + return paramArray[0]; + } else { + return null; + } + } + + @Override + public Map getParameterMap() { + return Collections.unmodifiableMap(params); + } + + @Override + public Enumeration getParameterNames() { + return Collections.enumeration(params.keySet()); + } + + @Override + public String[] getParameterValues(String name) { + String[] result = null; + String[] dummyParamValue = params.get(name); + + if (dummyParamValue != null) { + result = new String[dummyParamValue.length]; + System.arraycopy(dummyParamValue, 0, result, 0, dummyParamValue.length); + } + return result; + } + + public void setParameter(String name, String value) { + String[] param = {value}; + setParameter(name, param); + } + + public void setParameter(String name, String[] values) { + params.put(name, values); + } + + @Override + public ServletInputStream getInputStream() { + final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(this.rawData); + + return new ServletInputStream() { + @Override + public boolean isFinished() { + return false; + } + + @Override + public boolean isReady() { + return false; + } + + @Override + public void setReadListener(ReadListener readListener) { + // Do nothing + } + + public int read() { + return byteArrayInputStream.read(); + } + }; + } + + @Override + public BufferedReader getReader() { + return new BufferedReader(new InputStreamReader(this.getInputStream(), this.encoding)); + } +} + diff --git a/src/main/java/bokjak/bokjakserver/web/log/ReadableRequestWrapperFilter.java b/src/main/java/bokjak/bokjakserver/web/log/ReadableRequestWrapperFilter.java new file mode 100644 index 0000000..1b47fad --- /dev/null +++ b/src/main/java/bokjak/bokjakserver/web/log/ReadableRequestWrapperFilter.java @@ -0,0 +1,28 @@ +package bokjak.bokjakserver.web.log; + +import jakarta.servlet.*; +import jakarta.servlet.annotation.WebFilter; +import jakarta.servlet.http.HttpServletRequest; + +import java.io.IOException; + +@WebFilter(urlPatterns = "/*") // 대상: 전체 URI +public class ReadableRequestWrapperFilter implements Filter { // TODO: 4xx 응답을 캐치 못한다? + + @Override + public void init(FilterConfig filterConfig) { + // Do nothing + } + + @Override + public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException { + ReadableRequestWrapper wrapper = new ReadableRequestWrapper((HttpServletRequest) request); + filterChain.doFilter(wrapper, response); // 필터 체인에 Wrapper 추가 + } + + @Override + public void destroy() { + // Do nothing + } + +} From 104248eea72c3155aebc691171dc5ddc5d0eafbd Mon Sep 17 00:00:00 2001 From: wonseok2877 Date: Wed, 13 Sep 2023 21:15:14 +0900 Subject: [PATCH 3/6] =?UTF-8?q?style:=20=EB=A9=94=EC=84=9C=EB=93=9C?= =?UTF-8?q?=EB=AA=85=20=EA=B5=AC=EC=B2=B4=EC=A0=81=EC=9C=BC=EB=A1=9C=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/exception/GlobalExceptionHandler.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/main/java/bokjak/bokjakserver/common/exception/GlobalExceptionHandler.java b/src/main/java/bokjak/bokjakserver/common/exception/GlobalExceptionHandler.java index 8821256..0daec6d 100644 --- a/src/main/java/bokjak/bokjakserver/common/exception/GlobalExceptionHandler.java +++ b/src/main/java/bokjak/bokjakserver/common/exception/GlobalExceptionHandler.java @@ -28,7 +28,7 @@ public class GlobalExceptionHandler { @ExceptionHandler(BuzException.class) - public ResponseEntity handle(BuzException ex) { + public ResponseEntity handleBuzException(BuzException ex) { log.warn("{}({}) - {}", ex.getClass().getSimpleName(), ex.statusCode.getStatusCode(), ex.getMessage()); return ResponseEntity .status(ex.statusCode.getHttpCode()) @@ -36,12 +36,11 @@ public ResponseEntity handle(BuzException ex) { } @ExceptionHandler(AuthException.class) //분리 이유: SignToken - public ResponseEntity handle(AuthException ex) { + public ResponseEntity handleAuthException(AuthException ex) { log.warn("{}({}) - {}", ex.getClass().getSimpleName(), ex.statusCode.getStatusCode(), ex.getMessage()); return ResponseEntity .status(ex.statusCode.getHttpCode()) .body(ApiResponse.error(ex.statusCode.getStatusCode(), ex.data, ex.statusCode.getMessage())); - } /** @@ -51,7 +50,7 @@ public ResponseEntity handle(AuthException ex) { // AWS S3 버킷 정책에 맞지 않는 요청 @ExceptionHandler(AwsS3Exception.class) @ResponseStatus(value = HttpStatus.BAD_REQUEST) - protected ApiResponse handleAwsS3Error(final AwsS3Exception ex) { + protected ApiResponse handleAwsS3Exception(final AwsS3Exception ex) { log.warn("{} - {}", ex.getClass().getSimpleName(), ex.getMessage()); return error(ex.getAwsS3ErrorCode().getStatusCode(), ex.getMessage()); } From be5b80d09bfed95aaeb010dadd76628393d428a0 Mon Sep 17 00:00:00 2001 From: wonseok2877 Date: Thu, 14 Sep 2023 22:47:11 +0900 Subject: [PATCH 4/6] =?UTF-8?q?fix:=20WebFilter=20=EC=A0=81=EC=9A=A9?= =?UTF-8?q?=EB=90=98=EB=8F=84=EB=A1=9D=20ServletComponentScan=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 --- .../java/bokjak/bokjakserver/BokjakserverApplication.java | 2 ++ ...eRequestWrapper.java => ReadableRequestBodyWrapper.java} | 6 +++--- ...perFilter.java => ReadableRequestBodyWrapperFilter.java} | 4 ++-- 3 files changed, 7 insertions(+), 5 deletions(-) rename src/main/java/bokjak/bokjakserver/web/log/{ReadableRequestWrapper.java => ReadableRequestBodyWrapper.java} (95%) rename src/main/java/bokjak/bokjakserver/web/log/{ReadableRequestWrapperFilter.java => ReadableRequestBodyWrapperFilter.java} (76%) diff --git a/src/main/java/bokjak/bokjakserver/BokjakserverApplication.java b/src/main/java/bokjak/bokjakserver/BokjakserverApplication.java index a28437e..bd3b7a4 100644 --- a/src/main/java/bokjak/bokjakserver/BokjakserverApplication.java +++ b/src/main/java/bokjak/bokjakserver/BokjakserverApplication.java @@ -2,6 +2,7 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.web.servlet.ServletComponentScan; import org.springframework.context.annotation.Bean; import org.springframework.data.jpa.repository.config.EnableJpaAuditing; import org.springframework.scheduling.annotation.EnableScheduling; @@ -12,6 +13,7 @@ @EnableJpaAuditing @EnableWebSecurity @EnableScheduling +@ServletComponentScan public class BokjakserverApplication { @Bean diff --git a/src/main/java/bokjak/bokjakserver/web/log/ReadableRequestWrapper.java b/src/main/java/bokjak/bokjakserver/web/log/ReadableRequestBodyWrapper.java similarity index 95% rename from src/main/java/bokjak/bokjakserver/web/log/ReadableRequestWrapper.java rename to src/main/java/bokjak/bokjakserver/web/log/ReadableRequestBodyWrapper.java index eb9d165..a6426c8 100644 --- a/src/main/java/bokjak/bokjakserver/web/log/ReadableRequestWrapper.java +++ b/src/main/java/bokjak/bokjakserver/web/log/ReadableRequestBodyWrapper.java @@ -25,12 +25,12 @@ import java.util.stream.Collectors; @Slf4j -public class ReadableRequestWrapper extends HttpServletRequestWrapper { +public class ReadableRequestBodyWrapper extends HttpServletRequestWrapper { private final Charset encoding; private byte[] rawData; private final Map params = new HashMap<>(); - public ReadableRequestWrapper(HttpServletRequest request) { + public ReadableRequestBodyWrapper(HttpServletRequest request) { super(request); this.params.putAll(request.getParameterMap()); // 오리지널 요청의 파라미터들 저장 @@ -44,7 +44,7 @@ public ReadableRequestWrapper(HttpServletRequest request) { // body 파싱 String collect = this.getReader().lines().collect(Collectors.joining(System.lineSeparator())); - if (StringUtils.isEmpty(collect)) { // body 가 없을경우 로깅 제외 TODO: 이 경우에도 로깅해야 하지 않나? + if (StringUtils.isEmpty(collect)) { // body 가 없을경우 종료 return; } if (request.getContentType() != null && request.getContentType().contains( diff --git a/src/main/java/bokjak/bokjakserver/web/log/ReadableRequestWrapperFilter.java b/src/main/java/bokjak/bokjakserver/web/log/ReadableRequestBodyWrapperFilter.java similarity index 76% rename from src/main/java/bokjak/bokjakserver/web/log/ReadableRequestWrapperFilter.java rename to src/main/java/bokjak/bokjakserver/web/log/ReadableRequestBodyWrapperFilter.java index 1b47fad..86fc0b2 100644 --- a/src/main/java/bokjak/bokjakserver/web/log/ReadableRequestWrapperFilter.java +++ b/src/main/java/bokjak/bokjakserver/web/log/ReadableRequestBodyWrapperFilter.java @@ -7,7 +7,7 @@ import java.io.IOException; @WebFilter(urlPatterns = "/*") // 대상: 전체 URI -public class ReadableRequestWrapperFilter implements Filter { // TODO: 4xx 응답을 캐치 못한다? +public class ReadableRequestBodyWrapperFilter implements Filter { @Override public void init(FilterConfig filterConfig) { @@ -16,7 +16,7 @@ public void init(FilterConfig filterConfig) { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException { - ReadableRequestWrapper wrapper = new ReadableRequestWrapper((HttpServletRequest) request); + ReadableRequestBodyWrapper wrapper = new ReadableRequestBodyWrapper((HttpServletRequest) request); filterChain.doFilter(wrapper, response); // 필터 체인에 Wrapper 추가 } From e55a7582e7f7796b470183bac115848b094a724e Mon Sep 17 00:00:00 2001 From: wonseok2877 Date: Thu, 14 Sep 2023 22:51:23 +0900 Subject: [PATCH 5/6] =?UTF-8?q?style:=20println=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/bokjak/bokjakserver/domain/test/TestController.java | 4 ---- .../bokjak/bokjakserver/util/CustomSliceExecutionUtils.java | 5 ----- 2 files changed, 9 deletions(-) diff --git a/src/main/java/bokjak/bokjakserver/domain/test/TestController.java b/src/main/java/bokjak/bokjakserver/domain/test/TestController.java index b0e866c..8286a5c 100644 --- a/src/main/java/bokjak/bokjakserver/domain/test/TestController.java +++ b/src/main/java/bokjak/bokjakserver/domain/test/TestController.java @@ -40,12 +40,8 @@ public ApiResponse testCreateNotificat @GetMapping("/send/email") public String testSendEmailMySelf() { - long beforeTime = System.currentTimeMillis(); User currentUser = userService.getCurrentUser(); sleepingUserService.sendMail(currentUser.getEmail()); - long afterTime = System.currentTimeMillis(); - long diffTime = afterTime-beforeTime; - System.out.println("실행 시간: " + diffTime); return "good"; } } diff --git a/src/main/java/bokjak/bokjakserver/util/CustomSliceExecutionUtils.java b/src/main/java/bokjak/bokjakserver/util/CustomSliceExecutionUtils.java index 595fc49..8b6ea93 100644 --- a/src/main/java/bokjak/bokjakserver/util/CustomSliceExecutionUtils.java +++ b/src/main/java/bokjak/bokjakserver/util/CustomSliceExecutionUtils.java @@ -10,16 +10,11 @@ public class CustomSliceExecutionUtils { public static Slice getSlice(List content, Pageable pageable) { boolean hasNext = false; - System.out.println("content.size() = " + content.size()); - System.out.println("pageable.getPageSize() = " + pageable.getPageSize()); - if (content.size() > pageable.getPageSize()) { // content.size가 최대일 경우: 항상 page size + 1 이고 다음 레코드가 있다. content.remove(pageable.getPageSize()); // limit걸 때 +1 했던 마지막 레코드를 삭제 hasNext = true; } - System.out.println("hasNext = " + hasNext); - return new SliceImpl<>(content, pageable, hasNext); } From 81f6677602e9ff2e1c0d4b5e718307d85e4a2971 Mon Sep 17 00:00:00 2001 From: wonseok2877 Date: Fri, 15 Sep 2023 19:33:48 +0900 Subject: [PATCH 6/6] =?UTF-8?q?style:=20Filter,=20SerurityConfig=20?= =?UTF-8?q?=EC=BD=94=EB=93=9C=20=EC=A0=95=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/constant/GlobalConstants.java | 11 +++++---- .../config/security/SecurityConfig.java | 23 +++++++++--------- .../web/log/ReadableRequestBodyWrapper.java | 2 +- .../log/ReadableRequestBodyWrapperFilter.java | 24 +++++++------------ 4 files changed, 27 insertions(+), 33 deletions(-) diff --git a/src/main/java/bokjak/bokjakserver/common/constant/GlobalConstants.java b/src/main/java/bokjak/bokjakserver/common/constant/GlobalConstants.java index ce10dcc..bac37f4 100644 --- a/src/main/java/bokjak/bokjakserver/common/constant/GlobalConstants.java +++ b/src/main/java/bokjak/bokjakserver/common/constant/GlobalConstants.java @@ -4,7 +4,11 @@ public class GlobalConstants { /** * Common */ - public static final String[] APPOINTED_URIS = { + public static final String[] AUTH_WHITELIST = { + "/", + "/csrf", + "/error", + "/auth/login/admin", "/auth/reissue", "/auth/login", @@ -26,10 +30,7 @@ public class GlobalConstants { "/swagger-ui/#", "/webjars/**", "/swagger/**", - "/swagger-ui/**", - "/", - "/csrf", - "/error" + "/swagger-ui/**" }; /** diff --git a/src/main/java/bokjak/bokjakserver/config/security/SecurityConfig.java b/src/main/java/bokjak/bokjakserver/config/security/SecurityConfig.java index 8330944..ea3b4dd 100644 --- a/src/main/java/bokjak/bokjakserver/config/security/SecurityConfig.java +++ b/src/main/java/bokjak/bokjakserver/config/security/SecurityConfig.java @@ -43,7 +43,10 @@ public PasswordEncoder passwordEncoder() { @Bean public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { - http.csrf().disable() + http + .csrf().disable() + .formLogin().disable() + .httpBasic().disable() .cors().configurationSource(corsConfigurationSource()) .and() @@ -55,23 +58,21 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { //세션 사용 안함 .sessionManagement() .sessionCreationPolicy(SessionCreationPolicy.STATELESS) - .and() - .formLogin().disable() - .httpBasic().disable() + .authorizeHttpRequests() - .requestMatchers(GlobalConstants.APPOINTED_URIS).permitAll() - .anyRequest().authenticated(); + .requestMatchers(GlobalConstants.AUTH_WHITELIST).permitAll() + .anyRequest().authenticated() - http.addFilterBefore(jwtAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class); + .and() + .addFilterBefore(jwtAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class); return http.build(); } @Bean public JwtAuthenticationFilter jwtAuthenticationFilter() { - JwtAuthenticationFilter jwtAuthenticationFilter = new JwtAuthenticationFilter(jwtProvider); - return jwtAuthenticationFilter; + return new JwtAuthenticationFilter(jwtProvider); } @Bean @@ -79,13 +80,13 @@ public CorsConfigurationSource corsConfigurationSource() { //다시 알아 CorsConfiguration configuration = new CorsConfiguration(); configuration.setAllowCredentials(true); - configuration.setAllowedOrigins(Arrays.asList("*")); + configuration.setAllowedOrigins(List.of("*")); configuration.setAllowedMethods( Arrays.asList(HttpMethod.POST.name(), HttpMethod.GET.name(), HttpMethod.PUT.name(), HttpMethod.DELETE.name(), HttpMethod.OPTIONS.name()) ); - configuration.setAllowedHeaders(Arrays.asList("*")); + configuration.setAllowedHeaders(List.of("*")); UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); source.registerCorsConfiguration("/**", configuration); diff --git a/src/main/java/bokjak/bokjakserver/web/log/ReadableRequestBodyWrapper.java b/src/main/java/bokjak/bokjakserver/web/log/ReadableRequestBodyWrapper.java index a6426c8..8ddbb51 100644 --- a/src/main/java/bokjak/bokjakserver/web/log/ReadableRequestBodyWrapper.java +++ b/src/main/java/bokjak/bokjakserver/web/log/ReadableRequestBodyWrapper.java @@ -48,7 +48,7 @@ public ReadableRequestBodyWrapper(HttpServletRequest request) { return; } if (request.getContentType() != null && request.getContentType().contains( - ContentType.MULTIPART_FORM_DATA.getMimeType())) { // 파일 업로드시 로깅 제외 TODO: 이 경우에도 로깅해야 하지 않나? + ContentType.MULTIPART_FORM_DATA.getMimeType())) { // 파일 업로드시 로깅 제외 return; } diff --git a/src/main/java/bokjak/bokjakserver/web/log/ReadableRequestBodyWrapperFilter.java b/src/main/java/bokjak/bokjakserver/web/log/ReadableRequestBodyWrapperFilter.java index 86fc0b2..48b24a8 100644 --- a/src/main/java/bokjak/bokjakserver/web/log/ReadableRequestBodyWrapperFilter.java +++ b/src/main/java/bokjak/bokjakserver/web/log/ReadableRequestBodyWrapperFilter.java @@ -1,28 +1,20 @@ package bokjak.bokjakserver.web.log; -import jakarta.servlet.*; +import jakarta.servlet.FilterChain; +import jakarta.servlet.ServletException; import jakarta.servlet.annotation.WebFilter; import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import org.jetbrains.annotations.NotNull; +import org.springframework.web.filter.OncePerRequestFilter; import java.io.IOException; @WebFilter(urlPatterns = "/*") // 대상: 전체 URI -public class ReadableRequestBodyWrapperFilter implements Filter { - - @Override - public void init(FilterConfig filterConfig) { - // Do nothing - } - - @Override - public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException { - ReadableRequestBodyWrapper wrapper = new ReadableRequestBodyWrapper((HttpServletRequest) request); - filterChain.doFilter(wrapper, response); // 필터 체인에 Wrapper 추가 - } - +public class ReadableRequestBodyWrapperFilter extends OncePerRequestFilter { @Override - public void destroy() { - // Do nothing + protected void doFilterInternal(@NotNull HttpServletRequest request, @NotNull HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { + filterChain.doFilter(new ReadableRequestBodyWrapper(request), response); // 필터 체인에 커스텀 Wrapper 추가 } }