parent
554cc51ec3
commit
2b0b8ae0a9
|
@ -19,3 +19,9 @@ permission.admin-users=admin
|
||||||
# 认证配置
|
# 认证配置
|
||||||
auth.verify-request-token=true
|
auth.verify-request-token=true
|
||||||
auth.token-timeout-minute=30
|
auth.token-timeout-minute=30
|
||||||
|
|
||||||
|
# 安全配置
|
||||||
|
crypto.security-protocol-type=0
|
||||||
|
crypto.aes-key="hkoUV5ZWh0q1jSxMnpjovVn19Qg99HY6DD40"
|
||||||
|
crypto.des-key="P3mq9iSIvQcvfyfdWR8sAnfAadO"
|
||||||
|
|
||||||
|
|
6
pom.xml
6
pom.xml
|
@ -201,6 +201,12 @@
|
||||||
<artifactId>ipaddress</artifactId>
|
<artifactId>ipaddress</artifactId>
|
||||||
<version>5.2.1</version>
|
<version>5.2.1</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.jetbrains</groupId>
|
||||||
|
<artifactId>annotations</artifactId>
|
||||||
|
<version>20.0.0</version>
|
||||||
|
<scope>compile</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|
|
@ -7,13 +7,21 @@ package com.dispose.common;
|
||||||
*/
|
*/
|
||||||
public enum ProtoCryptoType {
|
public enum ProtoCryptoType {
|
||||||
/**
|
/**
|
||||||
* Crypto none proto crypto type.
|
* The Crypto none.
|
||||||
*/
|
*/
|
||||||
CRYPTO_NONE(0, "不加密"),
|
CRYPTO_NONE(0, "不加密"),
|
||||||
|
/**
|
||||||
|
* The Crypto base 64.
|
||||||
|
*/
|
||||||
CRYPTO_BASE64(1, "Base64编码"),
|
CRYPTO_BASE64(1, "Base64编码"),
|
||||||
|
/**
|
||||||
|
* The Crypto aes 256.
|
||||||
|
*/
|
||||||
CRYPTO_AES256(2, "AES256加密"),
|
CRYPTO_AES256(2, "AES256加密"),
|
||||||
CRYPTO_RSA(3, "RSA非对称加密"),
|
/**
|
||||||
CRYPTO_DES(4, "DES对称加密")
|
* The Crypto des.
|
||||||
|
*/
|
||||||
|
CRYPTO_DES(3, "DES对称加密"),
|
||||||
;
|
;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -54,6 +62,12 @@ public enum ProtoCryptoType {
|
||||||
return this.readme;
|
return this.readme;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Verify crypto valuable boolean.
|
||||||
|
*
|
||||||
|
* @param code the code
|
||||||
|
* @return the boolean
|
||||||
|
*/
|
||||||
public static boolean verifyCryptoValuable(int code) {
|
public static boolean verifyCryptoValuable(int code) {
|
||||||
return code >= CRYPTO_NONE.getCode() && code <= CRYPTO_DES.getCode();
|
return code >= CRYPTO_NONE.getCode() && code <= CRYPTO_DES.getCode();
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,23 @@
|
||||||
|
package com.dispose.common;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The type Security config value.
|
||||||
|
*
|
||||||
|
* @author <huangxin@cmhi.chinamoblie.com>
|
||||||
|
*/
|
||||||
|
public class SecurityConfigValue {
|
||||||
|
/**
|
||||||
|
* The constant AES_KEY.
|
||||||
|
*/
|
||||||
|
public static String AES_KEY = "";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The constant DES_KEY.
|
||||||
|
*/
|
||||||
|
public static String DES_KEY = "";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The constant SECURITY_PROTOCOL_TYPE.
|
||||||
|
*/
|
||||||
|
public static Integer SECURITY_PROTOCOL_TYPE = 0;
|
||||||
|
}
|
|
@ -0,0 +1,50 @@
|
||||||
|
package com.dispose.config;
|
||||||
|
|
||||||
|
import com.dispose.common.ProtoCryptoType;
|
||||||
|
import com.dispose.common.SecurityConfigValue;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import javax.annotation.PostConstruct;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The type Security configure.
|
||||||
|
*
|
||||||
|
* @author <huangxin@cmhi.chinamoblie.com>
|
||||||
|
*/
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
@Component
|
||||||
|
@ConfigurationProperties(prefix = "crypto")
|
||||||
|
@Configuration
|
||||||
|
public class SecurityConfigure {
|
||||||
|
/**
|
||||||
|
* The Aes key.
|
||||||
|
*/
|
||||||
|
private String aesKey;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The Des key.
|
||||||
|
*/
|
||||||
|
private String desKey;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The Security protocol type.
|
||||||
|
*/
|
||||||
|
private Integer securityProtocolType;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Init global value.
|
||||||
|
*/
|
||||||
|
@PostConstruct
|
||||||
|
private void initGlobalValue() {
|
||||||
|
SecurityConfigValue.AES_KEY = Optional.ofNullable(aesKey).orElse("");
|
||||||
|
SecurityConfigValue.DES_KEY = Optional.ofNullable(desKey).orElse("");
|
||||||
|
SecurityConfigValue.SECURITY_PROTOCOL_TYPE =
|
||||||
|
Optional.ofNullable(securityProtocolType).orElse(ProtoCryptoType.CRYPTO_NONE.getCode());
|
||||||
|
}
|
||||||
|
}
|
|
@ -10,6 +10,7 @@ import com.dispose.pojo.dto.protocol.base.ProtocolRespDTO;
|
||||||
import com.dispose.pojo.dto.protocol.base.ValidGroups;
|
import com.dispose.pojo.dto.protocol.base.ValidGroups;
|
||||||
import com.dispose.pojo.po.MulReturnType;
|
import com.dispose.pojo.po.MulReturnType;
|
||||||
import com.dispose.service.UserAccountService;
|
import com.dispose.service.UserAccountService;
|
||||||
|
import com.security.annotation.Decryption;
|
||||||
import io.swagger.annotations.Api;
|
import io.swagger.annotations.Api;
|
||||||
import io.swagger.annotations.ApiOperation;
|
import io.swagger.annotations.ApiOperation;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
@ -54,7 +55,7 @@ public class AuthController {
|
||||||
@PostMapping("/login")
|
@PostMapping("/login")
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
@ApiOperation("登录")
|
@ApiOperation("登录")
|
||||||
public ProtocolRespDTO<? extends BaseRespStatus> userLogin(
|
public ProtocolRespDTO<? extends BaseRespStatus> userLogin(@Decryption
|
||||||
@Validated(ValidGroups.LoginReqValid.class)
|
@Validated(ValidGroups.LoginReqValid.class)
|
||||||
@RequestBody ProtocolReqDTO<LoginReq> mr)
|
@RequestBody ProtocolReqDTO<LoginReq> mr)
|
||||||
throws NoSuchAlgorithmException {
|
throws NoSuchAlgorithmException {
|
||||||
|
|
|
@ -0,0 +1,94 @@
|
||||||
|
package com.dispose.interceptor;
|
||||||
|
|
||||||
|
import com.security.annotation.Decryption;
|
||||||
|
import com.security.protocol.DecryptRequestProtocol;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.springframework.core.MethodParameter;
|
||||||
|
import org.springframework.http.HttpInputMessage;
|
||||||
|
import org.springframework.http.converter.HttpMessageConverter;
|
||||||
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||||
|
import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdvice;
|
||||||
|
|
||||||
|
import java.lang.reflect.Type;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The type Protocol security.
|
||||||
|
*
|
||||||
|
* @author <huangxin@cmhi.chinamoblie.com>
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@RestControllerAdvice
|
||||||
|
public class ProtocolSecurity implements RequestBodyAdvice {
|
||||||
|
/**
|
||||||
|
* Supports boolean.
|
||||||
|
*
|
||||||
|
* @param methodParameter the method parameter
|
||||||
|
* @param type the type
|
||||||
|
* @param aClass the a class
|
||||||
|
* @return the boolean
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean supports(@NotNull MethodParameter methodParameter,
|
||||||
|
@NotNull Type type,
|
||||||
|
@NotNull Class<? extends HttpMessageConverter<?>> aClass) {
|
||||||
|
return methodParameter.hasParameterAnnotation(Decryption.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Before body read http input message.
|
||||||
|
*
|
||||||
|
* @param httpInputMessage the http input message
|
||||||
|
* @param methodParameter the method parameter
|
||||||
|
* @param type the type
|
||||||
|
* @param aClass the a class
|
||||||
|
* @return the http input message
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@NotNull
|
||||||
|
public HttpInputMessage beforeBodyRead(@NotNull HttpInputMessage httpInputMessage,
|
||||||
|
@NotNull MethodParameter methodParameter,
|
||||||
|
@NotNull Type type,
|
||||||
|
@NotNull Class<? extends HttpMessageConverter<?>> aClass) {
|
||||||
|
return new DecryptRequestProtocol(httpInputMessage);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle empty body object.
|
||||||
|
*
|
||||||
|
* @param o the o
|
||||||
|
* @param httpInputMessage the http input message
|
||||||
|
* @param methodParameter the method parameter
|
||||||
|
* @param type the type
|
||||||
|
* @param aClass the a class
|
||||||
|
* @return the object
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public Object handleEmptyBody(Object o,
|
||||||
|
@NotNull HttpInputMessage httpInputMessage,
|
||||||
|
@NotNull MethodParameter methodParameter,
|
||||||
|
@NotNull Type type,
|
||||||
|
@NotNull Class<? extends HttpMessageConverter<?>> aClass) {
|
||||||
|
return o;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* After body read object.
|
||||||
|
*
|
||||||
|
* @param o the o
|
||||||
|
* @param httpInputMessage the http input message
|
||||||
|
* @param methodParameter the method parameter
|
||||||
|
* @param type the type
|
||||||
|
* @param aClass the a class
|
||||||
|
* @return the object
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@NotNull
|
||||||
|
public Object afterBodyRead(@NotNull Object o,
|
||||||
|
@NotNull HttpInputMessage httpInputMessage,
|
||||||
|
@NotNull MethodParameter methodParameter,
|
||||||
|
@NotNull Type type,
|
||||||
|
@NotNull Class<? extends HttpMessageConverter<?>> aClass) {
|
||||||
|
return o;
|
||||||
|
}
|
||||||
|
}
|
|
@ -36,7 +36,7 @@ public class DeviceTaskManagerImpl implements DeviceTaskManager {
|
||||||
@Override
|
@Override
|
||||||
public boolean addDisposeDeviceTaskInfo(Long taskId, Long deviceId, Long attackTypeMask) {
|
public boolean addDisposeDeviceTaskInfo(Long taskId, Long deviceId, Long attackTypeMask) {
|
||||||
// 如果已经存在相同任务,不再添加重复任务
|
// 如果已经存在相同任务,不再添加重复任务
|
||||||
if (deviceTaskMapper.getTaskByDetails(taskId, deviceId, attackTypeMask).size() != 0) {
|
if (deviceTaskMapper.getTaskByDetails(taskId, deviceId).size() != 0) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -68,14 +68,12 @@ public interface DeviceTaskMapper {
|
||||||
/**
|
/**
|
||||||
* Gets task by details.
|
* Gets task by details.
|
||||||
*
|
*
|
||||||
* @param taskId the task id
|
* @param taskId the task id
|
||||||
* @param deviceId the device id
|
* @param deviceId the device id
|
||||||
* @param attackType the attack type
|
|
||||||
* @return the task by details
|
* @return the task by details
|
||||||
*/
|
*/
|
||||||
List<DeviceTask> getTaskByDetails(@Param("taskId") Long taskId,
|
List<DeviceTask> getTaskByDetails(@Param("taskId") Long taskId,
|
||||||
@Param("deviceId") Long deviceId,
|
@Param("deviceId") Long deviceId);
|
||||||
@Param("attackType") Long attackType);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Change task status int.
|
* Change task status int.
|
||||||
|
|
|
@ -0,0 +1,16 @@
|
||||||
|
package com.security.annotation;
|
||||||
|
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The interface Decryption.
|
||||||
|
*
|
||||||
|
* @author <huangxin@cmhi.chinamoblie.com>
|
||||||
|
*/
|
||||||
|
@Target({ElementType.PARAMETER, ElementType.METHOD})
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
public @interface Decryption {
|
||||||
|
}
|
|
@ -0,0 +1,16 @@
|
||||||
|
package com.security.annotation;
|
||||||
|
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The interface Encryption.
|
||||||
|
*
|
||||||
|
* @author <huangxin@cmhi.chinamoblie.com>
|
||||||
|
*/
|
||||||
|
@Target({ElementType.PARAMETER, ElementType.METHOD})
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
public @interface Encryption {
|
||||||
|
}
|
|
@ -0,0 +1,148 @@
|
||||||
|
package com.security.arithmetic;
|
||||||
|
|
||||||
|
import jodd.util.Base64;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
import javax.crypto.BadPaddingException;
|
||||||
|
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.
|
||||||
|
*
|
||||||
|
* @author <huangxin@cmhi.chinamoblie.com>
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
public class CryptoHelper {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The constant AES_ALGORITHM_STR.
|
||||||
|
*/
|
||||||
|
private static final String AES_ALGORITHM_STR = "AES/ECB/PKCS5Padding";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The constant DES_ALGORITHM_STR.
|
||||||
|
*/
|
||||||
|
private static final String DES_ALGORITHM_STR = "DES/ECB/PKCS5Padding";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Base 64 decryption byte [ ].
|
||||||
|
*
|
||||||
|
* @param ciphertext the ciphertext
|
||||||
|
* @return the byte [ ]
|
||||||
|
*/
|
||||||
|
public static byte[] base64Decryption(String ciphertext) {
|
||||||
|
return Base64.decode(ciphertext);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Base 64 encryption string.
|
||||||
|
*
|
||||||
|
* @param plaintext the plaintext
|
||||||
|
* @return the string
|
||||||
|
*/
|
||||||
|
public static String base64Encryption(byte[] plaintext) {
|
||||||
|
return Base64.encodeToString(plaintext);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sha 256 encryption byte [ ].
|
||||||
|
*
|
||||||
|
* @param plaintext the plaintext
|
||||||
|
* @return the byte [ ]
|
||||||
|
* @throws NoSuchAlgorithmException the no such algorithm exception
|
||||||
|
*/
|
||||||
|
public static byte[] sha256Encryption(String plaintext) throws NoSuchAlgorithmException {
|
||||||
|
MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
|
||||||
|
messageDigest.update(plaintext.getBytes(StandardCharsets.UTF_8));
|
||||||
|
return messageDigest.digest();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Aes 256 encryption byte [ ].
|
||||||
|
*
|
||||||
|
* @param plaintext the ciphertext
|
||||||
|
* @param aesKey the aes key
|
||||||
|
* @return the byte [ ]
|
||||||
|
* @throws NoSuchAlgorithmException the no such algorithm exception
|
||||||
|
* @throws NoSuchPaddingException the no such padding exception
|
||||||
|
* @throws InvalidKeyException the invalid key exception
|
||||||
|
* @throws BadPaddingException the bad padding exception
|
||||||
|
* @throws IllegalBlockSizeException the illegal block size exception
|
||||||
|
*/
|
||||||
|
public static byte[] aes256Encryption(byte[] plaintext, String aesKey) throws NoSuchAlgorithmException,
|
||||||
|
NoSuchPaddingException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
|
||||||
|
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
|
||||||
|
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
|
||||||
|
secureRandom.setSeed(sha256Encryption(aesKey));
|
||||||
|
|
||||||
|
keyGen.init(256, secureRandom);
|
||||||
|
Cipher cipher = Cipher.getInstance(AES_ALGORITHM_STR);
|
||||||
|
SecretKeySpec key = new SecretKeySpec(keyGen.generateKey().getEncoded(), "AES");
|
||||||
|
cipher.init(Cipher.ENCRYPT_MODE, key);
|
||||||
|
return cipher.doFinal(plaintext);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Aes 256 decryption byte [ ].
|
||||||
|
*
|
||||||
|
* @param ciphertext the ciphertext
|
||||||
|
* @param aesKey the aes key
|
||||||
|
* @return the byte [ ]
|
||||||
|
* @throws NoSuchAlgorithmException the no such algorithm exception
|
||||||
|
* @throws NoSuchPaddingException the no such padding exception
|
||||||
|
* @throws InvalidKeyException the invalid key exception
|
||||||
|
* @throws BadPaddingException the bad padding exception
|
||||||
|
* @throws IllegalBlockSizeException the illegal block size exception
|
||||||
|
*/
|
||||||
|
public static byte[] aes256Decryption(byte[] ciphertext, String aesKey) throws NoSuchAlgorithmException,
|
||||||
|
NoSuchPaddingException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
|
||||||
|
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
|
||||||
|
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
|
||||||
|
secureRandom.setSeed(sha256Encryption(aesKey));
|
||||||
|
|
||||||
|
keyGen.init(256, secureRandom);
|
||||||
|
Cipher cipher = Cipher.getInstance(AES_ALGORITHM_STR);
|
||||||
|
SecretKeySpec key = new SecretKeySpec(keyGen.generateKey().getEncoded(), "AES");
|
||||||
|
cipher.init(Cipher.DECRYPT_MODE, key);
|
||||||
|
return cipher.doFinal(ciphertext);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Des decryption byte [ ].
|
||||||
|
*
|
||||||
|
* @param ciphertext the ciphertext
|
||||||
|
* @param desKey the des key
|
||||||
|
* @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,
|
||||||
|
IllegalBlockSizeException {
|
||||||
|
SecureRandom sr = new SecureRandom();
|
||||||
|
DESKeySpec desKeySpec = new DESKeySpec(desKey.getBytes());
|
||||||
|
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
|
||||||
|
SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
|
||||||
|
Cipher cipher = Cipher.getInstance(DES_ALGORITHM_STR);
|
||||||
|
cipher.init(Cipher.ENCRYPT_MODE, secretKey, sr);
|
||||||
|
return cipher.doFinal(ciphertext);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,130 @@
|
||||||
|
package com.security.protocol;
|
||||||
|
|
||||||
|
import com.dispose.common.ProtoCryptoType;
|
||||||
|
import com.dispose.common.SecurityConfigValue;
|
||||||
|
import com.dispose.pojo.dto.protocol.base.ProtocolReqDTO;
|
||||||
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.security.arithmetic.CryptoHelper;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.apache.commons.io.IOUtils;
|
||||||
|
import org.jetbrains.annotations.Contract;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.springframework.http.HttpHeaders;
|
||||||
|
import org.springframework.http.HttpInputMessage;
|
||||||
|
|
||||||
|
import javax.crypto.BadPaddingException;
|
||||||
|
import javax.crypto.IllegalBlockSizeException;
|
||||||
|
import javax.crypto.NoSuchPaddingException;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.security.InvalidKeyException;
|
||||||
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
import java.security.spec.InvalidKeySpecException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The type Decrypt request protocol.
|
||||||
|
*
|
||||||
|
* @author <huangxin@cmhi.chinamoblie.com>
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
public class DecryptRequestProtocol implements HttpInputMessage {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The Object mapper.
|
||||||
|
*/
|
||||||
|
private final ObjectMapper objectMapper = new ObjectMapper();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The Input message.
|
||||||
|
*/
|
||||||
|
private final HttpInputMessage inputMessage;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Instantiates a new Decrypt request protocol.
|
||||||
|
*
|
||||||
|
* @param inputMessage the input message
|
||||||
|
*/
|
||||||
|
@Contract(pure = true)
|
||||||
|
public DecryptRequestProtocol(HttpInputMessage inputMessage) {
|
||||||
|
this.inputMessage = inputMessage;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets body.
|
||||||
|
*
|
||||||
|
* @return the body
|
||||||
|
* @throws IOException the io exception
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@NotNull
|
||||||
|
public InputStream getBody() throws IOException {
|
||||||
|
// 解密后的内容
|
||||||
|
byte[] decryptContent;
|
||||||
|
|
||||||
|
// 提取协议中的JSON字符串
|
||||||
|
String reqMessage = IOUtils.toString(inputMessage.getBody(), StandardCharsets.UTF_8);
|
||||||
|
|
||||||
|
JsonNode objRoot = objectMapper.readTree(reqMessage);
|
||||||
|
|
||||||
|
int cryptoType = objRoot.path("cryptoType").asInt();
|
||||||
|
|
||||||
|
// 协议未加密
|
||||||
|
if (cryptoType == ProtoCryptoType.CRYPTO_NONE.getCode()) {
|
||||||
|
return IOUtils.toInputStream(reqMessage, StandardCharsets.UTF_8);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 反序列化为对象处理
|
||||||
|
ProtocolReqDTO<String> proReq = objectMapper.readValue(reqMessage,
|
||||||
|
new TypeReference<ProtocolReqDTO<String>>() {
|
||||||
|
});
|
||||||
|
|
||||||
|
// 首先对加密内容进行base64解码
|
||||||
|
byte[] base64Decode = CryptoHelper.base64Decryption(proReq.getMsgContent());
|
||||||
|
|
||||||
|
// 加密类型为base64直接返回处理结果
|
||||||
|
if (proReq.getCryptoType() == ProtoCryptoType.CRYPTO_BASE64.getCode()) {
|
||||||
|
decryptContent = base64Decode;
|
||||||
|
} else if (proReq.getCryptoType() == ProtoCryptoType.CRYPTO_AES256.getCode()) {
|
||||||
|
try {
|
||||||
|
decryptContent = CryptoHelper.aes256Decryption(base64Decode, SecurityConfigValue.AES_KEY);
|
||||||
|
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | BadPaddingException
|
||||||
|
| IllegalBlockSizeException e) {
|
||||||
|
log.error("AES256 decode message error: {}", base64Decode);
|
||||||
|
decryptContent = base64Decode;
|
||||||
|
}
|
||||||
|
} else if (proReq.getCryptoType() == ProtoCryptoType.CRYPTO_DES.getCode()) {
|
||||||
|
try {
|
||||||
|
decryptContent = CryptoHelper.desDecryption(base64Decode, SecurityConfigValue.DES_KEY);
|
||||||
|
} catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeySpecException
|
||||||
|
| BadPaddingException | IllegalBlockSizeException e) {
|
||||||
|
log.error("DES256 decode message error: {}", base64Decode);
|
||||||
|
decryptContent = base64Decode;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
log.error("Unknown protocol security type: {}, {}", proReq.getCryptoType(), inputMessage.getBody());
|
||||||
|
return inputMessage.getBody();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 字节数组转换为字符串
|
||||||
|
String decodeMsg = new String(decryptContent, StandardCharsets.UTF_8);
|
||||||
|
|
||||||
|
String decodeJson = reqMessage.replace("\"" + proReq.getMsgContent() + "\"", decodeMsg);
|
||||||
|
|
||||||
|
// 返回解密后的内容
|
||||||
|
return IOUtils.toInputStream(decodeJson, StandardCharsets.UTF_8);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets headers.
|
||||||
|
*
|
||||||
|
* @return the headers
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@NotNull
|
||||||
|
public HttpHeaders getHeaders() {
|
||||||
|
return inputMessage.getHeaders();
|
||||||
|
}
|
||||||
|
}
|
|
@ -69,12 +69,8 @@
|
||||||
<select id="getTaskByDetails" resultMap="device_task">
|
<select id="getTaskByDetails" resultMap="device_task">
|
||||||
SELECT *
|
SELECT *
|
||||||
FROM device_task
|
FROM device_task
|
||||||
WHERE status != ${@com.dispose.common.DisposeTaskStatus@TASK_FINISHED.getValue()}
|
WHERE taskId = #{taskId, jdbcType=INTEGER}
|
||||||
AND status != ${@com.dispose.common.DisposeTaskStatus@TASK_CANCELED.getValue()}
|
|
||||||
AND status != ${@com.dispose.common.DisposeTaskStatus@TASK_EXPIRED.getValue()}
|
|
||||||
AND taskId = #{taskId, jdbcType=INTEGER}
|
|
||||||
AND deviceId = #{deviceId, jdbcType=INTEGER}
|
AND deviceId = #{deviceId, jdbcType=INTEGER}
|
||||||
AND taskAttackType = #{attackType, jdbcType=INTEGER}
|
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
<update id="changeTaskStatus">
|
<update id="changeTaskStatus">
|
||||||
|
|
|
@ -23,7 +23,7 @@ import javax.annotation.Resource;
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Getter
|
@Getter
|
||||||
@ActiveProfiles("local")
|
@ActiveProfiles("local,dispose")
|
||||||
public class InitTestEnvironment {
|
public class InitTestEnvironment {
|
||||||
/**
|
/**
|
||||||
* The constant HTTP_CONNECT_TIMEOUT.
|
* The constant HTTP_CONNECT_TIMEOUT.
|
||||||
|
|
|
@ -5,14 +5,26 @@ import com.dispose.common.DisposeDeviceType;
|
||||||
import com.dispose.common.HttpType;
|
import com.dispose.common.HttpType;
|
||||||
import com.dispose.common.ObjectStatus;
|
import com.dispose.common.ObjectStatus;
|
||||||
import com.dispose.common.PrivacyHelper;
|
import com.dispose.common.PrivacyHelper;
|
||||||
|
import com.dispose.common.SecurityConfigValue;
|
||||||
import com.dispose.pojo.entity.DisposeDevice;
|
import com.dispose.pojo.entity.DisposeDevice;
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.security.arithmetic.CryptoHelper;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
|
import javax.crypto.BadPaddingException;
|
||||||
|
import javax.crypto.IllegalBlockSizeException;
|
||||||
|
import javax.crypto.NoSuchPaddingException;
|
||||||
|
import java.io.UnsupportedEncodingException;
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.lang.reflect.Modifier;
|
import java.lang.reflect.Modifier;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.security.InvalidKeyException;
|
||||||
|
import java.security.NoSuchAlgorithmException;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.time.format.DateTimeFormatter;
|
import java.time.format.DateTimeFormatter;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
@ -23,6 +35,8 @@ import java.util.Optional;
|
||||||
* @author <huangxin@cmhi.chinamoblie.com>
|
* @author <huangxin@cmhi.chinamoblie.com>
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@SpringBootTest
|
||||||
public class demo {
|
public class demo {
|
||||||
// /**
|
// /**
|
||||||
// * A 1 idid array req test.
|
// * A 1 idid array req test.
|
||||||
|
@ -177,4 +191,45 @@ public class demo {
|
||||||
// }
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void aes256Encrypt() throws UnsupportedEncodingException {
|
||||||
|
byte[] aesMsg;
|
||||||
|
byte[] deAesMsg;
|
||||||
|
String srcMsg = "{\n" +
|
||||||
|
" \"password\": \"c3855e6b6bb120450f160ba91134522868f89d36062f2061ebeefd80817e1d58\",\n" +
|
||||||
|
" \"userName\": \"admin\"\n" +
|
||||||
|
"}";
|
||||||
|
|
||||||
|
try {
|
||||||
|
aesMsg = CryptoHelper.aes256Encryption(srcMsg.getBytes(StandardCharsets.UTF_8), SecurityConfigValue.AES_KEY);
|
||||||
|
deAesMsg = CryptoHelper.aes256Decryption(aesMsg, SecurityConfigValue.AES_KEY);
|
||||||
|
} catch (NoSuchAlgorithmException | NoSuchPaddingException | BadPaddingException | InvalidKeyException | IllegalBlockSizeException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
aesMsg = new byte[] {0};
|
||||||
|
deAesMsg = new byte[] {0};
|
||||||
|
}
|
||||||
|
|
||||||
|
log.info("src: {}", srcMsg);
|
||||||
|
log.info("src AES256: {}", CryptoHelper.base64Encryption(aesMsg));
|
||||||
|
|
||||||
|
log.info("src DeAES256: {}", new String(deAesMsg, StandardCharsets.UTF_8));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void aes256Decrypt() throws UnsupportedEncodingException {
|
||||||
|
byte[] deAesMsg;
|
||||||
|
String aesBase64Msg = "AiBxQak+lHi3NtmRbLhM4JLW7LTddVDGMUzcutt+Ijx2wqUWKnMVlzeqLgVSdqekQFY1gA6Fg7n16IoQmlNvsMOBilFfyiWw9Noyk21fUt57RyRADsf/ABRzzbdcqz1+Cp0/zO73gssm5+xZpwZpAg==";
|
||||||
|
byte[] base64Decode = CryptoHelper.base64Decryption(aesBase64Msg);
|
||||||
|
|
||||||
|
try {
|
||||||
|
deAesMsg = CryptoHelper.aes256Decryption(base64Decode, SecurityConfigValue.AES_KEY);
|
||||||
|
} catch (NoSuchAlgorithmException | NoSuchPaddingException | BadPaddingException | InvalidKeyException | IllegalBlockSizeException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
deAesMsg = new byte[] {0};
|
||||||
|
}
|
||||||
|
|
||||||
|
log.info("src: {}", aesBase64Msg);
|
||||||
|
log.info("src DeAES256: {}", new String(deAesMsg, StandardCharsets.UTF_8));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
package com.dispose.test.mapper;
|
package com.dispose.test.mapper;
|
||||||
|
|
||||||
import com.dispose.common.*;
|
import com.dispose.common.DisposeTaskStatus;
|
||||||
import com.dispose.mapper.DeviceTaskMapper;
|
import com.dispose.mapper.DeviceTaskMapper;
|
||||||
import com.dispose.mapper.DisposeDeviceMapper;
|
import com.dispose.mapper.DisposeDeviceMapper;
|
||||||
import com.dispose.mapper.DisposeTaskMapper;
|
import com.dispose.mapper.DisposeTaskMapper;
|
||||||
|
@ -191,7 +191,7 @@ public class DeviceTaskMapperTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
deviceTaskList.forEach(v -> {
|
deviceTaskList.forEach(v -> {
|
||||||
List<DeviceTask> detailsTask = deviceTaskMapper.getTaskByDetails(v.getTaskId(), v.getDeviceId(), v.getTaskAttackType());
|
List<DeviceTask> detailsTask = deviceTaskMapper.getTaskByDetails(v.getTaskId(), v.getDeviceId());
|
||||||
|
|
||||||
try {
|
try {
|
||||||
log.info("detailsTask: {}", objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(detailsTask));
|
log.info("detailsTask: {}", objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(detailsTask));
|
||||||
|
|
Loading…
Reference in New Issue