yona#37 파일 업로드 클라이언트, 서버단 사전 검증으로 적용
This commit is contained in:
+16
@@ -7,8 +7,11 @@ import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import com.eactive.apim.portal.config.PortalProperties;
|
||||
import com.eactive.apim.portal.file.exception.InvalidFileException;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.apache.commons.lang3.exception.ExceptionUtils;
|
||||
import org.springframework.web.multipart.MaxUploadSizeExceededException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.core.annotation.Order;
|
||||
@@ -28,9 +31,11 @@ import org.springframework.web.servlet.mvc.support.RedirectAttributes;
|
||||
*/
|
||||
@Order(2)
|
||||
@ControllerAdvice(annotations = Controller.class)
|
||||
@RequiredArgsConstructor
|
||||
public class PortalGlobalExceptionHandler {
|
||||
|
||||
private final Logger log = LoggerFactory.getLogger(getClass());
|
||||
private final PortalProperties portalProperties;
|
||||
|
||||
@ExceptionHandler(value = NotFoundException.class)
|
||||
public ModelAndView handleINotFoundException(HttpServletRequest request, NotFoundException ex) {
|
||||
@@ -111,4 +116,15 @@ public class PortalGlobalExceptionHandler {
|
||||
modelAndView.setViewName("redirect:" + request.getRequestURI());
|
||||
return modelAndView;
|
||||
}
|
||||
|
||||
@ExceptionHandler(value = MaxUploadSizeExceededException.class)
|
||||
public ModelAndView handleMaxUploadSizeExceededException(HttpServletRequest request, RedirectAttributes redirectAttributes, MaxUploadSizeExceededException ex) {
|
||||
log.warn("파일 업로드 용량 초과: {}", ex.getMessage());
|
||||
String maxSize = portalProperties.getFile().getMaxSize();
|
||||
String errorMessage = "파일 크기가 허용된 최대 용량(" + maxSize + ")을 초과했습니다.";
|
||||
redirectAttributes.addFlashAttribute("error", errorMessage);
|
||||
ModelAndView modelAndView = new ModelAndView();
|
||||
modelAndView.setViewName("redirect:" + request.getRequestURI());
|
||||
return modelAndView;
|
||||
}
|
||||
}
|
||||
|
||||
+17
@@ -2,20 +2,28 @@ package com.eactive.apim.portal.common.exception;
|
||||
|
||||
|
||||
import com.eactive.apim.portal.common.dto.ResponseDTO;
|
||||
import com.eactive.apim.portal.config.PortalProperties;
|
||||
import com.eactive.apim.portal.portaluser.service.AuthNumberException;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
import org.springframework.web.multipart.MaxUploadSizeExceededException;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
@Slf4j
|
||||
@Order(1)
|
||||
@RestControllerAdvice(annotations = RestController.class)
|
||||
@RequiredArgsConstructor
|
||||
public class PortalRestExceptionHandler {
|
||||
|
||||
private final PortalProperties portalProperties;
|
||||
|
||||
@ExceptionHandler(value = IntegrationException.class)
|
||||
public ResponseEntity<ResponseDTO> handleHttpRequestMethodNotSupportedException(HttpServletRequest request, IntegrationException ex) {
|
||||
|
||||
@@ -51,4 +59,13 @@ public class PortalRestExceptionHandler {
|
||||
ResponseDTO response = new ResponseDTO(500, "500", ex.getMessage());
|
||||
return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
|
||||
@ExceptionHandler(value = MaxUploadSizeExceededException.class)
|
||||
public ResponseEntity<ResponseDTO> handleMaxUploadSizeExceededException(HttpServletRequest request, MaxUploadSizeExceededException ex) {
|
||||
log.warn("파일 업로드 용량 초과 (REST): {}", ex.getMessage());
|
||||
String maxSize = portalProperties.getFile().getMaxSize();
|
||||
String errorMessage = "파일 크기가 허용된 최대 용량(" + maxSize + ")을 초과했습니다.";
|
||||
ResponseDTO response = new ResponseDTO(413, "413", errorMessage);
|
||||
return new ResponseEntity<>(response, HttpStatus.PAYLOAD_TOO_LARGE);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.eactive.apim.portal.config;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
|
||||
|
||||
/**
|
||||
* WAS(WebLogic, JEUS 등) 독립적인 Multipart 파일 업로드 설정.
|
||||
* <p>
|
||||
* Spring Boot 내장 Tomcat의 spring.servlet.multipart 설정은 외부 WAS에서 동작하지 않으므로,
|
||||
* CommonsMultipartResolver를 사용하여 모든 환경에서 일관된 파일 업로드 처리를 보장합니다.
|
||||
* </p>
|
||||
*/
|
||||
@Slf4j
|
||||
@Configuration
|
||||
@RequiredArgsConstructor
|
||||
public class MultipartConfig {
|
||||
|
||||
private final PortalProperties portalProperties;
|
||||
|
||||
/**
|
||||
* Commons FileUpload 기반 MultipartResolver 설정.
|
||||
* <p>
|
||||
* Bean 이름을 "filterMultipartResolver"로 지정하여
|
||||
* PortalConfigWebDispatcherServlet의 MultipartFilter와 연동합니다.
|
||||
* </p>
|
||||
*
|
||||
* @return CommonsMultipartResolver 인스턴스
|
||||
*/
|
||||
@Bean(name = "filterMultipartResolver")
|
||||
public CommonsMultipartResolver filterMultipartResolver() {
|
||||
CommonsMultipartResolver resolver = new CommonsMultipartResolver();
|
||||
|
||||
long maxSizeBytes = portalProperties.getFile().getMaxSizeBytes();
|
||||
resolver.setMaxUploadSize(maxSizeBytes);
|
||||
resolver.setMaxUploadSizePerFile(maxSizeBytes);
|
||||
resolver.setMaxInMemorySize((int) Math.min(maxSizeBytes, Integer.MAX_VALUE));
|
||||
resolver.setDefaultEncoding("UTF-8");
|
||||
|
||||
log.info("CommonsMultipartResolver configured with maxUploadSize: {} bytes ({})",
|
||||
maxSizeBytes, portalProperties.getFile().getMaxSize());
|
||||
|
||||
return resolver;
|
||||
}
|
||||
}
|
||||
@@ -26,4 +26,31 @@ public class PortalProperties {
|
||||
private String authVirtualCode = "";
|
||||
|
||||
private Map<RoleCode, List<String>> portalSecurity;
|
||||
|
||||
private FileProperties file = new FileProperties();
|
||||
|
||||
/**
|
||||
* 파일 업로드 관련 설정
|
||||
*/
|
||||
@Data
|
||||
public static class FileProperties {
|
||||
private String maxSize = "8MB";
|
||||
private String allowedExtensions = "pdf,doc,docx,xls,xlsx,ppt,pptx,hwp,gif,jpg,jpeg,png";
|
||||
|
||||
/**
|
||||
* maxSize 문자열을 bytes로 변환
|
||||
* "8MB" -> 8388608, "10MB" -> 10485760
|
||||
*/
|
||||
public long getMaxSizeBytes() {
|
||||
String size = maxSize.toUpperCase().trim();
|
||||
if (size.endsWith("MB")) {
|
||||
return Long.parseLong(size.replace("MB", "").trim()) * 1024 * 1024;
|
||||
} else if (size.endsWith("KB")) {
|
||||
return Long.parseLong(size.replace("KB", "").trim()) * 1024;
|
||||
} else if (size.endsWith("GB")) {
|
||||
return Long.parseLong(size.replace("GB", "").trim()) * 1024 * 1024 * 1024;
|
||||
}
|
||||
return Long.parseLong(size); // bytes로 간주
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user