http encryption : response encrypt wrapper

This commit is contained in:
haerong22
2021-09-02 14:19:34 +09:00
parent abf70e98f9
commit ce3e4a91a5
7 changed files with 84 additions and 11 deletions

View File

@@ -19,7 +19,10 @@ public class HttpEncryptionFilter implements Filter {
HttpServletResponse httpServletResponse = (HttpServletResponse) response;
RequestDecryptWrapper requestDecryptWrapper = new RequestDecryptWrapper(httpServletRequest);
ResponseEncryptWrapper responseEncryptWrapper = new ResponseEncryptWrapper(httpServletResponse);
chain.doFilter(requestDecryptWrapper, response);
chain.doFilter(requestDecryptWrapper, responseEncryptWrapper);
httpServletResponse.getOutputStream().write(responseEncryptWrapper.encryptResponse());
}
}

View File

@@ -1,11 +1,46 @@
package com.example.httpencryption.common;
import com.example.httpencryption.utils.AESUtil;
import javax.servlet.ServletOutputStream;
import javax.servlet.WriteListener;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
public class ResponseEncryptWrapper extends HttpServletResponseWrapper {
private final ByteArrayOutputStream output;
public ResponseEncryptWrapper(HttpServletResponse response) {
super(response);
output = new ByteArrayOutputStream();
}
@Override
public ServletOutputStream getOutputStream() throws IOException {
return new ServletOutputStream() {
@Override
public boolean isReady() {
return false;
}
@Override
public void setWriteListener(WriteListener listener) {
}
@Override
public void write(int b) throws IOException {
output.write(b);
}
};
}
public byte[] encryptResponse() {
String responseMessage = new String(output.toByteArray(), StandardCharsets.UTF_8);
AESUtil aesUtil = new AESUtil();
return aesUtil.encrypt(responseMessage).getBytes(StandardCharsets.UTF_8);
}
}

View File

@@ -1,7 +1,9 @@
package com.example.httpencryption.controller;
import com.example.httpencryption.dto.TestDto;
import org.apache.commons.io.IOUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.ServletInputStream;
@@ -12,13 +14,14 @@ import java.nio.charset.StandardCharsets;
@RestController
public class TestController {
@GetMapping("/")
public String hello(HttpServletRequest request) throws IOException {
@PostMapping("/")
public TestDto hello(HttpServletRequest request, @RequestBody TestDto dto) throws IOException {
ServletInputStream inputStream = request.getInputStream();
byte[] bytes = IOUtils.toByteArray(inputStream);
String s = new String(bytes, StandardCharsets.UTF_8);
System.out.println("s = " + s);
return s;
return dto;
}
}

View File

@@ -0,0 +1,14 @@
package com.example.httpencryption.dto;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class TestDto {
private String username;
private int age;
}

View File

@@ -27,7 +27,6 @@ public class AESUtil {
byte[] result = new byte[iv.length + encryptData.length];
System.arraycopy(iv, 0, result, 0, iv.length);
System.arraycopy(encryptData, 0, result, iv.length, encryptData.length);
return Base64Utils.encodeToString(result);
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | InvalidAlgorithmParameterException | IllegalBlockSizeException | BadPaddingException e) {
throw new RuntimeException("encrypt fail : " + e.getMessage());

View File

@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
</body>
</html>

View File

@@ -1,5 +1,8 @@
package com.example.httpencryption.utils;
import com.example.httpencryption.dto.TestDto;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -9,13 +12,19 @@ class AESUtilTest {
AESUtil util = new AESUtil();
@Test
void encryptTest() {
String hello = util.encrypt("hello");
void encryptTest() throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
System.out.println("hello = " + hello);
TestDto testDto = new TestDto("kim", 20);
String decrypt = util.decrypt(hello);
String s = objectMapper.writeValueAsString(testDto);
assertEquals("hello", decrypt);
String test = util.encrypt(s);
System.out.println("hello = " + test);
String decrypt = util.decrypt(test);
assertEquals("{\"username\":\"kim\",\"age\":20}", decrypt);
}
}