feat:模块名称调整
This commit is contained in:
45
springboot-encrypt/pom.xml
Normal file
45
springboot-encrypt/pom.xml
Normal file
@@ -0,0 +1,45 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.tiesheng</groupId>
|
||||
<artifactId>springboot-parent</artifactId>
|
||||
<version>0.0.18</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>springboot-encrypt</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
<maven.compiler.target>8</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.bouncycastle</groupId>
|
||||
<artifactId>bcprov-jdk15to18</artifactId>
|
||||
<version>1.68</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.tiesheng</groupId>
|
||||
<artifactId>springboot-util</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.tiesheng</groupId>
|
||||
<artifactId>springboot-annotation</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.tiesheng.encrypt;
|
||||
|
||||
|
||||
import com.tiesheng.encrypt.config.EncryptConfig;
|
||||
import com.tiesheng.encrypt.config.EncryptRequestBodyAdvice;
|
||||
import com.tiesheng.encrypt.config.EncryptResponseBodyAdvice;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @author hao
|
||||
*/
|
||||
@Configuration
|
||||
@ComponentScan(basePackageClasses = {
|
||||
EncryptConfig.class,
|
||||
EncryptRequestBodyAdvice.class,
|
||||
EncryptResponseBodyAdvice.class,
|
||||
})
|
||||
public class EnableEncryptConfig {
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.tiesheng.encrypt.config;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @author hao
|
||||
*/
|
||||
@Configuration
|
||||
@ConfigurationProperties(prefix = "tiesheng.encrypt")
|
||||
public class EncryptConfig {
|
||||
|
||||
public String publicD;
|
||||
public String privateQ;
|
||||
private boolean enable = false;
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// setter\getter
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
|
||||
public boolean isEnable() {
|
||||
return enable;
|
||||
}
|
||||
|
||||
public void setEnable(boolean enable) {
|
||||
this.enable = enable;
|
||||
}
|
||||
|
||||
public String getPublicD() {
|
||||
return publicD;
|
||||
}
|
||||
|
||||
public void setPublicD(String publicD) {
|
||||
this.publicD = publicD;
|
||||
}
|
||||
|
||||
public String getPrivateQ() {
|
||||
return privateQ;
|
||||
}
|
||||
|
||||
public void setPrivateQ(String privateQ) {
|
||||
this.privateQ = privateQ;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
package com.tiesheng.encrypt.config;
|
||||
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import cn.hutool.core.util.CharsetUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.crypto.ECKeyUtil;
|
||||
import cn.hutool.crypto.SmUtil;
|
||||
import cn.hutool.crypto.asymmetric.KeyType;
|
||||
import cn.hutool.crypto.asymmetric.SM2;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
|
||||
import org.bouncycastle.crypto.params.ECPublicKeyParameters;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpInputMessage;
|
||||
import org.springframework.http.converter.HttpMessageConverter;
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdvice;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.lang.reflect.Type;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
/**
|
||||
* @author hao
|
||||
*/
|
||||
@ControllerAdvice
|
||||
public class EncryptRequestBodyAdvice implements RequestBodyAdvice {
|
||||
|
||||
|
||||
@Autowired
|
||||
EncryptConfig encryptConfig;
|
||||
|
||||
|
||||
@Override
|
||||
public boolean supports(MethodParameter methodParameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter parameter, Type targetType,
|
||||
Class<? extends HttpMessageConverter<?>> converterType) {
|
||||
|
||||
if (!encryptConfig.isEnable()) {
|
||||
return inputMessage;
|
||||
}
|
||||
|
||||
try {
|
||||
return new DecryptHttpInputMessage(inputMessage, encryptConfig.getPrivateQ());
|
||||
} catch (Exception ignore) {
|
||||
}
|
||||
|
||||
return inputMessage;
|
||||
}
|
||||
|
||||
@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;
|
||||
}
|
||||
|
||||
|
||||
static class DecryptHttpInputMessage implements HttpInputMessage {
|
||||
|
||||
private HttpHeaders headers;
|
||||
private InputStream body;
|
||||
|
||||
DecryptHttpInputMessage(HttpInputMessage inputMessage, String privateQ) throws Exception {
|
||||
this.headers = inputMessage.getHeaders();
|
||||
String bodyStr = IoUtil.read(inputMessage.getBody(), CharsetUtil.CHARSET_UTF_8);
|
||||
String encryptData = JSONUtil.parseObj(bodyStr).getStr("encryptData");
|
||||
if (!StrUtil.isEmpty(encryptData)) {
|
||||
// 部分语言加密之后缺少04前缀,如果解密失败,可尝试增加04
|
||||
ECPrivateKeyParameters privateKeyParameters = ECKeyUtil.toSm2PrivateParams(privateQ);
|
||||
ECPublicKeyParameters publicKeyParameters = ECKeyUtil.getPublicParams(privateKeyParameters);
|
||||
SM2 sm2 = SmUtil.sm2(privateKeyParameters, publicKeyParameters);
|
||||
String decrypt = sm2.decryptStr(encryptData, KeyType.PrivateKey);
|
||||
this.body = IoUtil.toStream(decrypt, Charset.defaultCharset());
|
||||
} else {
|
||||
this.body = IoUtil.toStream(bodyStr, Charset.defaultCharset());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream getBody() {
|
||||
return this.body;
|
||||
}
|
||||
|
||||
@Override
|
||||
public HttpHeaders getHeaders() {
|
||||
return this.headers;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
package com.tiesheng.encrypt.config;
|
||||
|
||||
import cn.hutool.core.annotation.AnnotationUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.crypto.ECKeyUtil;
|
||||
import cn.hutool.crypto.SmUtil;
|
||||
import cn.hutool.crypto.asymmetric.KeyType;
|
||||
import cn.hutool.crypto.asymmetric.SM2;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import cn.hutool.log.LogFactory;
|
||||
import com.tiesheng.annotation.encrypt.EncryptedRespBody;
|
||||
import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
|
||||
import org.bouncycastle.crypto.params.ECPublicKeyParameters;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.converter.HttpMessageConverter;
|
||||
import org.springframework.http.server.ServerHttpRequest;
|
||||
import org.springframework.http.server.ServerHttpResponse;
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
|
||||
|
||||
|
||||
/**
|
||||
* @author hao
|
||||
*/
|
||||
@ControllerAdvice
|
||||
public class EncryptResponseBodyAdvice implements ResponseBodyAdvice<Object> {
|
||||
|
||||
@Autowired
|
||||
EncryptConfig encryptConfig;
|
||||
|
||||
@Override
|
||||
public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class<? extends
|
||||
HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
|
||||
|
||||
if (!encryptConfig.isEnable()) {
|
||||
return body;
|
||||
}
|
||||
|
||||
boolean encrypt = AnnotationUtil.getAnnotation(returnType.getContainingClass(), EncryptedRespBody.class) != null;
|
||||
if (!encrypt) {
|
||||
return body;
|
||||
}
|
||||
|
||||
try {
|
||||
|
||||
String content = JSONUtil.toJsonStr(body);
|
||||
String respData = JSONUtil.parseObj(content).getStr("data");
|
||||
|
||||
if (StrUtil.isEmpty(respData)) {
|
||||
// 无需加密
|
||||
return body;
|
||||
}
|
||||
|
||||
JSONObject resp = JSONUtil.parseObj(content);
|
||||
resp.set("encrypted", true);
|
||||
if (resp.getInt("code") == 200) {
|
||||
// 用公钥进行加密
|
||||
ECPrivateKeyParameters privateKeyParameters = ECKeyUtil.toSm2PrivateParams(encryptConfig.getPrivateQ());
|
||||
ECPublicKeyParameters publicKeyParameters = ECKeyUtil.getPublicParams(privateKeyParameters);
|
||||
SM2 sm2 = SmUtil.sm2(privateKeyParameters, publicKeyParameters);
|
||||
String decrypt = sm2.encryptHex(respData, KeyType.PublicKey);
|
||||
resp.set("data", decrypt.substring(2));
|
||||
}
|
||||
return resp;
|
||||
} catch (Exception var17) {
|
||||
LogFactory.get().info("加密数据异常", var17);
|
||||
}
|
||||
|
||||
return body;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user