perf: 加解密数据处理
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
package com.tiesheng.encrypt;
|
||||
|
||||
|
||||
import com.tiesheng.encrypt.config.EncryptRequestBodyAdvice;
|
||||
import com.tiesheng.encrypt.config.DecryptRequestBodyAdvice;
|
||||
import com.tiesheng.encrypt.config.EncryptResponseBodyAdvice;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
@@ -14,7 +14,7 @@ import java.lang.annotation.*;
|
||||
@Target({ElementType.TYPE})
|
||||
@Documented
|
||||
@Import({
|
||||
EncryptRequestBodyAdvice.class,
|
||||
DecryptRequestBodyAdvice.class,
|
||||
EncryptResponseBodyAdvice.class,
|
||||
})
|
||||
public @interface EnableEncryptConfig {
|
||||
|
||||
@@ -21,7 +21,7 @@ import java.nio.charset.Charset;
|
||||
* @author hao
|
||||
*/
|
||||
@ControllerAdvice
|
||||
public class EncryptRequestBodyAdvice implements RequestBodyAdvice {
|
||||
public class DecryptRequestBodyAdvice implements RequestBodyAdvice {
|
||||
|
||||
|
||||
@Autowired
|
||||
@@ -1,11 +1,10 @@
|
||||
package com.tiesheng.encrypt.config;
|
||||
|
||||
import cn.hutool.core.annotation.AnnotationUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import cn.hutool.log.LogFactory;
|
||||
import com.tiesheng.util.CommonUtil;
|
||||
import com.tiesheng.util.config.EncryptConfig;
|
||||
import com.tiesheng.util.pojos.ApiResp;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.http.MediaType;
|
||||
@@ -21,7 +20,7 @@ import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
|
||||
* @author hao
|
||||
*/
|
||||
@ControllerAdvice
|
||||
public class EncryptResponseBodyAdvice implements ResponseBodyAdvice<Object> {
|
||||
public class EncryptResponseBodyAdvice implements ResponseBodyAdvice<ApiResp> {
|
||||
|
||||
@Autowired
|
||||
EncryptConfig encryptConfig;
|
||||
@@ -32,25 +31,19 @@ public class EncryptResponseBodyAdvice implements ResponseBodyAdvice<Object> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class<? extends
|
||||
public ApiResp beforeBodyWrite(ApiResp body, MethodParameter returnType, MediaType selectedContentType, Class<? extends
|
||||
HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
|
||||
|
||||
try {
|
||||
|
||||
String content = JSONUtil.toJsonStr(body);
|
||||
String respData = JSONUtil.parseObj(content).getStr("data");
|
||||
|
||||
if (StrUtil.isEmpty(respData)) {
|
||||
// 无需加密
|
||||
Object data = body.getData();
|
||||
if (data == null || !body.successful()) {
|
||||
return body;
|
||||
}
|
||||
|
||||
JSONObject resp = JSONUtil.parseObj(content);
|
||||
resp.set("encrypt", true);
|
||||
if (resp.getInt("code") == 200) {
|
||||
resp.set("data", encryptConfig.encrypt(respData));
|
||||
}
|
||||
return resp;
|
||||
body.setEncrypt(true);
|
||||
body.setData(encryptConfig.encrypt(CommonUtil.writeJsonString(data)));
|
||||
return body;
|
||||
} catch (Exception var17) {
|
||||
LogFactory.get().info("加密数据异常", var17);
|
||||
}
|
||||
|
||||
@@ -25,6 +25,11 @@
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.tiesheng.springboot-parent</groupId>
|
||||
<artifactId>springboot-annotation</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>cn.hutool</groupId>
|
||||
<artifactId>hutool-all</artifactId>
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
package com.tiesheng.util;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.serializer.SerializeFilter;
|
||||
import com.alibaba.fastjson.serializer.SerializerFeature;
|
||||
import com.alibaba.fastjson.support.config.FastJsonConfig;
|
||||
import com.tiesheng.util.config.desensitize.DesensitizeValueFilter;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class CommonUtil {
|
||||
|
||||
|
||||
/**
|
||||
* FastJson配置
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static FastJsonConfig fastJsonConfig() {
|
||||
FastJsonConfig config = new FastJsonConfig();
|
||||
config.setSerializerFeatures(SerializerFeature.WriteMapNullValue,
|
||||
SerializerFeature.WriteNullStringAsEmpty,
|
||||
SerializerFeature.WriteEnumUsingName);
|
||||
config.setDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
config.setSerializeFilters(new DesensitizeValueFilter());
|
||||
return config;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 格式化数据
|
||||
*
|
||||
* @param value
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
public static String writeJsonString(Object value) throws IOException {
|
||||
FastJsonConfig fastJsonConfig = fastJsonConfig();
|
||||
SerializeFilter[] globalFilters = fastJsonConfig.getSerializeFilters();
|
||||
List<SerializeFilter> allFilters = new ArrayList(Arrays.asList(globalFilters));
|
||||
|
||||
ByteArrayOutputStream outnew = new ByteArrayOutputStream();
|
||||
JSON.writeJSONStringWithFastJsonConfig(outnew, fastJsonConfig.getCharset(),
|
||||
value, fastJsonConfig.getSerializeConfig(),
|
||||
allFilters.toArray(new SerializeFilter[allFilters.size()]),
|
||||
fastJsonConfig.getDateFormat(), JSON.DEFAULT_GENERATE_FEATURE,
|
||||
fastJsonConfig.getSerializerFeatures());
|
||||
return outnew.toString();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.tiesheng.core.config.desensitize;
|
||||
package com.tiesheng.util.config.desensitize;
|
||||
|
||||
import cn.hutool.core.util.ClassUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
@@ -72,11 +72,6 @@
|
||||
<artifactId>springboot-poi</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.tiesheng.springboot-parent</groupId>
|
||||
<artifactId>springboot-annotation</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.tiesheng.springboot-parent</groupId>
|
||||
<artifactId>springboot-login</artifactId>
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
package com.tiesheng.core.config.json;
|
||||
|
||||
import com.alibaba.fastjson.serializer.SerializerFeature;
|
||||
import com.alibaba.fastjson.support.config.FastJsonConfig;
|
||||
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
|
||||
import com.tiesheng.core.config.desensitize.DesensitizeValueFilter;
|
||||
import com.tiesheng.util.CommonUtil;
|
||||
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
@@ -26,15 +24,9 @@ public class FastJsonMessageConverter {
|
||||
*/
|
||||
@Bean
|
||||
public HttpMessageConverters fastJsonHttpMessageConverters() {
|
||||
FastJsonConfig config = new FastJsonConfig();
|
||||
config.setSerializerFeatures(SerializerFeature.WriteMapNullValue,
|
||||
SerializerFeature.WriteNullStringAsEmpty,
|
||||
SerializerFeature.WriteEnumUsingName);
|
||||
config.setDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
config.setSerializeFilters(new DesensitizeValueFilter());
|
||||
|
||||
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
|
||||
fastConverter.setFastJsonConfig(config);
|
||||
fastConverter.setFastJsonConfig(CommonUtil.fastJsonConfig());
|
||||
fastConverter.setDefaultCharset(StandardCharsets.UTF_8);
|
||||
|
||||
List<MediaType> mediaTypes = new ArrayList<>();
|
||||
|
||||
Reference in New Issue
Block a user