Skip to content

Commit

Permalink
2.3.2
Browse files Browse the repository at this point in the history
  • Loading branch information
yl-yue committed Apr 17, 2021
1 parent fcac72c commit 7149b39
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import ai.yue.library.web.util.servlet.ServletUtils;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.symmetric.SymmetricCrypto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.core.MethodParameter;
import org.springframework.http.HttpHeaders;
Expand All @@ -29,6 +30,7 @@
* @author ylyue
* @since 2020年9月18日
*/
@Slf4j
@ControllerAdvice
@ConditionalOnClass(HttpServletRequest.class)
public class RequestDecryptHandler extends RequestBodyAdviceAdapter {
Expand Down Expand Up @@ -65,6 +67,7 @@ public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodPara
// 创建加密算法实例
KeyExchangeStorage keyExchangeStorage = SpringUtils.getBean(KeyExchangeStorage.class);
String exchangeKey = keyExchangeStorage.getExchangeKey(keyExchangeStorageKey);
log.debug("【密钥交换-解密】keyExchangeStorageKey={},exchangeKey={}", keyExchangeStorageKey, exchangeKey);
ExchangeKeyEnum exchangeKeyType = methodAnnotation.exchangeKeyType();
symmetricCrypto = exchangeKeyType.getSymmetricCrypto(exchangeKey.getBytes());
} else {
Expand All @@ -77,7 +80,9 @@ public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodPara
return new HttpInputMessage() {
@Override
public InputStream getBody() throws IOException {
return new ByteArrayInputStream(finalSymmetricCrypto.decrypt(StreamUtils.copyToString(inputMessage.getBody(), Charset.defaultCharset())));
String decryptStr = StreamUtils.copyToString(inputMessage.getBody(), Charset.defaultCharset());
log.debug("【密钥交换-解密】decryptStr={}", decryptStr);
return new ByteArrayInputStream(finalSymmetricCrypto.decrypt(decryptStr));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package ai.yue.library.data.jdbc.client;

import ai.yue.library.base.crypto.client.SecureSingleton;
import ai.yue.library.base.exception.DbException;
import ai.yue.library.base.util.ClassUtils;
import ai.yue.library.base.util.ListUtils;
import ai.yue.library.base.util.MapUtils;
import ai.yue.library.base.util.StringUtils;
import ai.yue.library.base.view.ResultPrompt;
import ai.yue.library.data.jdbc.client.dialect.Dialect;
import ai.yue.library.data.jdbc.config.properties.DataEncrypt;
import ai.yue.library.data.jdbc.config.properties.JdbcProperties;
import ai.yue.library.data.jdbc.constant.DbConstant;
import ai.yue.library.data.jdbc.support.BeanPropertyRowMapper;
Expand Down Expand Up @@ -629,32 +627,32 @@ protected void aop(String tableName, JSONObject paramJson) {

}

protected void dataEncrypt(String tableName, JSONObject paramJson) {
List<DataEncrypt> dataEncryptConfigList = getJdbcProperties().getDataEncryptConfigs();
if (ListUtils.isNotEmpty(dataEncryptConfigList)) {
for (DataEncrypt dataEncryptConfig : dataEncryptConfigList) {
String dataEncryptTableName = dataEncryptConfig.getTableName();
if (tableName.equalsIgnoreCase(dataEncryptTableName)) {
List<String> fieldNameList = dataEncryptConfig.getFieldNames();
for (String fieldName : fieldNameList) {
paramJson.replace(fieldName, SecureSingleton.getAES().encryptBase64(paramJson.getString(fieldName)));
}

break;
}
}
}
}

protected void audit(String tableName, JSONObject paramJson) {
List<String> auditTableNames = getJdbcProperties().getAuditTableNames();
if (ListUtils.isNotEmpty(auditTableNames)) {
for (String auditTableName : auditTableNames) {
if (tableName.equalsIgnoreCase(auditTableName)) {
paramJson.put("","");
}
}
}
}
// protected void dataEncrypt(String tableName, JSONObject paramJson) {
// List<DataEncrypt> dataEncryptConfigList = getJdbcProperties().getDataEncryptConfigs();
// if (ListUtils.isNotEmpty(dataEncryptConfigList)) {
// for (DataEncrypt dataEncryptConfig : dataEncryptConfigList) {
// String dataEncryptTableName = dataEncryptConfig.getTableName();
// if (tableName.equalsIgnoreCase(dataEncryptTableName)) {
// List<String> fieldNameList = dataEncryptConfig.getFieldNames();
// for (String fieldName : fieldNameList) {
// paramJson.replace(fieldName, SecureSingleton.getAES().encryptBase64(paramJson.getString(fieldName)));
// }
//
// break;
// }
// }
// }
// }
//
// protected void audit(String tableName, JSONObject paramJson) {
// List<String> auditTableNames = getJdbcProperties().getAuditTableNames();
// if (ListUtils.isNotEmpty(auditTableNames)) {
// for (String auditTableName : auditTableNames) {
// if (tableName.equalsIgnoreCase(auditTableName)) {
// paramJson.put("","");
// }
// }
// }
// }

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import ai.yue.library.base.crypto.annotation.key.exchange.RequestDecrypt;
import ai.yue.library.base.crypto.annotation.key.exchange.ResponseEncrypt;
import ai.yue.library.base.crypto.constant.key.exchange.ExchangeKeyEnum;
import ai.yue.library.base.view.R;
import ai.yue.library.base.view.Result;
import ai.yue.library.test.ipo.UserIPO;
Expand All @@ -23,10 +24,22 @@ public Result<?> decrypt(@RequestBody UserIPO userIPO) {
return R.success(userIPO);
}

@RequestDecrypt(exchangeKeyType = ExchangeKeyEnum.SM2_SM4)
@PostMapping("/decrypt/SM2_SM4")
public Result<?> decryptSM2_SM4(@RequestBody UserIPO userIPO) {
return R.success(userIPO);
}

@ResponseEncrypt
@GetMapping("/{encrypt}")
public Result<?> encrypt(@PathVariable String encrypt) {
return R.success(encrypt);
}

@ResponseEncrypt(exchangeKeyType = ExchangeKeyEnum.SM2_SM4)
@GetMapping("/{encrypt}/SM2_SM4")
public Result<?> encryptSM2_SM4(@PathVariable String encrypt) {
return R.success(encrypt);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import ai.yue.library.base.util.IdUtils;
import ai.yue.library.base.view.Result;
import ai.yue.library.test.ipo.UserIPO;
import cn.hutool.core.lang.Console;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.SmUtil;
import cn.hutool.crypto.asymmetric.KeyType;
Expand All @@ -12,6 +13,7 @@
import cn.hutool.crypto.symmetric.AES;
import cn.hutool.crypto.symmetric.SymmetricCrypto;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -31,30 +33,11 @@
* @author ylyue
* @since 2021/4/12
*/
//@RunWith(SpringRunner.class)
//@SpringBootTest(classes = TestApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class KeyExchangeTest {

/**
* @LocalServerPort 提供了 @Value("${local.server.port}") 的代替
*/
@LocalServerPort
private int port;

@Autowired
// private TestRestTemplate restTemplate;
private RestTemplate restTemplate = new RestTemplate();

// private String serverUrl;
private String serverUrl = "http://localhost:8080";

// @Before
// public void setUp() throws Exception {
// serverUrl = String.format("http://localhost:%d/", port);
// System.out.println(serverUrl);
// System.out.println(String.format("port is : [%d]", port));
// }

/**
* 通信过程加密
* 1. 前端大写UUID → 后端返回RSA公钥
Expand All @@ -63,8 +46,8 @@ public class KeyExchangeTest {
@Test
public void aes() {
// 第一步
String uuid = IdUtils.getSimpleUUID();
String url = serverUrl + "/open/v2.3/keyExchange/" + uuid + "?exchangeKeyType={exchangeKeyType}";
String storageKey = IdUtils.getSimpleUUID();
String url = serverUrl + "/open/v2.3/keyExchange/" + storageKey + "?exchangeKeyType={exchangeKeyType}";
Result exchangeKeyResult = restTemplate.postForObject(url, null, Result.class, ExchangeKeyEnum.RSA_AES);
exchangeKeyResult.successValidate();
String data = (String) exchangeKeyResult.getData();
Expand All @@ -90,7 +73,7 @@ public void aes() {
String storageKeyAlias = IdUtils.getSimpleUUID();
JSONObject paramJson2 = new JSONObject();
paramJson2.put("storageKeyAlias", storageKeyAlias);
Result exchangeKeyResult3 = restTemplate.postForObject(serverUrl + "/open/v2.3/keyExchange/" + uuid + "/addAlias", paramJson2, Result.class, ExchangeKeyEnum.RSA_AES);
Result exchangeKeyResult3 = restTemplate.postForObject(serverUrl + "/open/v2.3/keyExchange/" + storageKey + "/addAlias", paramJson2, Result.class, ExchangeKeyEnum.RSA_AES);
exchangeKeyResult3.successValidate();

// 业务接口请求解密测试
Expand Down Expand Up @@ -122,8 +105,8 @@ public void aes() {
@Test
public void sm4() {
// 第一步
String uuid = IdUtils.getSimpleUUID();
String url = serverUrl + "/open/v2.3/keyExchange/" + uuid + "?exchangeKeyType={exchangeKeyType}";
String storageKey = IdUtils.getSimpleUUID();
String url = serverUrl + "/open/v2.3/keyExchange/" + storageKey + "?exchangeKeyType={exchangeKeyType}";
Result exchangeKeyResult = restTemplate.postForObject(url, null, Result.class, ExchangeKeyEnum.SM2_SM4);
exchangeKeyResult.successValidate();
String data = (String) exchangeKeyResult.getData();
Expand All @@ -137,13 +120,43 @@ public void sm4() {
Result exchangeKeyResult2 = restTemplate.postForObject(url, paramJson, Result.class, ExchangeKeyEnum.SM2_SM4);
exchangeKeyResult2.successValidate();
String serverRsaEncryptClientAesKey = (String) exchangeKeyResult2.getData();
String clientAesKey = sm2.decryptStr(serverRsaEncryptClientAesKey, KeyType.PrivateKey);
SymmetricCrypto sm4 = SmUtil.sm4(clientAesKey.getBytes());
String exchangeKey = sm2.decryptStr(serverRsaEncryptClientAesKey, KeyType.PrivateKey);
SymmetricCrypto sm4 = SmUtil.sm4(exchangeKey.getBytes());
String encryptBase64 = sm4.encryptBase64("123456");
System.out.println(encryptBase64);
String decryptStr = sm4.decryptStr(encryptBase64);
System.out.println(decryptStr);
Assert.assertEquals("123456", decryptStr);

// 第三步
String storageKeyAlias = IdUtils.getSimpleUUID();
JSONObject paramJson2 = new JSONObject();
paramJson2.put("storageKeyAlias", storageKeyAlias);
Result exchangeKeyResult3 = restTemplate.postForObject(serverUrl + "/open/v2.3/keyExchange/" + storageKey + "/addAlias", paramJson2, Result.class, ExchangeKeyEnum.SM2_SM4);
exchangeKeyResult3.successValidate();

// 业务接口请求解密测试
UserIPO userIPO = new UserIPO();
userIPO.setCellphone("18523146311");
userIPO.setNickname("123456");
userIPO.setBirthday(LocalDate.now());
String userIPOToEncryptBase64 = sm4.encryptBase64(JSONObject.toJSONString(userIPO));
Console.log("storageKey={}", storageKey);
Console.log("storageKeyAlias={}", storageKeyAlias);
Console.log("exchangeKey={}", exchangeKey);
Console.log("userIPOToEncryptBase64={}", userIPOToEncryptBase64);
MultiValueMap headers = new LinkedMultiValueMap();
headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
HttpEntity httpEntity = new HttpEntity<>(userIPOToEncryptBase64, headers);
Result exchangeKeyResult4 = restTemplate.postForObject(serverUrl + "/controllerEncrypt/decrypt/SM2_SM4?access_token=" + storageKeyAlias, httpEntity, Result.class);
exchangeKeyResult4.successValidate();
System.out.println(exchangeKeyResult4);

// 业务接口响应加密测试
Result exchangeKeyResult5 = restTemplate.getForObject(serverUrl + "/controllerEncrypt/encrypt/SM2_SM4?access_token=" + storageKeyAlias, Result.class);
exchangeKeyResult5.successValidate();
String serverEncryptContent = (String) exchangeKeyResult5.getData();
Assert.assertEquals("encrypt", sm4.decryptStr(serverEncryptContent));
}

}

0 comments on commit 7149b39

Please sign in to comment.