perf: 加解密数据处理

This commit is contained in:
曾文豪
2023-03-07 15:16:26 +08:00
parent c0fb149558
commit 0094bf2e69
8 changed files with 75 additions and 35 deletions

View File

@@ -1,7 +1,7 @@
package com.tiesheng.encrypt; package com.tiesheng.encrypt;
import com.tiesheng.encrypt.config.EncryptRequestBodyAdvice; import com.tiesheng.encrypt.config.DecryptRequestBodyAdvice;
import com.tiesheng.encrypt.config.EncryptResponseBodyAdvice; import com.tiesheng.encrypt.config.EncryptResponseBodyAdvice;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
@@ -14,7 +14,7 @@ import java.lang.annotation.*;
@Target({ElementType.TYPE}) @Target({ElementType.TYPE})
@Documented @Documented
@Import({ @Import({
EncryptRequestBodyAdvice.class, DecryptRequestBodyAdvice.class,
EncryptResponseBodyAdvice.class, EncryptResponseBodyAdvice.class,
}) })
public @interface EnableEncryptConfig { public @interface EnableEncryptConfig {

View File

@@ -21,7 +21,7 @@ import java.nio.charset.Charset;
* @author hao * @author hao
*/ */
@ControllerAdvice @ControllerAdvice
public class EncryptRequestBodyAdvice implements RequestBodyAdvice { public class DecryptRequestBodyAdvice implements RequestBodyAdvice {
@Autowired @Autowired

View File

@@ -1,11 +1,10 @@
package com.tiesheng.encrypt.config; package com.tiesheng.encrypt.config;
import cn.hutool.core.annotation.AnnotationUtil; 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 cn.hutool.log.LogFactory;
import com.tiesheng.util.CommonUtil;
import com.tiesheng.util.config.EncryptConfig; import com.tiesheng.util.config.EncryptConfig;
import com.tiesheng.util.pojos.ApiResp;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter; import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
@@ -21,7 +20,7 @@ import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
* @author hao * @author hao
*/ */
@ControllerAdvice @ControllerAdvice
public class EncryptResponseBodyAdvice implements ResponseBodyAdvice<Object> { public class EncryptResponseBodyAdvice implements ResponseBodyAdvice<ApiResp> {
@Autowired @Autowired
EncryptConfig encryptConfig; EncryptConfig encryptConfig;
@@ -32,25 +31,19 @@ public class EncryptResponseBodyAdvice implements ResponseBodyAdvice<Object> {
} }
@Override @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) { HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
try { try {
String content = JSONUtil.toJsonStr(body); Object data = body.getData();
String respData = JSONUtil.parseObj(content).getStr("data"); if (data == null || !body.successful()) {
if (StrUtil.isEmpty(respData)) {
// 无需加密
return body; return body;
} }
JSONObject resp = JSONUtil.parseObj(content); body.setEncrypt(true);
resp.set("encrypt", true); body.setData(encryptConfig.encrypt(CommonUtil.writeJsonString(data)));
if (resp.getInt("code") == 200) { return body;
resp.set("data", encryptConfig.encrypt(respData));
}
return resp;
} catch (Exception var17) { } catch (Exception var17) {
LogFactory.get().info("加密数据异常", var17); LogFactory.get().info("加密数据异常", var17);
} }

View File

@@ -25,6 +25,11 @@
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<dependency>
<groupId>com.tiesheng.springboot-parent</groupId>
<artifactId>springboot-annotation</artifactId>
</dependency>
<dependency> <dependency>
<groupId>cn.hutool</groupId> <groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId> <artifactId>hutool-all</artifactId>

View File

@@ -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();
}
}

View File

@@ -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.ClassUtil;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;

View File

@@ -72,11 +72,6 @@
<artifactId>springboot-poi</artifactId> <artifactId>springboot-poi</artifactId>
</dependency> </dependency>
<dependency>
<groupId>com.tiesheng.springboot-parent</groupId>
<artifactId>springboot-annotation</artifactId>
</dependency>
<dependency> <dependency>
<groupId>com.tiesheng.springboot-parent</groupId> <groupId>com.tiesheng.springboot-parent</groupId>
<artifactId>springboot-login</artifactId> <artifactId>springboot-login</artifactId>

View File

@@ -1,9 +1,7 @@
package com.tiesheng.core.config.json; 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.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.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
@@ -26,15 +24,9 @@ public class FastJsonMessageConverter {
*/ */
@Bean @Bean
public HttpMessageConverters fastJsonHttpMessageConverters() { 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(); FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
fastConverter.setFastJsonConfig(config); fastConverter.setFastJsonConfig(CommonUtil.fastJsonConfig());
fastConverter.setDefaultCharset(StandardCharsets.UTF_8); fastConverter.setDefaultCharset(StandardCharsets.UTF_8);
List<MediaType> mediaTypes = new ArrayList<>(); List<MediaType> mediaTypes = new ArrayList<>();