ㅇㅇ
This commit is contained in:
@@ -1,19 +1,17 @@
|
|||||||
package com.madeu.config;
|
package com.madeu.config;
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.core.io.Resource;
|
import org.springframework.core.io.Resource;
|
||||||
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
||||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||||
import org.springframework.web.servlet.resource.PathResourceResolver;
|
import org.springframework.web.servlet.resource.PathResourceResolver;
|
||||||
import org.springframework.web.util.UriUtils;
|
|
||||||
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Configuration
|
@Configuration
|
||||||
public class WebConfig implements WebMvcConfigurer {
|
public class WebConfig implements WebMvcConfigurer {
|
||||||
@@ -24,89 +22,97 @@ public class WebConfig implements WebMvcConfigurer {
|
|||||||
@Override
|
@Override
|
||||||
public void addResourceHandlers(ResourceHandlerRegistry registry) {
|
public void addResourceHandlers(ResourceHandlerRegistry registry) {
|
||||||
log.info("=== WebConfig 초기화 시작 ===");
|
log.info("=== WebConfig 초기화 시작 ===");
|
||||||
log.info("Upload Path: {}", uploadPath);
|
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/**")
|
registry.addResourceHandler("/cdn/**")
|
||||||
.addResourceLocations("file:" + uploadPath + "/")
|
.addResourceLocations("file:" + uploadPath + "/")
|
||||||
.setCachePeriod(3600) // 1시간 캐싱
|
.setCachePeriod(3600)
|
||||||
.resourceChain(true)
|
.resourceChain(true)
|
||||||
.addResolver(new PathResourceResolver() {
|
.addResolver(new PathResourceResolver() {
|
||||||
@Override
|
@Override
|
||||||
protected Resource getResource(String resourcePath, Resource location) throws IOException {
|
protected Resource getResource(String resourcePath, Resource location) throws IOException {
|
||||||
log.info("=== Resource 요청 받음 ===");
|
log.info("========================================");
|
||||||
log.info("원본 Resource Path (인코딩됨): {}", resourcePath);
|
log.info("요청 Resource Path: {}", resourcePath);
|
||||||
|
log.info("Base Location URI: {}", location.getURI());
|
||||||
|
|
||||||
// URL 디코딩 처리 (한글 파일명 지원)
|
Resource requestedResource = location.createRelative(resourcePath);
|
||||||
String decodedResourcePath = UriUtils.decode(resourcePath, StandardCharsets.UTF_8);
|
|
||||||
log.info("디코딩된 Resource Path: {}", decodedResourcePath);
|
|
||||||
log.info("Location: {}", location.getURI());
|
|
||||||
|
|
||||||
|
// 핵심: 실제 조합된 전체 경로 확인
|
||||||
|
|
||||||
Resource requestedResource = location.createRelative(decodedResourcePath);
|
|
||||||
// 상세 파일 정보 로깅
|
|
||||||
try {
|
try {
|
||||||
File file = requestedResource.getFile();
|
File file = requestedResource.getFile();
|
||||||
log.info("실제 파일 경로: {}", file.getAbsolutePath());
|
log.info(">>> 조합된 전체 파일 경로: {}", file.getAbsolutePath());
|
||||||
log.info("파일 존재 여부: {}", file.exists());
|
log.info(">>> 파일 존재 여부: {}", file.exists());
|
||||||
log.info("파일 읽기 권한: {}", file.canRead());
|
|
||||||
log.info("파일 크기: {} bytes", file.length());
|
if (!file.exists()) {
|
||||||
log.info("파일인지 여부: {}", file.isFile());
|
// 파일이 없을 때 부모 디렉토리 확인
|
||||||
log.info("디렉토리인지 여부: {}", file.isDirectory());
|
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) {
|
} catch (Exception e) {
|
||||||
log.error("파일 정보 조회 실패", e);
|
log.error(">>> 파일 정보 조회 실패", e);
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean exists = requestedResource.exists();
|
boolean exists = requestedResource.exists();
|
||||||
boolean readable = exists && requestedResource.isReadable();
|
boolean readable = exists && requestedResource.isReadable();
|
||||||
|
|
||||||
log.info("Requested Resource URI: {}", requestedResource.getURI());
|
log.info("Resource exists: {}", exists);
|
||||||
log.info("Requested Resource exists: {}", exists);
|
log.info("Resource readable: {}", readable);
|
||||||
log.info("Requested Resource readable: {}", readable);
|
|
||||||
|
|
||||||
// 보안 검증: 허용된 파일 타입만
|
|
||||||
if (exists && readable) {
|
if (exists && readable) {
|
||||||
boolean allowed = isAllowedResource(requestedResource);
|
boolean allowed = isAllowedResource(requestedResource);
|
||||||
log.info("Resource allowed: {}", allowed);
|
|
||||||
|
|
||||||
if (allowed) {
|
if (allowed) {
|
||||||
log.info("✓ 파일 반환 성공: {}", decodedResourcePath);
|
log.info("✓✓✓ 파일 반환 성공 ✓✓✓");
|
||||||
return requestedResource;
|
return requestedResource;
|
||||||
} else {
|
} else {
|
||||||
log.info("✗ 허용되지 않은 파일 타입: {}", decodedResourcePath);
|
log.info("✗ 허용되지 않은 파일 타입");
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
log.info("✗ 파일 없음 또는 읽을 수 없음: {}", decodedResourcePath);
|
log.info("✗✗✗ 파일 없음 또는 읽을 수 없음 ✗✗✗");
|
||||||
log.info(" - exists: {}, readable: {}", exists, readable);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.info("========================================");
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isAllowedResource(Resource resource) {
|
private boolean isAllowedResource(Resource resource) {
|
||||||
try {
|
try {
|
||||||
String filename = resource.getFilename();
|
String filename = resource.getFilename();
|
||||||
log.info("Checking filename: {}", filename);
|
|
||||||
|
|
||||||
if (filename == null) {
|
if (filename == null) {
|
||||||
log.info("파일명이 null입니다");
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
String lowerFilename = filename.toLowerCase();
|
String lowerFilename = filename.toLowerCase();
|
||||||
boolean isAllowed = lowerFilename.endsWith(".jpg") ||
|
return lowerFilename.endsWith(".jpg") ||
|
||||||
lowerFilename.endsWith(".jpeg") ||
|
lowerFilename.endsWith(".jpeg") ||
|
||||||
lowerFilename.endsWith(".png") ||
|
lowerFilename.endsWith(".png") ||
|
||||||
lowerFilename.endsWith(".gif") ||
|
lowerFilename.endsWith(".gif") ||
|
||||||
lowerFilename.endsWith(".webp") ||
|
lowerFilename.endsWith(".webp") ||
|
||||||
lowerFilename.endsWith(".bmp") ||
|
lowerFilename.endsWith(".bmp") ||
|
||||||
lowerFilename.endsWith(".svg");
|
lowerFilename.endsWith(".svg");
|
||||||
|
|
||||||
log.info("파일 타입 검증 결과 - 파일명: {}, 허용: {}", filename, isAllowed);
|
|
||||||
return isAllowed;
|
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("파일 타입 검증 중 오류 발생", e);
|
log.error("파일 타입 검증 중 오류", e);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user