REM:
1. 删除无用代码
2. 增加Mock请求协议返回时拦截器标记
3. 修正DES算法异常问题
This commit is contained in:
HuangXin 2020-09-15 14:25:57 +08:00
parent c13c6a3c1e
commit 20424c9fc9
4 changed files with 26 additions and 32 deletions

View File

@ -46,4 +46,6 @@ public class DisposeConfigValue {
* The constant MIN_SPLIT_PAGE_SIZE.
*/
public static volatile int MIN_SPLIT_PAGE_SIZE = 10;
public static volatile boolean ENABLE_UTEST_MOCK = false;
}

View File

@ -1,5 +1,6 @@
package com.dispose.interceptor;
import com.dispose.common.DisposeConfigValue;
import com.dispose.common.ProtoCryptoType;
import com.dispose.common.SecurityConfigValue;
import com.dispose.pojo.dto.protocol.base.ProtocolRespDTO;
@ -42,6 +43,11 @@ public class ResponseProtocolSecurity implements ResponseBodyAdvice<Object> {
@Override
public boolean supports(@NotNull MethodParameter methodParameter,
@NotNull Class<? extends HttpMessageConverter<?>> aClass) {
// 单元测试Mock模式
if (DisposeConfigValue.ENABLE_UTEST_MOCK) {
return true;
}
return methodParameter.getContainingClass().isAnnotationPresent(Encryption.class)
|| methodParameter.hasMethodAnnotation(Encryption.class);
}

View File

@ -4,8 +4,6 @@ import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Arrays;
/**
* The type Response status.
*
@ -24,14 +22,4 @@ public class BaseRespStatus {
* 登录消息: status状态码对应的提示信息
*/
private String[] message;
/**
* To string string.
*
* @return the string
*/
@Override
public String toString() {
return "{\"status\":" + status + ", \"message\":\"" + Arrays.toString(message) + "\"}";
}
}

View File

@ -8,16 +8,12 @@ import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;
/**
* The type Crypto helper.
@ -71,7 +67,7 @@ public class CryptoHelper {
}
/**
* Aes 256 encryption byte [ ].
* Aes 128 encryption byte [ ].
*
* @param plaintext the plaintext
* @param aesKey the aes key
@ -96,7 +92,7 @@ public class CryptoHelper {
}
/**
* Aes 256 decryption byte [ ].
* Aes 128 decryption byte [ ].
*
* @param ciphertext the ciphertext
* @param aesKey the aes key
@ -129,20 +125,21 @@ public class CryptoHelper {
* @return the byte [ ]
* @throws InvalidKeyException the invalid key exception
* @throws NoSuchAlgorithmException the no such algorithm exception
* @throws InvalidKeySpecException the invalid key spec exception
* @throws NoSuchPaddingException the no such padding exception
* @throws BadPaddingException the bad padding exception
* @throws IllegalBlockSizeException the illegal block size exception
*/
public static byte[] desDecryption(byte[] ciphertext, String desKey) throws InvalidKeyException,
NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, BadPaddingException,
NoSuchAlgorithmException, NoSuchPaddingException, BadPaddingException,
IllegalBlockSizeException {
SecureRandom sr = new SecureRandom();
DESKeySpec desKeySpec = new DESKeySpec(desKey.getBytes());
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
KeyGenerator keyGen = KeyGenerator.getInstance("DES");
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(desKey.getBytes());
keyGen.init(56, secureRandom);
Cipher cipher = Cipher.getInstance(DES_ALGORITHM_STR);
cipher.init(Cipher.ENCRYPT_MODE, secretKey, sr);
SecretKeySpec key = new SecretKeySpec(keyGen.generateKey().getEncoded(), "DES");
cipher.init(Cipher.DECRYPT_MODE, key);
return cipher.doFinal(ciphertext);
}
@ -154,20 +151,21 @@ public class CryptoHelper {
* @return the byte [ ]
* @throws InvalidKeyException the invalid key exception
* @throws NoSuchAlgorithmException the no such algorithm exception
* @throws InvalidKeySpecException the invalid key spec exception
* @throws NoSuchPaddingException the no such padding exception
* @throws BadPaddingException the bad padding exception
* @throws IllegalBlockSizeException the illegal block size exception
*/
public static byte[] desEncryption(byte[] plaintext, String desKey) throws InvalidKeyException,
NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, BadPaddingException,
NoSuchAlgorithmException, NoSuchPaddingException, BadPaddingException,
IllegalBlockSizeException {
SecureRandom sr = new SecureRandom();
DESKeySpec desKeySpec = new DESKeySpec(desKey.getBytes());
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
KeyGenerator keyGen = KeyGenerator.getInstance("DES");
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(desKey.getBytes());
keyGen.init(56, secureRandom);
Cipher cipher = Cipher.getInstance(DES_ALGORITHM_STR);
cipher.init(Cipher.DECRYPT_MODE, secretKey, sr);
SecretKeySpec key = new SecretKeySpec(keyGen.generateKey().getEncoded(), "DES");
cipher.init(Cipher.ENCRYPT_MODE, key);
return cipher.doFinal(plaintext);
}
}