接口加密优化

v1.4.1
Parker 4 years ago
parent c7ecad94e4
commit b2b6040984

@ -29,6 +29,7 @@ import javax.servlet.http.HttpServletResponse;
import org.opsli.api.wrapper.system.options.OptionsModel;
import java.util.List;
/**
@ -149,4 +150,11 @@ public interface OptionsApi {
@GetMapping("/getByCode")
ResultVo<OptionsModel> getByCode(String optionCode);
/**
*
* @return ResultVo
*/
@GetMapping("/findAll")
ResultVo<List<OptionsModel>> findAll();
}

@ -9,15 +9,19 @@ public enum OptionsType {
/** 参数类型 */
/** 目前支持 RSA SM2 ECIES 3种模式 */
/** 非对称加密 目前支持 RSA SM2 ECIES 3种模式 */
CRYPTO_ASYMMETRIC("crypto_asymmetric", "加解密-非对称"),
/** 非对称加密 公钥 */
CRYPTO_ASYMMETRIC_PUBLIC_KEY("crypto_asymmetric_public_key", "加解密-非对称-公钥"),
/** 非对称加密 私钥 */
CRYPTO_ASYMMETRIC_PRIVATE_KEY("crypto_asymmetric_private_key", "加解密-非对称-私钥"),
;
private final String code;
private final String desc;
public static OptionsType getCacheType(String cacheType) {
public static OptionsType getType(String cacheType) {
OptionsType[] var1 = values();
for (OptionsType type : var1) {
if (type.code.equalsIgnoreCase(cacheType)) {
@ -41,4 +45,4 @@ public enum OptionsType {
this.code = code;
this.desc = desc;
}
}
}

@ -27,14 +27,10 @@ import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.opsli.api.base.encrypt.BaseEncrypt;
import org.opsli.api.base.result.ResultVo;
import org.opsli.api.wrapper.system.options.OptionsModel;
import org.opsli.common.annotation.InterfaceCrypto;
import org.opsli.common.enums.CryptoAsymmetricType;
import org.opsli.common.enums.OptionsType;
import org.opsli.common.exception.ServiceException;
import org.opsli.core.msg.CoreMsg;
import org.opsli.core.utils.CryptoAsymmetricUtil;
import org.opsli.core.utils.OptionsUtil;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
@ -72,22 +68,13 @@ public class InterfaceCryptoAop {
// 返回结果
Object returnValue;
// 获得系统配置参数 非对称加密枚举
CryptoAsymmetricType asymmetricType = null;
OptionsModel optionsModel = OptionsUtil.getOptionByCode(OptionsType.CRYPTO_ASYMMETRIC);
if(optionsModel != null){
// 获得加密类型
asymmetricType = CryptoAsymmetricType.getCryptoType(
optionsModel.getOptionValue());
}
MethodSignature signature = (MethodSignature) point.getSignature();
// 获得 方法
Method method = signature.getMethod();
// 获得方法注解
InterfaceCrypto annotation =
method.getAnnotation(InterfaceCrypto.class);
if(asymmetricType != null && annotation != null){
if(annotation != null){
// 1. 拆解请求数据
// request 解密
@ -100,7 +87,7 @@ public class InterfaceCryptoAop {
BaseEncrypt baseEncrypt = (BaseEncrypt) arg;
String encryptData = baseEncrypt.getEncryptData();
// 解密对象
Object dataToObj = CryptoAsymmetricUtil.decryptToObj(asymmetricType, encryptData);
Object dataToObj = CryptoAsymmetricUtil.decryptToObj(encryptData);
// 根据方法类型转化对象
Type type = TypeUtil.getParamType(method, i);
@ -136,11 +123,11 @@ public class InterfaceCryptoAop {
if(returnValue instanceof ResultVo){
ResultVo<Object> ret = (ResultVo<Object>) returnValue;
ret.setData(
CryptoAsymmetricUtil.encrypt(asymmetricType, ret.getData())
CryptoAsymmetricUtil.encrypt(ret.getData())
);
returnValue = ret;
}else {
returnValue = CryptoAsymmetricUtil.encrypt(asymmetricType, returnValue);
returnValue = CryptoAsymmetricUtil.encrypt(returnValue);
}
}catch (Exception e){
// RSA非对称加密失败

@ -230,11 +230,10 @@ public class CryptoAsymmetricUtil {
/**
*
* @param cryptoAsymmetricType
* @param data
* @return String
*/
public static String encrypt(final CryptoAsymmetricType cryptoAsymmetricType, final Object data){
public static String encrypt(final Object data){
JSONObject jsonObject = new JSONObject();
jsonObject.put(CRYPTO_KEY, data);
@ -244,26 +243,47 @@ public class CryptoAsymmetricUtil {
try {
OtherCryptoAsymmetricModel cryptoAsymmetric = getCryptoAsymmetric(cryptoAsymmetricType);
// 加解密方式
OptionsModel cryptoAsymmetric = OptionsUtil.getOptionByCode(OptionsType.CRYPTO_ASYMMETRIC);
// 公钥
OptionsModel cryptoAsymmetricPublicKey =
OptionsUtil.getOptionByCode(OptionsType.CRYPTO_ASYMMETRIC_PUBLIC_KEY);
// 私钥
OptionsModel cryptoAsymmetricPrivateKey =
OptionsUtil.getOptionByCode(OptionsType.CRYPTO_ASYMMETRIC_PRIVATE_KEY);
// 非法验证
if(cryptoAsymmetric == null || cryptoAsymmetricPublicKey == null ||
cryptoAsymmetricPrivateKey == null
){
throw new RuntimeException();
}
// 如果找不到 公私钥 直接返回原始数据
if(cryptoAsymmetric == null){
// 加解密方式枚举
CryptoAsymmetricType cryptoType = CryptoAsymmetricType.getCryptoType(
cryptoAsymmetric.getOptionValue());
// 非法验证
if(cryptoType == null){
throw new RuntimeException();
}
switch (cryptoAsymmetricType){
switch (cryptoType){
case RSA:
RSA rsa = SecureUtil.rsa(cryptoAsymmetric.getPrivateKey(), cryptoAsymmetric.getPublicKey());
RSA rsa = SecureUtil.rsa(cryptoAsymmetricPrivateKey.getOptionValue(),
cryptoAsymmetricPublicKey.getOptionValue());
encryptedStr = rsa.encryptBase64(
StrUtil.bytes(encryptedStr, CharsetUtil.CHARSET_UTF_8), KeyType.PublicKey);
break;
case SM2:
SM2 sm2 = SmUtil.sm2(cryptoAsymmetric.getPrivateKey(), cryptoAsymmetric.getPublicKey());
SM2 sm2 = SmUtil.sm2(cryptoAsymmetricPrivateKey.getOptionValue(),
cryptoAsymmetricPublicKey.getOptionValue());
encryptedStr = sm2.encryptBase64(
StrUtil.bytes(encryptedStr, CharsetUtil.CHARSET_UTF_8), KeyType.PublicKey);
break;
case ECIES:
ECIES ecies = new ECIES(cryptoAsymmetric.getPrivateKey(), cryptoAsymmetric.getPublicKey());
ECIES ecies = new ECIES(cryptoAsymmetricPrivateKey.getOptionValue(),
cryptoAsymmetricPublicKey.getOptionValue());
encryptedStr = ecies.encryptBase64(
StrUtil.bytes(encryptedStr, CharsetUtil.CHARSET_UTF_8), KeyType.PublicKey);
break;
@ -280,13 +300,12 @@ public class CryptoAsymmetricUtil {
/**
* RSA
* @param cryptoAsymmetricType
* @param data
* @return Object
*/
public static Object decryptToObj(final CryptoAsymmetricType cryptoAsymmetricType, final String data){
public static Object decryptToObj(final String data){
Object obj;
String decryptedData = decrypt(cryptoAsymmetricType, data);
String decryptedData = decrypt(data);
try{
obj = JSONObject.parse(decryptedData);
}catch (Exception e){
@ -298,36 +317,53 @@ public class CryptoAsymmetricUtil {
/**
*
* @param cryptoAsymmetricType
* @param data
* @return String
*/
public static String decrypt(final CryptoAsymmetricType cryptoAsymmetricType, final String data){
public static String decrypt(final String data){
String decryptStr;
try {
if(StringUtils.isEmpty(data)){
throw new RuntimeException();
}
OtherCryptoAsymmetricModel cryptoAsymmetric = getCryptoAsymmetric(cryptoAsymmetricType);
// 如果找不到 公私钥 直接返回原始数据
if(cryptoAsymmetric == null){
// 加解密方式
OptionsModel cryptoAsymmetric = OptionsUtil.getOptionByCode(OptionsType.CRYPTO_ASYMMETRIC);
// 公钥
OptionsModel cryptoAsymmetricPublicKey =
OptionsUtil.getOptionByCode(OptionsType.CRYPTO_ASYMMETRIC_PUBLIC_KEY);
// 私钥
OptionsModel cryptoAsymmetricPrivateKey =
OptionsUtil.getOptionByCode(OptionsType.CRYPTO_ASYMMETRIC_PRIVATE_KEY);
// 非法验证
if(cryptoAsymmetric == null || cryptoAsymmetricPublicKey == null ||
cryptoAsymmetricPrivateKey == null
){
throw new RuntimeException();
}
// 加解密方式枚举
CryptoAsymmetricType cryptoType = CryptoAsymmetricType.getCryptoType(
cryptoAsymmetric.getOptionValue());
// 非法验证
if(cryptoType == null){
throw new RuntimeException();
}
String tmp;
String currData = data.replaceAll(" ", "+");
switch (cryptoAsymmetricType){
switch (cryptoType){
case RSA:
RSA rsa = SecureUtil.rsa(cryptoAsymmetric.getPrivateKey(), cryptoAsymmetric.getPublicKey());
RSA rsa = SecureUtil.rsa(cryptoAsymmetricPrivateKey.getOptionValue(), cryptoAsymmetricPublicKey.getOptionValue());
tmp = rsa.decryptStr(currData, KeyType.PrivateKey);
break;
case SM2:
SM2 sm2 = SmUtil.sm2(cryptoAsymmetric.getPrivateKey(), cryptoAsymmetric.getPublicKey());
SM2 sm2 = SmUtil.sm2(cryptoAsymmetricPrivateKey.getOptionValue(), cryptoAsymmetricPublicKey.getOptionValue());
tmp = sm2.decryptStr(currData, KeyType.PrivateKey);
break;
case ECIES:
ECIES ecies = new ECIES(cryptoAsymmetric.getPrivateKey(), cryptoAsymmetric.getPublicKey());
ECIES ecies = new ECIES(cryptoAsymmetricPrivateKey.getOptionValue(), cryptoAsymmetricPublicKey.getOptionValue());
tmp = ecies.decryptStr(currData, KeyType.PrivateKey);
break;
default:

@ -1,182 +0,0 @@
/**
* Copyright 2020 OPSLI https://www.opsli.com
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package org.opsli.core.utils;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import com.alibaba.fastjson.JSONObject;
import org.opsli.common.exception.ServiceException;
import org.opsli.common.utils.Props;
import org.opsli.core.msg.CoreMsg;
import java.util.Collection;
/**
* RSA
*
* @author
*/
public enum EncryptAndDecryptByRsaUtil {
/** 默认实例 */
INSTANCE();
/** RSA KEY */
private final String rsaKey = "data";
/** RSA对象 */
private final RSA rsa;
EncryptAndDecryptByRsaUtil(){
this.rsa = this.createRsa();
}
/**
* RSA
* @return RSA
*/
public RSA createRsa(){
return new RSA();
}
/**
* RSA
* @param publicKey
* @param privateKey
* @return RSA
*/
public RSA createRsa(String publicKey, String privateKey){
return new RSA(privateKey, publicKey);
}
/**
*
* @return String
*/
public String getPublicKey(){
return rsa.getPublicKeyBase64();
}
/**
*
* @return String
*/
public String getPrivateKey(){
return rsa.getPrivateKeyBase64();
}
/**
* RSA
* @param data
* @return String
*/
public String encryptedData(Object data){
return this.encryptedData(this.rsa, data);
}
/**
* RSA
* @param rsa RSA
* @param data
* @return String
*/
public String encryptedData(RSA rsa, Object data){
String encryptedStr;
try {
JSONObject jsonObject = new JSONObject();
jsonObject.put(rsaKey, data);
encryptedStr = rsa.encryptBase64(StrUtil.bytes(jsonObject.toString(), CharsetUtil.CHARSET_UTF_8), KeyType.PublicKey);
}catch (Exception e){
// 加密失败
throw new ServiceException(CoreMsg.OTHER_EXCEPTION_RSA_EN);
}
return encryptedStr;
}
/**
* RSA
* @param data
* @return Object
*/
public Object decryptedDataToObj(String data){
return this.decryptedDataToObj(this.rsa, data);
}
/**
* RSA
* @param rsa RSA
* @param data
* @return Object
*/
public Object decryptedDataToObj(RSA rsa, String data){
Object obj;
String decryptedData = this.decryptedData(rsa, data);
try{
obj = JSONObject.parse(decryptedData);
}catch (Exception e){
// RSA非对称解密反射失败
throw new ServiceException(CoreMsg.OTHER_EXCEPTION_RSA_REFLEX);
}
return obj;
}
/**
* RSA
* @param data
* @return String
*/
public String decryptedData(String data){
return this.decryptedData(this.rsa, data);
}
/**
* RSA
* @param rsa RSA
* @param data
* @return String
*/
public String decryptedData(RSA rsa,String data){
//解密,因为编码传值时有空格出现
String decryptStr;
try{
data = data.replaceAll(" ", "+");
String tmp = rsa.decryptStr(data, KeyType.PrivateKey);
JSONObject jsonObject = JSONObject.parseObject(tmp);
Object obj = jsonObject.get(rsaKey);
if(obj instanceof Collection){
decryptStr = jsonObject.getJSONArray(rsaKey).toJSONString();
}else{
decryptStr = jsonObject.getJSONObject(rsaKey).toJSONString();
}
}catch (Exception e){
// 解密失败
throw new ServiceException(CoreMsg.OTHER_EXCEPTION_RSA_DE);
}
return decryptStr;
}
}

@ -15,11 +15,14 @@
*/
package org.opsli.core.utils;
import cn.hutool.core.collection.CollUtil;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.opsli.api.base.result.ResultVo;
import org.opsli.api.web.system.options.OptionsApi;
import org.opsli.api.wrapper.system.options.OptionsModel;
import org.opsli.api.wrapper.system.other.crypto.OtherCryptoAsymmetricModel;
import org.opsli.common.enums.OptionsType;
import org.opsli.core.cache.local.CacheUtil;
import org.opsli.core.msg.CoreMsg;
@ -28,6 +31,9 @@ import org.springframework.context.annotation.Lazy;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map;
import static org.opsli.common.constants.OrderConstants.UTIL_ORDER;
/**
@ -122,6 +128,29 @@ public class OptionsUtil {
return model;
}
/**
*
* @return Map
*/
public static Map<String, OptionsModel> findAllOptions(){
ResultVo<List<OptionsModel>> optionsApiAll = optionsApi.findAll();
if(optionsApiAll == null || !optionsApiAll.isSuccess()){
return null;
}
List<OptionsModel> optionsModels = optionsApiAll.getData();
if(CollUtil.isEmpty(optionsModels)){
return null;
}
Map<String, OptionsModel> optionsModelMap = Maps.newHashMap();
for (OptionsModel optionsModel : optionsModels) {
optionsModelMap.put(optionsModel.getOptionCode(), optionsModel);
}
return optionsModelMap;
}
// ============== 刷新缓存 ==============

@ -1,26 +0,0 @@
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.SmUtil;
import cn.hutool.crypto.asymmetric.SM2;
import org.junit.Test;
/**
* @author
* @date 2021-01-31 5:15
**/
public class EncryptTest {
@Test
public void testEncrypt(){
SM2 sm2 = SmUtil.sm2("MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAJqTLKryt+SYq8oHZ/PeFR64Pq2A9qsKZmZFzU6gvkQ37JeCFCYMitxmF7AtlktSktB3dPdxHEppbfSdY1+i140eAqMZga7j4FmjIdnyW1OauiVgaimJLAEyE7uTAAPggrU9kdqgXbA8hIffm5tu6DKRq/t1NpYEO42S/IGK/yoxAgMBAAECgYEAiWu+klwm0LxKPdpHuK7/58e1MVst8PHWB6aW2AhgHxX46NlkQE92RGsfNCnTLDPFAkCxZCrTE/SXJJmn9yY2qoS26OV0PbTGajk96M8lDi9JSmWCNV1eywPecObSyvtPd5jaPtq2jkgNY/hHJjH6kV7UAFZuaSK7jxskfq7uR2ECQQDPfmGjPiMc65+LE9U7jC4LokyUi1yCgN6AY5MgF6fkxUVJD2mtl9BqRK7qE0OnsRb0NzID3PSfa7aA2I0Rlsj/AkEAvrXUBQ6hfuEwD1896qpSJUr7tLidby/3jYwSoewuydDT2duDc2ZCz4/U/1NpxSxWT10ZZi2ExsFZn/3PDylczwJARA3oijkcHSUu69eybVh51bkCswnOasNHtwZxv+niWEdXhTH38EbFxcUHNaDh5MNRiwH7dobm+M7EShg8lJNHEwJAclRdU97OkFr9zeliHCGZd4P5XAFlWHfgJ7p2nR4Teqe3qZ6Aspj2qqpmnd7qxOrsn02H4YqeU+0sBs9I56T7XwJAAg8wHrh/FAPY96mAya0bpv6zm/7bave17vs+8B+fhBEHHuvetfv8Xi/RkXL0rjE4LaTHefoUbZPNbIhNYiN0CQ==",
"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCakyyq8rfkmKvKB2fz3hUeuD6tgParCmZmRc1OoL5EN+yXghQmDIrcZhewLZZLUpLQd3T3cRxKaW30nWNfoteNHgKjGYGu4+BZoyHZ8ltTmrolYGopiSwBMhO7kwAD4IK1PZHaoF2wPISH35ubbugykav7dTaWBDuNkvyBiv8qMQIDAQAB"
);
System.out.println(sm2.getPublicKeyBase64());
System.out.println(sm2.getPrivateKeyBase64());
}
}

@ -1,79 +0,0 @@
import cn.hutool.core.convert.Convert;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import lombok.Data;
import org.junit.Test;
import org.opsli.core.utils.EncryptAndDecryptByRsaUtil;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* Rsa
*/
public class RsaTest {
/**
*
*/
@Test
public void getPublicKey(){
System.out.println(EncryptAndDecryptByRsaUtil.INSTANCE.getPublicKey());
}
/**
*
*/
@Test
public void test(){
Map<String, Object> map = Maps.newHashMap();
map.put("test1", 123);
map.put("test2", "aaa");
// Test1 t1 = new Test1();
// t1.setId( "123");
// t1.setName( "张三");
// t1.setAge( 16);
List<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(111);
Object parse = JSONObject.parse("{\"username\":\"demo\",\"password\":\"Aa123456\",\"captcha\":\"\",\"uuid\":\"0d3eea43edf19e4ed0e88aae8d56878046a5\"}");
// 加密
String encryptedData = EncryptAndDecryptByRsaUtil.INSTANCE.encryptedData(list);
System.out.println(encryptedData);
// 解密
String decryptedData = EncryptAndDecryptByRsaUtil.INSTANCE.decryptedData(encryptedData);
Object decryptedDataToObj = EncryptAndDecryptByRsaUtil.INSTANCE.decryptedDataToObj(encryptedData);
System.out.println(decryptedData);
// 解密
List<Integer> integers = Convert.toList(Integer.class, decryptedDataToObj);
//Map<String, Object> stringObjectMap = Convert.toMap(String.class, Object.class, decryptedData);
//Map<String, Object> stringObjectMap = Convert.toMap(String.class, Object.class, decryptedDataToObj);
System.out.println(123);
}
}
@Data
class Test1 {
private String id;
private String name;
private Integer age;
}

@ -42,6 +42,7 @@ import org.springframework.web.multipart.MultipartHttpServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
import java.util.List;
/**
@ -227,4 +228,15 @@ public class SysOptionsRestController extends BaseRestController<SysOptions, Opt
WrapperUtil.transformInstance(option, OptionsModel.class)
);
}
/**
*
* @return ResultVo
*/
@Override
public ResultVo<List<OptionsModel>> findAll() {
return ResultVo.success(
WrapperUtil.transformInstance(IService.findAllList(), OptionsModel.class)
);
}
}

Loading…
Cancel
Save