|
@@ -23,53 +23,82 @@ public class EncryptionUtils {
|
|
|
} catch (Exception e) {
|
|
|
//
|
|
|
}
|
|
|
+ // 检测是用token/固定key加密的情况
|
|
|
+ int enCount = 0;
|
|
|
+ int tokenCount = 0;
|
|
|
+ int keyCount = 0;
|
|
|
for (Field field : fields) {
|
|
|
if (field.isAnnotationPresent(EncryptionV.class)) {
|
|
|
field.setAccessible(true);
|
|
|
Object attObj = null;
|
|
|
try {
|
|
|
attObj = field.get(t);
|
|
|
- } catch (Exception e) {
|
|
|
- //
|
|
|
- }
|
|
|
+ } catch (Exception ignored) {}
|
|
|
if (ObjectUtils.isNotEmpty(attObj)) {
|
|
|
String beforeSet = attObj.toString();
|
|
|
if (beforeSet.contains("✱")) {
|
|
|
throw new ServiceException("s✱"+field.getAnnotation(ApiModelProperty.class).value() + "属性包含非法字符,请修改后提交"+"e✱");
|
|
|
} else {
|
|
|
- // 先根据token进行一次解密 如果解密成功了说明传过来的数据是这次取出来的 这种情况根据token解密处理后 再加密存储
|
|
|
+ enCount ++;
|
|
|
String beforeSetEnToken = null;
|
|
|
try {
|
|
|
beforeSetEnToken = SecurityUtils.sm4decrypt_ECB(SecurityUtils.getEncryptKey(), beforeSet);
|
|
|
- } catch(Exception e) {
|
|
|
- //
|
|
|
- }
|
|
|
+ } catch(Exception ignored) {}
|
|
|
String beforeSetEnKey = null;
|
|
|
try {
|
|
|
beforeSetEnKey = SecurityUtils.sm4decrypt_ECB(key, beforeSet);
|
|
|
- } catch(Exception e) {
|
|
|
- //
|
|
|
- }
|
|
|
- if (StringUtils.isNotEmpty(beforeSetEnToken) && StringUtils.isEmpty(beforeSetEnKey)) {
|
|
|
- if((java.nio.charset.Charset.forName("GBK").newEncoder().canEncode(beforeSetEnToken)) && isMessyCode(beforeSetEnToken)){
|
|
|
- beforeSet = beforeSetEnToken;
|
|
|
- }
|
|
|
- } else if (StringUtils.isNotEmpty(beforeSetEnKey) && StringUtils.isEmpty(beforeSetEnToken)) {
|
|
|
- if((java.nio.charset.Charset.forName("GBK").newEncoder().canEncode(beforeSetEnKey)) && isMessyCode(beforeSetEnKey)){
|
|
|
- beforeSet = beforeSetEnKey;
|
|
|
- }
|
|
|
- } else if (StringUtils.isNotEmpty(beforeSetEnToken) && StringUtils.isNotEmpty(beforeSetEnKey)) {
|
|
|
+ } catch(Exception ignored) {}
|
|
|
+ if (StringUtils.isNotEmpty(beforeSetEnToken)) {
|
|
|
if((java.nio.charset.Charset.forName("GBK").newEncoder().canEncode(beforeSetEnToken)) && isMessyCode(beforeSetEnToken)){
|
|
|
- beforeSet = beforeSetEnToken;
|
|
|
+ tokenCount ++;
|
|
|
}
|
|
|
+ }
|
|
|
+ if (StringUtils.isNotEmpty(beforeSetEnKey)) {
|
|
|
if((java.nio.charset.Charset.forName("GBK").newEncoder().canEncode(beforeSetEnKey)) && isMessyCode(beforeSetEnKey)){
|
|
|
- beforeSet = beforeSetEnKey;
|
|
|
+ keyCount ++;
|
|
|
}
|
|
|
}
|
|
|
- try {
|
|
|
- field.set(t, SecurityUtils.sm4encrypt_ECB(key, beforeSet));
|
|
|
- } catch (Exception e) {
|
|
|
- //
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (enCount > 0) {
|
|
|
+ // 存在加密且有值的字段
|
|
|
+ String status = "none";
|
|
|
+ if (enCount == tokenCount) {
|
|
|
+ // token 加密情况
|
|
|
+ status = "token";
|
|
|
+ } else if (enCount == keyCount) {
|
|
|
+ // 固定key 加密情况
|
|
|
+ status = "key";
|
|
|
+ }
|
|
|
+ for (Field field : fields) {
|
|
|
+ if (field.isAnnotationPresent(EncryptionV.class)) {
|
|
|
+ field.setAccessible(true);
|
|
|
+ Object attObj = null;
|
|
|
+ try {
|
|
|
+ attObj = field.get(t);
|
|
|
+ } catch (Exception e) {
|
|
|
+ //
|
|
|
+ }
|
|
|
+ if (ObjectUtils.isNotEmpty(attObj)) {
|
|
|
+ String beforeSet = attObj.toString();
|
|
|
+ if (beforeSet.contains("✱")) {
|
|
|
+ throw new ServiceException("s✱"+field.getAnnotation(ApiModelProperty.class).value() + "属性包含非法字符,请修改后提交"+"e✱");
|
|
|
+ } else {
|
|
|
+ if (StringUtils.equals(status, "token")) {
|
|
|
+ try {
|
|
|
+ beforeSet = SecurityUtils.sm4decrypt_ECB(SecurityUtils.getEncryptKey(), beforeSet);
|
|
|
+ } catch (Exception ignored) {}
|
|
|
+ } else if (StringUtils.equals(status, "key")) {
|
|
|
+ try {
|
|
|
+ beforeSet = SecurityUtils.sm4decrypt_ECB(key, beforeSet);
|
|
|
+ } catch (Exception ignored) {}
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ field.set(t, SecurityUtils.sm4encrypt_ECB(key, beforeSet));
|
|
|
+ } catch (Exception ignored) {}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -209,6 +238,45 @@ public class EncryptionUtils {
|
|
|
// }
|
|
|
// }
|
|
|
|
|
|
+ public static <T> void decryptCommon(T t) {
|
|
|
+ Class<?> bean;
|
|
|
+ Field[] fields = new Field[0];
|
|
|
+ try {
|
|
|
+ bean = t.getClass();
|
|
|
+ fields = bean.getDeclaredFields();
|
|
|
+ } catch (Exception e) {
|
|
|
+ //
|
|
|
+ }
|
|
|
+ for (Field field : fields) {
|
|
|
+ if (field.isAnnotationPresent(EncryptionV.class)) {
|
|
|
+ field.setAccessible(true);
|
|
|
+ Object attObj = null;
|
|
|
+ try {
|
|
|
+ attObj = field.get(t);
|
|
|
+ } catch (Exception e) {
|
|
|
+ //
|
|
|
+ }
|
|
|
+ if (ObjectUtils.isNotEmpty(attObj)) {
|
|
|
+ String beforeSet = attObj.toString();
|
|
|
+ if (beforeSet.contains("✱")) {
|
|
|
+ throw new ServiceException("s✱"+field.getAnnotation(ApiModelProperty.class).value() + "属性包含非法字符,请修改后提交"+"e✱");
|
|
|
+ } else {
|
|
|
+ try {
|
|
|
+ String en = SecurityUtils.sm4decrypt_ECB(key, beforeSet);
|
|
|
+ if (StringUtils.isNotEmpty(en)) {
|
|
|
+ if (java.nio.charset.Charset.forName("GBK").newEncoder().canEncode(en)) {
|
|
|
+ field.set(t, en);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ //
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
private static boolean isMessyCode(String fileName) {
|
|
|
try {
|
|
|
for (int i = fileName.length(); --i >= 0; ) {
|