diff --git a/config/application-local.properties b/config/application-local.properties index bf044b25..5e8406aa 100644 --- a/config/application-local.properties +++ b/config/application-local.properties @@ -58,6 +58,7 @@ dispose.debug-model=true dispose.check-protocol-timeout=false dispose.check-request-token=true dispose.split_char=, +dispose.request-timeout-second=5 # 迪普设备配置 # 发送超时时间(ms) diff --git a/src/main/java/com/dispose/common/AuthConfigValue.java b/src/main/java/com/dispose/common/AuthConfigValue.java index 3a945fef..9adb97b3 100644 --- a/src/main/java/com/dispose/common/AuthConfigValue.java +++ b/src/main/java/com/dispose/common/AuthConfigValue.java @@ -14,7 +14,7 @@ public class AuthConfigValue { /** * The constant TOKEN_EXPIRED_TIME_MS. */ - public static long TOKEN_EXPIRED_TIME_MS = 30 * 60 * 1000; + public static volatile long TOKEN_EXPIRED_TIME_MS = 30 * 60 * 1000; /** * The constant MYSQL_REGEX. diff --git a/src/main/java/com/dispose/common/DisposeConfigValue.java b/src/main/java/com/dispose/common/DisposeConfigValue.java new file mode 100644 index 00000000..76237da2 --- /dev/null +++ b/src/main/java/com/dispose/common/DisposeConfigValue.java @@ -0,0 +1,19 @@ +package com.dispose.common; + +/** + * The type Dispose config value. + * + * @author + */ +public class DisposeConfigValue { + + /** + * The constant CHECK_PROTO_REQUEST_TIMEOUT. + */ + public static volatile boolean CHECK_PROTO_REQUEST_TIMEOUT = false; + + /** + * The constant REQUEST_TIMEOUT_MS. + */ + public static volatile long REQUEST_TIMEOUT_MS = 5 * 1000; +} diff --git a/src/main/java/com/dispose/common/ProtoCryptoType.java b/src/main/java/com/dispose/common/ProtoCryptoType.java index 834af6ec..caf28398 100644 --- a/src/main/java/com/dispose/common/ProtoCryptoType.java +++ b/src/main/java/com/dispose/common/ProtoCryptoType.java @@ -53,4 +53,8 @@ public enum ProtoCryptoType { public String getReadme() { return this.readme; } + + public static boolean verifyCryptoValuable(int code) { + return code >= CRYPTO_NONE.getCode() && code <= CRYPTO_DES.getCode(); + } } diff --git a/src/main/java/com/dispose/config/DisposeConfigure.java b/src/main/java/com/dispose/config/DisposeConfigure.java index de387571..4f5f209f 100644 --- a/src/main/java/com/dispose/config/DisposeConfigure.java +++ b/src/main/java/com/dispose/config/DisposeConfigure.java @@ -27,4 +27,9 @@ public class DisposeConfigure { * The Split char. */ private String splitChar; + + /** + * The Request timeout second. + */ + private String requestTimeoutSecond; } diff --git a/src/main/java/com/dispose/controller/AuthController.java b/src/main/java/com/dispose/controller/AuthController.java index 96f4a809..7de4d6dc 100644 --- a/src/main/java/com/dispose/controller/AuthController.java +++ b/src/main/java/com/dispose/controller/AuthController.java @@ -55,7 +55,7 @@ public class AuthController { @ResponseBody @ApiOperation("登录") public ProtocolRespDTO userLogin( - @Validated(ValidGroups.LoginReq.class) + @Validated(ValidGroups.LoginReqValid.class) @RequestBody ProtocolReqDTO mr) throws NoSuchAlgorithmException { @@ -95,7 +95,7 @@ public class AuthController { @PostMapping("/logout") @ResponseBody @ApiOperation("注销") - public ProtocolRespDTO userLogout(@Validated(ValidGroups.LogoutReq.class) + public ProtocolRespDTO userLogout(@Validated(ValidGroups.LogoutReqValid.class) @RequestBody ProtocolReqDTO mr, @RequestHeader HttpHeaders headers) { // 注销用户登录 diff --git a/src/main/java/com/dispose/pojo/dto/protocol/auth/LoginReq.java b/src/main/java/com/dispose/pojo/dto/protocol/auth/LoginReq.java index c2e2e0ee..cfe1c7f0 100644 --- a/src/main/java/com/dispose/pojo/dto/protocol/auth/LoginReq.java +++ b/src/main/java/com/dispose/pojo/dto/protocol/auth/LoginReq.java @@ -35,13 +35,13 @@ public class LoginReq { /** * The Password. */ - @NotBlank(message = "password 密码不能为空", groups = ValidGroups.LoginReq.class) + @NotBlank(message = "password 密码不能为空", groups = ValidGroups.LoginReqValid.class) @Length(min = 64, max = 64, message = "password 密码长度必须为SHA256编码后的长度", - groups = ValidGroups.LoginReq.class) + groups = ValidGroups.LoginReqValid.class) @Pattern(regexp = AuthConfigValue.MYSQL_REGEX_CHARS, flags = Pattern.Flag.CASE_INSENSITIVE, message = "password 密码存在非法字符串", - groups = ValidGroups.LoginReq.class) + groups = ValidGroups.LoginReqValid.class) private String password; } diff --git a/src/main/java/com/dispose/pojo/dto/protocol/base/BaseProtocolDTO.java b/src/main/java/com/dispose/pojo/dto/protocol/base/BaseProtocolDTO.java index 371948d7..71418463 100644 --- a/src/main/java/com/dispose/pojo/dto/protocol/base/BaseProtocolDTO.java +++ b/src/main/java/com/dispose/pojo/dto/protocol/base/BaseProtocolDTO.java @@ -26,8 +26,8 @@ public class BaseProtocolDTO { * 当前协议版本号 */ @ApiModelProperty(value = "协议版本号", required = true, example = "1") - @NotNull(message = "ver 字段不能为空", groups = ValidGroups.ProtocolCommon.class) - @Range(min = 3, max = 9999, message = "ver 字段最小值为 3", groups = ValidGroups.ProtocolCommon.class) + @NotNull(message = "ver 字段不能为空", groups = ValidGroups.ProtocolCommonValid.class) + @Range(min = 3, max = 9999, message = "ver 字段最小值为 3", groups = ValidGroups.ProtocolCommonValid.class) private Integer ver; /** @@ -39,20 +39,20 @@ public class BaseProtocolDTO { "2:采用AES加密后的base64编码格式\n", required = true, allowableValues = "0, 1, 2", example = "0") - @NotNull(message = "cryptoType 字段不能为空", groups = ValidGroups.ProtocolCommon.class) + @NotNull(message = "cryptoType 字段不能为空", groups = ValidGroups.ProtocolCommonValid.class) @Range(min = 0, max = 2, message = "cryptoType 字段取值为 [0, 2]" - , groups = ValidGroups.ProtocolCommon.class) + , groups = ValidGroups.ProtocolCommonValid.class) private Integer cryptoType; /** * 当前UTC时间戳(ms) */ @ApiModelProperty(value = "当前UTC时间戳", required = true, example = "1526625689000") - @NotNull(message = "timeStamp 字段不能为空", groups = ValidGroups.ProtocolCommon.class) + @NotNull(message = "timeStamp 字段不能为空", groups = ValidGroups.ProtocolCommonValid.class) @DecimalMin(value = "1595494343000", message = "timeStamp 字段值不能为过去时间" - , groups = ValidGroups.ProtocolCommon.class) + , groups = ValidGroups.ProtocolCommonValid.class) private Long timeStamp; /** diff --git a/src/main/java/com/dispose/pojo/dto/protocol/base/ValidGroups.java b/src/main/java/com/dispose/pojo/dto/protocol/base/ValidGroups.java index 4622d16e..accda024 100644 --- a/src/main/java/com/dispose/pojo/dto/protocol/base/ValidGroups.java +++ b/src/main/java/com/dispose/pojo/dto/protocol/base/ValidGroups.java @@ -11,7 +11,7 @@ public class ValidGroups { * * @author */ - public interface ProtocolCommon { + public interface ProtocolCommonValid { } /** @@ -19,7 +19,7 @@ public class ValidGroups { * * @author */ - public interface LoginReq extends ProtocolCommon { + public interface LoginReqValid extends ProtocolCommonValid { } /** @@ -27,6 +27,6 @@ public class ValidGroups { * * @author */ - public interface LogoutReq extends ProtocolCommon { + public interface LogoutReqValid extends ProtocolCommonValid { } } diff --git a/src/main/java/com/dispose/setup/SystemInitial.java b/src/main/java/com/dispose/setup/SystemInitial.java index f94b57a0..bdeba585 100644 --- a/src/main/java/com/dispose/setup/SystemInitial.java +++ b/src/main/java/com/dispose/setup/SystemInitial.java @@ -1,6 +1,7 @@ package com.dispose.setup; import com.dispose.common.AuthConfigValue; +import com.dispose.common.DisposeConfigValue; import com.dispose.common.DpTechConfigValue; import com.dispose.config.AuthConfigure; import com.dispose.config.DisposeConfigure; @@ -59,12 +60,18 @@ public class SystemInitial implements CommandLineRunner { } catch(Exception ex) { log.error("load SOAP_RECEIVE_TIMEOUT_SECOND configure error: {}", ex.getMessage()); } -// -// try { -// GlobalVar.IS_CHECK_REQUEST_TIMEOUT = Boolean.parseBoolean(disposeConfigure.getCheckProtocolTimeout()); -// } catch(Exception ex) { -// log.error("load IS_CHECK_REQUEST_TIMEOUT configure error: {}", ex.getMessage()); -// } + + try { + DisposeConfigValue.REQUEST_TIMEOUT_MS = Integer.parseInt(disposeConfigure.getRequestTimeoutSecond()) * 1000; + } catch(Exception ex) { + log.error("load REQUEST_TIMEOUT_MS configure error: {}", ex.getMessage()); + } + + try { + DisposeConfigValue.CHECK_PROTO_REQUEST_TIMEOUT = Boolean.parseBoolean(disposeConfigure.getCheckProtocolTimeout()); + } catch(Exception ex) { + log.error("load CHECK_PROTO_REQUEST_TIMEOUT configure error: {}", ex.getMessage()); + } } /** diff --git a/src/test/java/com/dispose/test/Global/InitTestEnvironment.java b/src/test/java/com/dispose/test/Global/InitTestEnvironment.java index 50f33419..aa440aa4 100644 --- a/src/test/java/com/dispose/test/Global/InitTestEnvironment.java +++ b/src/test/java/com/dispose/test/Global/InitTestEnvironment.java @@ -1,6 +1,10 @@ package com.dispose.test.Global; +import com.dispose.common.ConstValue; +import com.dispose.common.DisposeConfigValue; import com.dispose.common.ErrorCode; +import com.dispose.common.ProtoCryptoType; +import com.dispose.pojo.dto.protocol.base.ProtocolRespDTO; import com.dispose.pojo.po.MulReturnType; import com.dispose.service.UserAccountService; import lombok.Getter; @@ -14,13 +18,15 @@ import javax.annotation.Resource; /** * The type Init test environment. + * + * @author */ @Slf4j @Getter @ActiveProfiles("local") public class InitTestEnvironment { /** - * The connect timeout. + * The constant HTTP_CONNECT_TIMEOUT. */ public static final int HTTP_CONNECT_TIMEOUT = 10000; /** @@ -64,4 +70,43 @@ public class InitTestEnvironment { Assert.assertNotNull(logToken); Assert.assertNotEquals(logToken.length(), 0); } + + /** + * Verify resp protocol. + * + * @param the type parameter + * @param proObject the pro object + */ + public void verifyRespProtocol(ProtocolRespDTO proObject) { + try { + + // 检查协议版本 + if (proObject.getVer() < ConstValue.Protocol.VERSION) { + log.error("Protocol version: {} need {}", proObject.getVer(), ConstValue.Protocol.VERSION); + Assert.fail(); + } + + // 检查请求是否超时 + if (DisposeConfigValue.CHECK_PROTO_REQUEST_TIMEOUT) { + Assert.assertFalse(Math.abs(System.currentTimeMillis() - proObject.getTimeStamp()) + >= DisposeConfigValue.REQUEST_TIMEOUT_MS); + } + + // 检查加密方式 + Assert.assertTrue(ProtoCryptoType.verifyCryptoValuable(proObject.getCryptoType())); + + } catch (Exception ex) { + log.error("Protocol Exception: {}", ex.getMessage()); + Assert.fail(); + } + } + + /** + * Gets login token. + * + * @return the login token + */ + public String getLoginToken() { + return logToken; + } }