file test wrapper 수정
This commit is contained in:
parent
15e64f65b3
commit
60c184d743
@ -153,8 +153,6 @@ public class CustomRememberMeServices implements RememberMeServices {
|
|||||||
return expectedSignature.equals(signature);
|
return expectedSignature.equals(signature);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 현재는 id만 담는디
|
|
||||||
// 만약 바꾼다면 findById로 정보체크 하는데 idx + id 로 체크 하는 거 만들어야 할 듯
|
|
||||||
private String generateSignature(String username, long expiryTime) {
|
private String generateSignature(String username, long expiryTime) {
|
||||||
try {
|
try {
|
||||||
String data = username + DELIMITER + expiryTime;
|
String data = username + DELIMITER + expiryTime;
|
||||||
|
|||||||
@ -16,7 +16,6 @@ package io.company.localhost.common.wrapper;
|
|||||||
|
|
||||||
import com.fasterxml.jackson.core.type.TypeReference;
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
import io.company.localhost.utils.JacksonUtil;
|
import io.company.localhost.utils.JacksonUtil;
|
||||||
import io.netty.handler.codec.ValueConverter;
|
|
||||||
import jakarta.servlet.ReadListener;
|
import jakarta.servlet.ReadListener;
|
||||||
import jakarta.servlet.ServletInputStream;
|
import jakarta.servlet.ServletInputStream;
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
@ -24,6 +23,8 @@ import jakarta.servlet.http.HttpServletRequestWrapper;
|
|||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.util.StreamUtils;
|
import org.springframework.util.StreamUtils;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
import org.springframework.web.multipart.MultipartHttpServletRequest;
|
||||||
|
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
@ -41,11 +42,11 @@ public class CachedBodyRequestWrapper extends HttpServletRequestWrapper {
|
|||||||
|
|
||||||
private final byte[] cachedBody;
|
private final byte[] cachedBody;
|
||||||
|
|
||||||
private ValueConverter targetConvert;
|
|
||||||
|
|
||||||
public CachedBodyRequestWrapper(HttpServletRequest request) throws IOException {
|
public CachedBodyRequestWrapper(HttpServletRequest request) throws IOException {
|
||||||
super(request);
|
super(request);
|
||||||
|
|
||||||
|
String contentType = request.getContentType();
|
||||||
|
|
||||||
String characterEncoding = request.getCharacterEncoding();
|
String characterEncoding = request.getCharacterEncoding();
|
||||||
if (!StringUtils.hasText(characterEncoding)) {
|
if (!StringUtils.hasText(characterEncoding)) {
|
||||||
characterEncoding = StandardCharsets.UTF_8.name();
|
characterEncoding = StandardCharsets.UTF_8.name();
|
||||||
@ -56,39 +57,54 @@ public class CachedBodyRequestWrapper extends HttpServletRequestWrapper {
|
|||||||
// this.cachedBody = StreamUtils.copyToByteArray(requestInputStream);
|
// this.cachedBody = StreamUtils.copyToByteArray(requestInputStream);
|
||||||
this.encoding = Charset.forName(characterEncoding);
|
this.encoding = Charset.forName(characterEncoding);
|
||||||
|
|
||||||
// XSS 방지
|
// multipart/form 요청인지 확인
|
||||||
String requestBody = null;
|
if (contentType != null && contentType.toLowerCase().startsWith("multipart/")) {
|
||||||
try {
|
this.cachedBody = handleMultipartRequest(request);
|
||||||
requestBody = StreamUtils.copyToString(request.getInputStream(), Charset.defaultCharset());
|
} else {
|
||||||
} catch (IOException e) {
|
this.cachedBody = handleStandardRequestBody(request);
|
||||||
log.error("StreamUtil.toString Exception", e);
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private byte[] handleMultipartRequest(HttpServletRequest request) throws IOException {
|
||||||
|
// 멀티파트 요청 처리
|
||||||
|
if (request instanceof MultipartHttpServletRequest) {
|
||||||
|
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
|
||||||
|
multipartRequest.getFileMap().forEach((name, file) -> {
|
||||||
|
try {
|
||||||
|
String fileName = file.getOriginalFilename();
|
||||||
|
byte[] fileBytes = file.getBytes();
|
||||||
|
// 파일 내용을 필요에 맞게 처리 (저장, 분석 등)
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException("파일 내용을 읽을 수 없습니다.", e);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
// 필요시 form 필드 처리도 추가 가능
|
||||||
|
} else {
|
||||||
|
request = new StandardMultipartHttpServletRequest(request);
|
||||||
|
}
|
||||||
|
// 파일 처리 후 필요시 수정
|
||||||
|
return new byte[0];
|
||||||
|
}
|
||||||
|
|
||||||
if (requestBody == null) {
|
private byte[] handleStandardRequestBody(HttpServletRequest request) throws IOException {
|
||||||
InputStream requestInputStream = request.getInputStream();
|
String requestBody;
|
||||||
this.cachedBody = StreamUtils.copyToByteArray(requestInputStream);
|
try {
|
||||||
return;
|
requestBody = StreamUtils.copyToString(request.getInputStream(), encoding);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new IOException("요청 본문을 읽을 수 없습니다.", e);
|
||||||
}
|
}
|
||||||
|
|
||||||
Object bodyObject;
|
Object bodyObject;
|
||||||
if (requestBody.startsWith("[")) { // List
|
if (requestBody.startsWith("[")) {
|
||||||
bodyObject = JacksonUtil.fromJson(requestBody, new TypeReference<List<Map<String, Object>>>() {
|
bodyObject = JacksonUtil.fromJson(requestBody, new TypeReference<List<Map<String, Object>>>() {});
|
||||||
});
|
} else {
|
||||||
} else { // Map
|
bodyObject = JacksonUtil.fromJson(requestBody, new TypeReference<Map<String, Object>>() {});
|
||||||
bodyObject = JacksonUtil.fromJson(requestBody, new TypeReference<Map<String, Object>>() {
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
String newRequestBody = JacksonUtil.toJson(bodyObject);
|
String newRequestBody = JacksonUtil.toJson(bodyObject);
|
||||||
if (Objects.isNull(newRequestBody)) {
|
return Objects.requireNonNullElse(newRequestBody, "").getBytes(StandardCharsets.UTF_8);
|
||||||
newRequestBody = new String("");
|
|
||||||
}
|
|
||||||
this.cachedBody = newRequestBody.getBytes(StandardCharsets.UTF_8);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** json body 관련 메소드 START */
|
|
||||||
@Override
|
@Override
|
||||||
public ServletInputStream getInputStream() throws IOException {
|
public ServletInputStream getInputStream() throws IOException {
|
||||||
return new CachedBodyServletInputStream(this.cachedBody);
|
return new CachedBodyServletInputStream(this.cachedBody);
|
||||||
@ -100,8 +116,6 @@ public class CachedBodyRequestWrapper extends HttpServletRequestWrapper {
|
|||||||
return new BufferedReader(new InputStreamReader(byteArrayInputStream, encoding));
|
return new BufferedReader(new InputStreamReader(byteArrayInputStream, encoding));
|
||||||
}
|
}
|
||||||
|
|
||||||
/** json body 관련 메소드 END */
|
|
||||||
|
|
||||||
private static class CachedBodyServletInputStream extends ServletInputStream {
|
private static class CachedBodyServletInputStream extends ServletInputStream {
|
||||||
|
|
||||||
private final InputStream cachedBodyInputStream;
|
private final InputStream cachedBodyInputStream;
|
||||||
@ -115,8 +129,7 @@ public class CachedBodyRequestWrapper extends HttpServletRequestWrapper {
|
|||||||
try {
|
try {
|
||||||
return this.cachedBodyInputStream.available() == 0;
|
return this.cachedBodyInputStream.available() == 0;
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
log.error("", e);
|
return true;
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -136,5 +149,6 @@ public class CachedBodyRequestWrapper extends HttpServletRequestWrapper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -23,6 +23,7 @@ import io.company.localhost.service.TestService;
|
|||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@RequestMapping("/api/test/")
|
@RequestMapping("/api/test/")
|
||||||
@ -75,4 +76,10 @@ public class TestController {
|
|||||||
return ApiResponse.ok("OK");
|
return ApiResponse.ok("OK");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ParameterCheck
|
||||||
|
@PostMapping("fileTest")
|
||||||
|
public ApiResponse<String> fileTest(@RequestParam("file") MultipartFile file,
|
||||||
|
@ReqMap MapDto map) {
|
||||||
|
return ApiResponse.ok("OK");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user