From 12e0025ef6b0df1a018861df994b12139802e2cc Mon Sep 17 00:00:00 2001
From: chenfeng <694447355@qq.com>
Date: Mon, 26 Jun 2023 17:23:10 +0800
Subject: [PATCH] =?UTF-8?q?1.=20=E6=A1=86=E6=9E=B6=E4=BC=98=E5=8C=96=202.?=
=?UTF-8?q?=20=E6=8E=A5=E5=8F=A3=E5=93=8D=E5=BA=94=E6=95=B0=E6=8D=AE?=
=?UTF-8?q?=E5=A2=9E=E5=8A=A0AES=E5=8A=A0=E5=AF=86=E6=8B=A6=E6=88=AA?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
framework-core/pom.xml | 394 +++++++++++----------
.../java/com/unionmed/framework/crypto/AES.java | 2 +-
.../mvc/response/DecryptRequestBodyAdvice.java | 76 ++++
.../RequestResponseBodyCryptProperties.java | 30 ++
.../mvc/response/ResponseBodyConfiguration.java | 10 +-
.../EncryptionResponseBodyInterceptor.java | 26 +-
framework-orm/pom.xml | 2 +-
framework-test/pom.xml | 2 +-
pom.xml | 2 +-
9 files changed, 323 insertions(+), 221 deletions(-)
create mode 100644 framework-core/src/main/java/com/unionmed/framework/spring/mvc/response/DecryptRequestBodyAdvice.java
create mode 100644 framework-core/src/main/java/com/unionmed/framework/spring/mvc/response/RequestResponseBodyCryptProperties.java
diff --git a/framework-core/pom.xml b/framework-core/pom.xml
index 6d7e438..841c3bd 100644
--- a/framework-core/pom.xml
+++ b/framework-core/pom.xml
@@ -2,200 +2,206 @@
-
- com.unionmed
- unionmed-framework
- 0.0.11
-
-
- 4.0.0
+
com.unionmed
- framework-core
- framework-core
-
-
- UTF-8
- UTF-8
- 1.8
-
-
-
-
- cn.hutool
- hutool-all
-
-
-
-
- com.alibaba
- fastjson
-
-
-
-
- org.springframework.boot
- spring-boot-starter-data-jpa
-
-
- org.springframework.boot
- spring-boot-starter-web
-
-
+ unionmed-framework
+ 0.0.12
+
+
+ 4.0.0
+ com.unionmed
+ framework-core
+ framework-core
+
+
+ UTF-8
+ UTF-8
+ 1.8
+
+
+
+
+ cn.hutool
+ hutool-all
+
+
+
+
+ com.alibaba
+ fastjson
+
+
+
+
+ org.springframework.boot
+ spring-boot-starter-data-jpa
+
+
+ org.springframework.boot
+ spring-boot-starter-web
+
+
+ org.springframework.boot
+ spring-boot-devtools
+ runtime
+ true
+
+
+
+
+ io.minio
+ minio
+
+
+
+ org.projectlombok
+ lombok
+
+
+
+ io.github.yedaxia
+ japidocs
+
+
+ io.github.openfeign
+ feign-core
+
+
+
+
+ io.springfox
+ springfox-swagger2
+
+
+ io.springfox
+ springfox-spring-webmvc
+
+
+ io.springfox
+ springfox-swagger-ui
+
+
+ com.github.xiaoymin
+ knife4j-spring-boot-starter
+
+
+
+
+ com.github.isrsal
+ spring-mvc-logger
+
+
+ log4j
+ log4j
+
+
+ javax.servlet
+ servlet-api
+
+
+ org.slf4j
+ slf4j-log4j12
+
+
+
+
+
+
+ com.google.guava
+ guava
+
+
+ com.google.code.gson
+ gson
+
+
+
+ com
+ com.util
+
+
+ log4j
+ log4j
+
+
+ io.reactivex
+ rxnetty
+
+
+ com.mks.api
+ mksapi-jar
+
+
+
+
+ org.apache.httpcomponents
+ httpcore
+
+
+ org.apache.httpcomponents
+ httpclient
+
+
+ org.apache.httpcomponents
+ httpcore
+
+
+
+
+ org.apache.httpcomponents
+ httpasyncclient
+
+
+ org.apache.httpcomponents
+ httpmime
+
+
+
+
+ org.bouncycastle
+ bcprov-jdk18on
+
+
+
+ commons-io
+ commons-io
+ 2.6
+
+
+
+
+ ${project.name}-${project.parent.version}
+
+
-
- io.minio
- minio
-
-
-
- org.projectlombok
- lombok
-
-
-
- io.github.yedaxia
- japidocs
-
-
- io.github.openfeign
- feign-core
-
-
-
-
- io.springfox
- springfox-swagger2
-
-
- io.springfox
- springfox-spring-webmvc
-
-
- io.springfox
- springfox-swagger-ui
-
-
- com.github.xiaoymin
- knife4j-spring-boot-starter
-
-
-
-
- com.github.isrsal
- spring-mvc-logger
-
-
- log4j
- log4j
-
-
- javax.servlet
- servlet-api
-
-
- org.slf4j
- slf4j-log4j12
-
-
-
-
-
-
- com.google.guava
- guava
-
-
- com.google.code.gson
- gson
-
-
-
- com
- com.util
-
-
- log4j
- log4j
-
-
- io.reactivex
- rxnetty
-
-
- com.mks.api
- mksapi-jar
-
-
-
-
- org.apache.httpcomponents
- httpcore
-
-
- org.apache.httpcomponents
- httpclient
-
-
- org.apache.httpcomponents
- httpcore
-
-
-
-
- org.apache.httpcomponents
- httpasyncclient
-
-
- org.apache.httpcomponents
- httpmime
-
-
-
-
- org.bouncycastle
- bcprov-jdk18on
-
-
-
-
- ${project.name}-${project.parent.version}
-
-
-
-
-
-
- src/main/resources
- true
-
-
- src/main/java
-
- **/*.xml
-
- true
-
-
-
+ spring-boot-maven-plugin
+
+ true
+ com.unionmed.framework.springboot.Application
+
+
+ org.springframework.boot
+ spring-boot-configuration-processor
+
+
+
+
+ -->
+
+
+
+
+ src/main/resources
+ true
+
+
+ src/main/java
+
+ **/*.xml
+
+ true
+
+
+
\ No newline at end of file
diff --git a/framework-core/src/main/java/com/unionmed/framework/crypto/AES.java b/framework-core/src/main/java/com/unionmed/framework/crypto/AES.java
index eae80bd..9101b03 100644
--- a/framework-core/src/main/java/com/unionmed/framework/crypto/AES.java
+++ b/framework-core/src/main/java/com/unionmed/framework/crypto/AES.java
@@ -28,7 +28,7 @@ public class AES {
SecretKeySpec skSpec = new SecretKeySpec(toUtf8Bytes(sk), ALGORITHM);
Cipher cipher = Cipher.getInstance(PADDING);
cipher.init(Cipher.ENCRYPT_MODE, skSpec, ivSpec);
- return Base64.encodeBase64String(cipher.doFinal(value.getBytes()));
+ return Base64.encodeBase64String(cipher.doFinal(toUtf8Bytes(value)));
} catch (Exception ex) {
log.error("AES加密失败", ex);
return null;
diff --git a/framework-core/src/main/java/com/unionmed/framework/spring/mvc/response/DecryptRequestBodyAdvice.java b/framework-core/src/main/java/com/unionmed/framework/spring/mvc/response/DecryptRequestBodyAdvice.java
new file mode 100644
index 0000000..b6a6fd2
--- /dev/null
+++ b/framework-core/src/main/java/com/unionmed/framework/spring/mvc/response/DecryptRequestBodyAdvice.java
@@ -0,0 +1,76 @@
+package com.unionmed.framework.spring.mvc.response;
+
+import com.alibaba.fastjson.JSON;
+import com.unionmed.framework.crypto.AES;
+import com.unionmed.framework.http.HttpHeaders;
+import com.unionmed.framework.util.ObjectUtils;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.io.IOUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.core.MethodParameter;
+import org.springframework.http.HttpInputMessage;
+import org.springframework.http.converter.HttpMessageConverter;
+import org.springframework.web.bind.annotation.ControllerAdvice;
+import org.springframework.web.context.request.RequestContextHolder;
+import org.springframework.web.context.request.ServletRequestAttributes;
+import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdvice;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.lang.reflect.Type;
+
+/**
+ * @author ianChen
+ * @date 2023/6/26 14:02
+ */
+@Slf4j
+@ControllerAdvice
+public class DecryptRequestBodyAdvice implements RequestBodyAdvice {
+
+ @Autowired
+ private RequestResponseBodyCryptProperties requestResponseBodyCryptProperties;
+
+ @Override
+ public boolean supports(MethodParameter methodParameter, Type targetType, Class extends HttpMessageConverter>> converterType) {
+ return true;
+ }
+
+ @Override
+ public HttpInputMessage beforeBodyRead(final HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class extends HttpMessageConverter>> converterType) throws IOException {
+ String body = IOUtils.toString(inputMessage.getBody(), HttpHeaders.CHARSET_UTF8);
+ if (ObjectUtils.notEmpty(body) && requestResponseBodyCryptProperties.isEnabled() && ObjectUtils.equalsIgnore(inputMessage.getHeaders().getFirst(HttpHeaders.X_DATA_CRYPT_E), HttpHeaders.X_DATA_CRYPT_E_VALUE_TRUE)) {
+ body = AES.decrypt(requestResponseBodyCryptProperties.getSk(), body.substring(0, 16), body.substring(16));
+ }
+
+ if (log.isDebugEnabled()) {
+ ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
+ log.debug("URI: {}, Method: {}", servletRequestAttributes.getRequest().getRequestURI(), servletRequestAttributes.getRequest().getMethod());
+ log.debug("Headers: {}", JSON.toJSONString(inputMessage.getHeaders().toSingleValueMap()));
+ log.debug("RequestParameter: {}", servletRequestAttributes.getRequest().getParameterMap() == null ? "" : JSON.toJSONString(servletRequestAttributes.getRequest().getParameterMap()));
+ log.debug("RequestBody: {}", body);
+ }
+
+ InputStream is = IOUtils.toInputStream(body, HttpHeaders.CHARSET_UTF8);
+ return new HttpInputMessage() {
+ @Override
+ public InputStream getBody() throws IOException {
+ return is;
+ }
+
+ @Override
+ public org.springframework.http.HttpHeaders getHeaders() {
+ return inputMessage.getHeaders();
+ }
+ };
+ }
+
+ @Override
+ public Object afterBodyRead(Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class extends HttpMessageConverter>> converterType) {
+ return body;
+ }
+
+ @Override
+ public Object handleEmptyBody(Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class extends HttpMessageConverter>> converterType) {
+ return body;
+ }
+}
diff --git a/framework-core/src/main/java/com/unionmed/framework/spring/mvc/response/RequestResponseBodyCryptProperties.java b/framework-core/src/main/java/com/unionmed/framework/spring/mvc/response/RequestResponseBodyCryptProperties.java
new file mode 100644
index 0000000..917ed7e
--- /dev/null
+++ b/framework-core/src/main/java/com/unionmed/framework/spring/mvc/response/RequestResponseBodyCryptProperties.java
@@ -0,0 +1,30 @@
+package com.unionmed.framework.spring.mvc.response;
+
+import org.springframework.boot.context.properties.ConfigurationProperties;
+
+/**
+ * @author ianChen
+ * @date 2023/6/26 14:06
+ */
+@ConfigurationProperties(prefix = "unionmed.web-mvc.body.crypt.aes")
+public class RequestResponseBodyCryptProperties {
+
+ private boolean enabled = false;
+ private String sk;
+
+ public boolean isEnabled() {
+ return enabled;
+ }
+
+ public void setEnabled(boolean enabled) {
+ this.enabled = enabled;
+ }
+
+ public String getSk() {
+ return sk;
+ }
+
+ public void setSk(String sk) {
+ this.sk = sk;
+ }
+}
diff --git a/framework-core/src/main/java/com/unionmed/framework/spring/mvc/response/ResponseBodyConfiguration.java b/framework-core/src/main/java/com/unionmed/framework/spring/mvc/response/ResponseBodyConfiguration.java
index a0d6c40..93f999b 100644
--- a/framework-core/src/main/java/com/unionmed/framework/spring/mvc/response/ResponseBodyConfiguration.java
+++ b/framework-core/src/main/java/com/unionmed/framework/spring/mvc/response/ResponseBodyConfiguration.java
@@ -3,9 +3,9 @@ package com.unionmed.framework.spring.mvc.response;
import com.unionmed.framework.spring.mvc.response.datatemplate.DataTemplateResponseBodyInterceptor;
import com.unionmed.framework.spring.mvc.response.interceptor.EncryptionResponseBodyInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.context.annotation.Bean;
+import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
-import org.springframework.core.env.Environment;
+import org.springframework.context.annotation.ImportResource;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.servlet.mvc.method.annotation.HttpEntityMethodProcessor;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
@@ -18,18 +18,18 @@ import java.util.List;
* @author ianChen
* @date 2023/6/25 15:25
*/
+@EnableConfigurationProperties({RequestResponseBodyCryptProperties.class})
@Configuration
public class ResponseBodyConfiguration {
@Autowired
- private Environment environment;
+ private RequestResponseBodyCryptProperties requestResponseBodyCryptProperties;
@Autowired
private RequestMappingHandlerAdapter requestMappingHandlerAdapter;
-// @Bean
public ResponseBodyProcessor responseBodyProcessor() {
ResponseBodyProcessorChain chain = new ResponseBodyProcessorChain();
- chain.add(new DataTemplateResponseBodyInterceptor(), new EncryptionResponseBodyInterceptor(environment));
+ chain.add(new DataTemplateResponseBodyInterceptor(), new EncryptionResponseBodyInterceptor(requestResponseBodyCryptProperties));
return new ResponseBodyProcessor(requestMappingHandlerAdapter.getMessageConverters(), chain);//初始化过滤器
}
diff --git a/framework-core/src/main/java/com/unionmed/framework/spring/mvc/response/interceptor/EncryptionResponseBodyInterceptor.java b/framework-core/src/main/java/com/unionmed/framework/spring/mvc/response/interceptor/EncryptionResponseBodyInterceptor.java
index 1f19d54..07c17a2 100644
--- a/framework-core/src/main/java/com/unionmed/framework/spring/mvc/response/interceptor/EncryptionResponseBodyInterceptor.java
+++ b/framework-core/src/main/java/com/unionmed/framework/spring/mvc/response/interceptor/EncryptionResponseBodyInterceptor.java
@@ -7,10 +7,10 @@ import com.unionmed.framework.crypto.AES;
import com.unionmed.framework.http.HttpHeaders;
import com.unionmed.framework.spring.mvc.BaseReturn;
import com.unionmed.framework.spring.mvc.BaseReturnUtils;
+import com.unionmed.framework.spring.mvc.response.RequestResponseBodyCryptProperties;
import com.unionmed.framework.util.Generators;
import com.unionmed.framework.util.ObjectUtils;
import org.springframework.core.MethodParameter;
-import org.springframework.core.env.Environment;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.ModelAndViewContainer;
@@ -24,33 +24,23 @@ import java.util.Map;
*/
public class EncryptionResponseBodyInterceptor implements ResponseBodyInterceptor {
- private final Environment environment;
- private final String RESOURCE_PREFIX = "unionmed.web.response.body.crypt.aes";
- private final String SK_KEY = RESOURCE_PREFIX + ".sk";
- private final String ENABLED_KEY = RESOURCE_PREFIX + ".enabled";
- private final boolean enabled;
- private final String sk;
+ private final RequestResponseBodyCryptProperties properties;
- public EncryptionResponseBodyInterceptor(Environment environment) {
- this.environment = environment;
- if (ObjectUtils.equalsIgnore(environment.getProperty(ENABLED_KEY), "true")) {
- this.sk = environment.getProperty(SK_KEY);
- if (ObjectUtils.isEmpty(sk))
+ public EncryptionResponseBodyInterceptor(RequestResponseBodyCryptProperties properties) {
+ this.properties = properties;
+ if (properties.isEnabled()) {
+ if (ObjectUtils.isEmpty(properties.getSk()))
throw new NullPointerException("AES SecretKey is empty");
- this.enabled = true;
- } else {
- this.enabled = false;
- this.sk = null;
}
}
@Override
public Object handleReturnValue(Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest) {
- if (!this.enabled || returnValue == null) return null;
+ if (!properties.isEnabled() || returnValue == null) return returnValue;
String value = webRequest.getHeader(HttpHeaders.X_DATA_CRYPT_E);
if (ObjectUtils.equalsIgnore(value, HttpHeaders.X_DATA_CRYPT_E_VALUE_TRUE)) {
- returnValue = handleReturnValue(sk, Generators.randomMix(16), returnValue);
+ returnValue = handleReturnValue(properties.getSk(), Generators.randomMix(16), returnValue);
}
return returnValue;
diff --git a/framework-orm/pom.xml b/framework-orm/pom.xml
index cd22068..8cce697 100644
--- a/framework-orm/pom.xml
+++ b/framework-orm/pom.xml
@@ -5,7 +5,7 @@
unionmed-framework
com.unionmed
- 0.0.11
+ 0.0.12
4.0.0
diff --git a/framework-test/pom.xml b/framework-test/pom.xml
index 5184739..97c648c 100644
--- a/framework-test/pom.xml
+++ b/framework-test/pom.xml
@@ -5,7 +5,7 @@
com.unionmed
unionmed-framework
- 0.0.11
+ 0.0.12
4.0.0
diff --git a/pom.xml b/pom.xml
index 3734f3c..2b3ff00 100644
--- a/pom.xml
+++ b/pom.xml
@@ -9,7 +9,7 @@
4.0.0
com.unionmed
unionmed-framework
- 0.0.11
+ 0.0.12
pom
unionmed-framework