mirror of
http://git.pig4cloud.com/pig/pigx.git
synced 2026-11-08 20:49:00 +08:00
♻️ Refactoring code. common-encry-api 支持国密算法
(cherry picked from commit 1891666cfe)
This commit is contained in:
1 parent
0d8e744621
commit
1fcc80846d
9 files changed
+111
-25
No files matched your search
+22
@@ -0,0 +1,22 @@
|
||||
package com.pig4cloud.pigx.common.api.encrypt.annotation.crypto;
|
||||
|
||||
import com.pig4cloud.pigx.common.api.encrypt.annotation.decrypt.ApiDecrypt;
|
||||
import com.pig4cloud.pigx.common.api.encrypt.annotation.encrypt.ApiEncrypt;
|
||||
import com.pig4cloud.pigx.common.api.encrypt.enums.EncryptType;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* Sm4加密解密注解
|
||||
*
|
||||
* @author lengleng
|
||||
*/
|
||||
@Target({ ElementType.TYPE, ElementType.METHOD })
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
@Inherited
|
||||
@ApiEncrypt(EncryptType.SM4)
|
||||
@ApiDecrypt(EncryptType.SM4)
|
||||
public @interface ApiCryptoSm4 {
|
||||
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
package com.pig4cloud.pigx.common.api.encrypt.annotation.decrypt;
|
||||
|
||||
import com.pig4cloud.pigx.common.api.encrypt.enums.EncryptType;
|
||||
import org.springframework.core.annotation.AliasFor;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* sm4 解密
|
||||
*
|
||||
* @author lengleng
|
||||
* @version 2023/8/30
|
||||
* @see ApiDecrypt
|
||||
*/
|
||||
@Target({ ElementType.TYPE, ElementType.METHOD, ElementType.PARAMETER })
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
@ApiDecrypt(EncryptType.SM4)
|
||||
public @interface ApiDecryptSm4 {
|
||||
|
||||
/**
|
||||
* Alias for {@link ApiDecrypt#secretKey()}.
|
||||
* @return {String}
|
||||
*/
|
||||
@AliasFor(annotation = ApiDecrypt.class)
|
||||
String secretKey() default "";
|
||||
|
||||
}
|
||||
+1
-1
@@ -6,7 +6,7 @@ import org.springframework.core.annotation.AliasFor;
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* des 加埋
|
||||
* des 加密
|
||||
*
|
||||
* @author licoy.cn
|
||||
* @author L.cm
|
||||
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
package com.pig4cloud.pigx.common.api.encrypt.annotation.encrypt;
|
||||
|
||||
import com.pig4cloud.pigx.common.api.encrypt.enums.EncryptType;
|
||||
import org.springframework.core.annotation.AliasFor;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* sm4 加密
|
||||
*
|
||||
* @author lengleng
|
||||
* @version 2023/8/30
|
||||
* @see ApiEncrypt
|
||||
*/
|
||||
@Target({ ElementType.METHOD, ElementType.TYPE })
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
@ApiEncrypt(EncryptType.SM4)
|
||||
public @interface ApiEncryptSm4 {
|
||||
|
||||
/**
|
||||
* Alias for {@link ApiEncrypt#secretKey()}.
|
||||
* @return {String}
|
||||
*/
|
||||
@AliasFor(annotation = ApiEncrypt.class)
|
||||
String secretKey() default "";
|
||||
|
||||
}
|
||||
+5
@@ -46,6 +46,11 @@ public class ApiEncryptProperties {
|
||||
*/
|
||||
private String desKey;
|
||||
|
||||
/**
|
||||
* sm4 密钥
|
||||
*/
|
||||
private String sm4Key;
|
||||
|
||||
/**
|
||||
* rsa 私钥
|
||||
*/
|
||||
|
||||
+6
-8
@@ -18,14 +18,12 @@ public class DefaultSecretKeyResolver implements ISecretKeyResolver {
|
||||
|
||||
@Override
|
||||
public String getSecretKey(HttpServletRequest request, EncryptType encryptType) {
|
||||
switch (encryptType) {
|
||||
case DES:
|
||||
return properties.getDesKey();
|
||||
case RSA:
|
||||
return properties.getRsaPrivateKey();
|
||||
default:
|
||||
return properties.getAesKey();
|
||||
}
|
||||
return switch (encryptType) {
|
||||
case SM4 -> properties.getSm4Key();
|
||||
case DES -> properties.getDesKey();
|
||||
case RSA -> properties.getRsaPrivateKey();
|
||||
default -> properties.getAesKey();
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
+1
-1
@@ -14,6 +14,6 @@ public enum EncryptType {
|
||||
/**
|
||||
* 加密方式
|
||||
*/
|
||||
DES, AES, RSA
|
||||
DES, AES, RSA, SM4
|
||||
|
||||
}
|
||||
+17
-4
@@ -1,9 +1,11 @@
|
||||
package com.pig4cloud.pigx.common.api.encrypt.util;
|
||||
|
||||
import cn.hutool.core.util.HexUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.crypto.Mode;
|
||||
import cn.hutool.crypto.Padding;
|
||||
import cn.hutool.crypto.SecureUtil;
|
||||
import cn.hutool.crypto.SmUtil;
|
||||
import cn.hutool.crypto.asymmetric.KeyType;
|
||||
import cn.hutool.crypto.symmetric.AES;
|
||||
import com.pig4cloud.pigx.common.api.encrypt.annotation.decrypt.ApiDecrypt;
|
||||
@@ -98,8 +100,12 @@ public class ApiCryptoUtil {
|
||||
return aes.encryptBase64(jsonData);
|
||||
}
|
||||
if (type == EncryptType.RSA) {
|
||||
return SecureUtil.rsa(secretKey.getBytes(StandardCharsets.UTF_8), null).encryptBase64(jsonData,
|
||||
KeyType.PrivateKey);
|
||||
return SecureUtil.rsa(secretKey.getBytes(StandardCharsets.UTF_8), null)
|
||||
.encryptBase64(jsonData, KeyType.PrivateKey);
|
||||
}
|
||||
|
||||
if (type == EncryptType.SM4) {
|
||||
return SmUtil.sm4(HexUtil.decodeHex(secretKey)).encryptHex(jsonData);
|
||||
}
|
||||
throw new EncryptBodyFailException();
|
||||
}
|
||||
@@ -134,9 +140,16 @@ public class ApiCryptoUtil {
|
||||
return SecureUtil.des(secretKey.getBytes(StandardCharsets.UTF_8)).decrypt(bodyData);
|
||||
}
|
||||
if (type == EncryptType.RSA) {
|
||||
return SecureUtil.rsa(secretKey.getBytes(StandardCharsets.UTF_8), null).decrypt(bodyData,
|
||||
KeyType.PrivateKey);
|
||||
return SecureUtil.rsa(secretKey.getBytes(StandardCharsets.UTF_8), null)
|
||||
.decrypt(bodyData, KeyType.PrivateKey);
|
||||
}
|
||||
|
||||
if (type == EncryptType.SM4) {
|
||||
return SmUtil.sm4(HexUtil.decodeHex(secretKey))
|
||||
.decryptStr(StrUtil.str(bodyData, Charset.defaultCharset()))
|
||||
.getBytes();
|
||||
}
|
||||
|
||||
throw new EncryptMethodNotFoundException();
|
||||
}
|
||||
|
||||
|
||||
+3
-11
@@ -18,10 +18,9 @@
|
||||
package com.pig4cloud.pigx.gateway.filter;
|
||||
|
||||
import cn.hutool.core.util.CharsetUtil;
|
||||
import cn.hutool.core.util.HexUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.crypto.Mode;
|
||||
import cn.hutool.crypto.Padding;
|
||||
import cn.hutool.crypto.symmetric.AES;
|
||||
import cn.hutool.crypto.SmUtil;
|
||||
import cn.hutool.http.HttpUtil;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
@@ -54,8 +53,6 @@ import org.springframework.web.server.ServerWebExchange;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import javax.crypto.spec.IvParameterSpec;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -159,15 +156,10 @@ public class PasswordDecoderFilter extends AbstractGatewayFilterFactory {
|
||||
*/
|
||||
private Function decryptAES() {
|
||||
return s -> {
|
||||
// 构建前端对应解密AES 因子
|
||||
AES aes = new AES(Mode.CFB, Padding.NoPadding,
|
||||
new SecretKeySpec(gatewayConfig.getEncodeKey().getBytes(), KEY_ALGORITHM),
|
||||
new IvParameterSpec(gatewayConfig.getEncodeKey().getBytes()));
|
||||
|
||||
// 获取请求密码并解密
|
||||
Map<String, String> inParamsMap = HttpUtil.decodeParamMap((String) s, CharsetUtil.CHARSET_UTF_8);
|
||||
if (inParamsMap.containsKey(PASSWORD)) {
|
||||
String password = aes.decryptStr(inParamsMap.get(PASSWORD));
|
||||
String password = SmUtil.sm4(HexUtil.decodeHex(gatewayConfig.getEncodeKey())).decryptStr(inParamsMap.get(PASSWORD));
|
||||
// 返回修改后报文字符
|
||||
inParamsMap.put(PASSWORD, password);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user