원복
This commit is contained in:
@@ -7,12 +7,8 @@ import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
import org.springframework.web.servlet.resource.PathResourceResolver;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
@Slf4j
|
||||
@Configuration
|
||||
public class WebConfig implements WebMvcConfigurer {
|
||||
|
||||
@@ -21,103 +17,38 @@ public class WebConfig implements WebMvcConfigurer {
|
||||
|
||||
@Override
|
||||
public void addResourceHandlers(ResourceHandlerRegistry registry) {
|
||||
log.info("=== WebConfig 초기화 시작 ===");
|
||||
log.info("Upload Path 설정값: {}", uploadPath);
|
||||
|
||||
File uploadDir = new File(uploadPath);
|
||||
log.info("Upload 디렉토리 절대 경로: {}", uploadDir.getAbsolutePath());
|
||||
log.info("Upload 디렉토리 존재: {}", uploadDir.exists());
|
||||
log.info("Upload 디렉토리 읽기 권한: {}", uploadDir.canRead());
|
||||
|
||||
registry.addResourceHandler("/cdn/**")
|
||||
.addResourceLocations("file:" + uploadPath + "/")
|
||||
.setCachePeriod(3600)
|
||||
.setCachePeriod(3600) // 1시간 캐싱
|
||||
.resourceChain(true)
|
||||
.addResolver(new PathResourceResolver() {
|
||||
@Override
|
||||
protected Resource getResource(String resourcePath, Resource location) throws IOException {
|
||||
log.info("========================================");
|
||||
log.info("요청 Resource Path: {}", resourcePath);
|
||||
log.info("Base Location URI: {}", location.getURI());
|
||||
|
||||
Resource requestedResource = location.createRelative(resourcePath);
|
||||
|
||||
// 핵심: 실제 조합된 전체 경로 확인
|
||||
try {
|
||||
File file = requestedResource.getFile();
|
||||
log.info(">>> 조합된 전체 파일 경로: {}", file.getAbsolutePath());
|
||||
log.info(">>> 파일 존재 여부: {}", file.exists());
|
||||
|
||||
if (!file.exists()) {
|
||||
// 파일이 없을 때 부모 디렉토리 확인
|
||||
File parentDir = file.getParentFile();
|
||||
log.info(">>> 부모 디렉토리: {}", parentDir.getAbsolutePath());
|
||||
log.info(">>> 부모 디렉토리 존재: {}", parentDir.exists());
|
||||
|
||||
if (parentDir.exists()) {
|
||||
log.info(">>> 부모 디렉토리 내 파일 목록:");
|
||||
File[] files = parentDir.listFiles();
|
||||
if (files != null) {
|
||||
for (File f : files) {
|
||||
log.info(" - {}", f.getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
log.info(">>> 파일 읽기 권한: {}", file.canRead());
|
||||
log.info(">>> 파일 크기: {} bytes", file.length());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error(">>> 파일 정보 조회 실패", e);
|
||||
// 보안 검증: 허용된 파일 타입만
|
||||
if (requestedResource.exists() && requestedResource.isReadable()
|
||||
&& isAllowedResource(requestedResource)) {
|
||||
return requestedResource;
|
||||
}
|
||||
|
||||
boolean exists = requestedResource.exists();
|
||||
boolean readable = exists && requestedResource.isReadable();
|
||||
|
||||
log.info("Resource exists: {}", exists);
|
||||
log.info("Resource readable: {}", readable);
|
||||
|
||||
if (exists && readable) {
|
||||
boolean allowed = isAllowedResource(requestedResource);
|
||||
|
||||
if (allowed) {
|
||||
log.info("✓✓✓ 파일 반환 성공 ✓✓✓");
|
||||
return requestedResource;
|
||||
} else {
|
||||
log.info("✗ 허용되지 않은 파일 타입");
|
||||
}
|
||||
} else {
|
||||
log.info("✗✗✗ 파일 없음 또는 읽을 수 없음 ✗✗✗");
|
||||
}
|
||||
|
||||
log.info("========================================");
|
||||
return null;
|
||||
}
|
||||
|
||||
private boolean isAllowedResource(Resource resource) {
|
||||
try {
|
||||
String filename = resource.getFilename();
|
||||
|
||||
if (filename == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
String lowerFilename = filename.toLowerCase();
|
||||
return lowerFilename.endsWith(".jpg") ||
|
||||
lowerFilename.endsWith(".jpeg") ||
|
||||
lowerFilename.endsWith(".png") ||
|
||||
lowerFilename.endsWith(".gif") ||
|
||||
lowerFilename.endsWith(".webp") ||
|
||||
lowerFilename.endsWith(".bmp") ||
|
||||
lowerFilename.endsWith(".svg");
|
||||
|
||||
return filename != null &&
|
||||
(filename.toLowerCase().endsWith(".jpg") ||
|
||||
filename.toLowerCase().endsWith(".jpeg") ||
|
||||
filename.toLowerCase().endsWith(".png") ||
|
||||
filename.toLowerCase().endsWith(".gif") ||
|
||||
filename.toLowerCase().endsWith(".webp") ||
|
||||
filename.toLowerCase().endsWith(".bmp") ||
|
||||
filename.toLowerCase().endsWith(".svg"));
|
||||
} catch (Exception e) {
|
||||
log.error("파일 타입 검증 중 오류", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
log.info("=== WebConfig 설정 완료 ===");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user