parent
1d100a25b6
commit
c0754981d1
|
@ -61,7 +61,7 @@ public class DisposeConfigure {
|
||||||
*/
|
*/
|
||||||
@PostConstruct
|
@PostConstruct
|
||||||
private void initGlobalValue() {
|
private void initGlobalValue() {
|
||||||
DisposeConfigValue.REQUEST_TIMEOUT_MS = Optional.ofNullable(requestTimeoutSecond).orElse((long) 5 * 1000);
|
DisposeConfigValue.REQUEST_TIMEOUT_MS = Optional.ofNullable(requestTimeoutSecond).orElse((long) 5) * 1000;
|
||||||
DisposeConfigValue.CHECK_PROTO_REQUEST_TIMEOUT = Optional.ofNullable(checkProtocolTimeout).orElse(true);
|
DisposeConfigValue.CHECK_PROTO_REQUEST_TIMEOUT = Optional.ofNullable(checkProtocolTimeout).orElse(true);
|
||||||
DisposeConfigValue.USED_PRIVACY_PROTECT = Optional.ofNullable(usedPrivacyProtect).orElse(true);
|
DisposeConfigValue.USED_PRIVACY_PROTECT = Optional.ofNullable(usedPrivacyProtect).orElse(true);
|
||||||
DisposeConfigValue.CALL_ERROR_RETRY_TIMES = Optional.ofNullable(callErrorRetryTimes).orElse(5);
|
DisposeConfigValue.CALL_ERROR_RETRY_TIMES = Optional.ofNullable(callErrorRetryTimes).orElse(5);
|
||||||
|
|
|
@ -0,0 +1,45 @@
|
||||||
|
package com.dispose.exception;
|
||||||
|
|
||||||
|
import com.dispose.common.ErrorCode;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The type Controller request timeout exception.
|
||||||
|
*
|
||||||
|
* @author <huangxin@cmhi.chinamoblie.com>
|
||||||
|
*/
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
public class ControllerRequestTimeoutException extends RuntimeException {
|
||||||
|
/**
|
||||||
|
* The Err.
|
||||||
|
*/
|
||||||
|
private ErrorCode err;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The Message.
|
||||||
|
*/
|
||||||
|
private String message;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Instantiates a new Controller request timeout exception.
|
||||||
|
*
|
||||||
|
* @param err the err
|
||||||
|
* @param msg the msg
|
||||||
|
*/
|
||||||
|
public ControllerRequestTimeoutException(ErrorCode err, String msg) {
|
||||||
|
super();
|
||||||
|
this.err = err;
|
||||||
|
this.message = err.getMsg() + ", " + msg;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets exception message.
|
||||||
|
*
|
||||||
|
* @return the exception message
|
||||||
|
*/
|
||||||
|
public String getExceptionMessage() {
|
||||||
|
return this.message;
|
||||||
|
}
|
||||||
|
}
|
|
@ -7,6 +7,7 @@ import com.dispose.pojo.dto.protocol.base.BaseRespStatus;
|
||||||
import com.dispose.pojo.dto.protocol.base.ProtocolRespDTO;
|
import com.dispose.pojo.dto.protocol.base.ProtocolRespDTO;
|
||||||
import com.dispose.security.exception.SecurityProtocolException;
|
import com.dispose.security.exception.SecurityProtocolException;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.validation.ObjectError;
|
||||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||||
|
@ -47,11 +48,29 @@ public class GlobalExceptionHandler {
|
||||||
if (reqToken != null && reqToken.length() > 0) {
|
if (reqToken != null && reqToken.length() > 0) {
|
||||||
reqToken = reqToken.replace(ConstValue.STRING_HTTP_AUTH_HEAD, "");
|
reqToken = reqToken.replace(ConstValue.STRING_HTTP_AUTH_HEAD, "");
|
||||||
}
|
}
|
||||||
log.info("Interface [{}] request <{}> from {}, token = <{}>\n" +
|
|
||||||
"+++ Request: {}\n" +
|
StringBuilder sb = new StringBuilder("[");
|
||||||
"--- Verify params failed: {}",
|
|
||||||
reqType, reqPath, reqIp, reqToken, Helper.inputStream2String(req.getInputStream()),
|
List<ObjectError> errList = ex.getBindingResult().getAllErrors();
|
||||||
ex.getMessage());
|
|
||||||
|
for (int i = 0; i < errList.size(); i++) {
|
||||||
|
log.debug("Verify params error {}: {}", i, errList.get(i));
|
||||||
|
sb.append("\"")
|
||||||
|
.append(i)
|
||||||
|
.append(": ")
|
||||||
|
.append(errList.get(i).getDefaultMessage())
|
||||||
|
.append("\"");
|
||||||
|
if (i < errList.size() - 1) {
|
||||||
|
sb.append(", ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sb.append("]");
|
||||||
|
|
||||||
|
log.error("Interface [{}] request <{}> from {}, token = <{}>\n" +
|
||||||
|
"+++ Request: {}\n" +
|
||||||
|
"--- Verify params failed: {}",
|
||||||
|
reqType, reqPath, reqIp, reqToken, Helper.inputStream2String(req.getInputStream()),
|
||||||
|
sb.toString());
|
||||||
} catch (Exception ignored) {
|
} catch (Exception ignored) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -93,13 +112,14 @@ public class GlobalExceptionHandler {
|
||||||
if (reqToken != null && reqToken.length() > 0) {
|
if (reqToken != null && reqToken.length() > 0) {
|
||||||
reqToken = reqToken.replace(ConstValue.STRING_HTTP_AUTH_HEAD, "");
|
reqToken = reqToken.replace(ConstValue.STRING_HTTP_AUTH_HEAD, "");
|
||||||
}
|
}
|
||||||
log.info("Interface [{}] request <{}> from {}, token = <{}>\n" +
|
log.error("Interface [{}] request <{}> from {}, token = <{}>\n" +
|
||||||
"+++ Request: {}\n" +
|
"+++ Request: {}\n" +
|
||||||
"--- Verify params failed: {}",
|
"--- Verify params failed: {}",
|
||||||
reqType, reqPath, reqIp, reqToken, Helper.inputStream2String(req.getInputStream()),
|
reqType, reqPath, reqIp, reqToken, Helper.inputStream2String(req.getInputStream()),
|
||||||
ex.getDescription() != null ? ex.getDescription() : ex.getMessage());
|
ex.getMessage() == null ? ex.getDescription() : ex.getMessage());
|
||||||
} catch (Exception ignored) {
|
} catch (Exception ignored) {
|
||||||
}
|
}
|
||||||
|
|
||||||
log.debug("SecurityProtocolException Exception: ", ex);
|
log.debug("SecurityProtocolException Exception: ", ex);
|
||||||
|
|
||||||
List<String> errMeg = new ArrayList<>();
|
List<String> errMeg = new ArrayList<>();
|
||||||
|
@ -120,6 +140,39 @@ public class GlobalExceptionHandler {
|
||||||
errMeg.toArray(new String[0]));
|
errMeg.toArray(new String[0]));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler(ControllerRequestTimeoutException.class)
|
||||||
|
@ResponseBody
|
||||||
|
public ProtocolRespDTO<BaseRespStatus> handleException(HttpServletResponse rsp,
|
||||||
|
HttpServletRequest req,
|
||||||
|
ControllerRequestTimeoutException ex) {
|
||||||
|
try {
|
||||||
|
String reqType = req.getMethod();
|
||||||
|
String reqPath = req.getRequestURI();
|
||||||
|
String reqIp = req.getRemoteAddr();
|
||||||
|
String reqToken = req.getHeader("Authorization");
|
||||||
|
if (reqToken != null && reqToken.length() > 0) {
|
||||||
|
reqToken = reqToken.replace(ConstValue.STRING_HTTP_AUTH_HEAD, "");
|
||||||
|
}
|
||||||
|
log.error("Interface [{}] request <{}> from {}, token = <{}>\n" +
|
||||||
|
"+++ Request: {}\n" +
|
||||||
|
"--- Verify params failed: {}",
|
||||||
|
reqType, reqPath, reqIp, reqToken, Helper.inputStream2String(req.getInputStream()),
|
||||||
|
ex.getMessage());
|
||||||
|
} catch (Exception ignored) {
|
||||||
|
}
|
||||||
|
|
||||||
|
List<String> errMeg = new ArrayList<>();
|
||||||
|
rsp.setStatus(ErrorCode.ERR_PARAMEXCEPTION.getHttpCode());
|
||||||
|
|
||||||
|
if (ex.getMessage() != null && ex.getMessage().length() > 0) {
|
||||||
|
errMeg.add(ex.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
return ProtocolRespDTO.result(ErrorCode.ERR_PARAMEXCEPTION,
|
||||||
|
ErrorCode.ERR_PARAMEXCEPTION.getHttpCode(),
|
||||||
|
errMeg.toArray(new String[0]));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handle exception protocol resp dto.
|
* Handle exception protocol resp dto.
|
||||||
*
|
*
|
||||||
|
|
|
@ -1,9 +1,11 @@
|
||||||
package com.dispose.service.impl;
|
package com.dispose.service.impl;
|
||||||
|
|
||||||
import com.dispose.common.ConstValue;
|
import com.dispose.common.ConstValue;
|
||||||
|
import com.dispose.common.DisposeConfigValue;
|
||||||
import com.dispose.common.ErrorCode;
|
import com.dispose.common.ErrorCode;
|
||||||
import com.dispose.common.ProtoCryptoType;
|
import com.dispose.common.ProtoCryptoType;
|
||||||
import com.dispose.common.SecurityConfigValue;
|
import com.dispose.common.SecurityConfigValue;
|
||||||
|
import com.dispose.exception.ControllerRequestTimeoutException;
|
||||||
import com.dispose.pojo.dto.protocol.base.ProtocolReqDTO;
|
import com.dispose.pojo.dto.protocol.base.ProtocolReqDTO;
|
||||||
import com.dispose.pojo.dto.protocol.base.ProtocolRespDTO;
|
import com.dispose.pojo.dto.protocol.base.ProtocolRespDTO;
|
||||||
import com.dispose.security.arithmetic.CryptoHelper;
|
import com.dispose.security.arithmetic.CryptoHelper;
|
||||||
|
@ -49,6 +51,22 @@ public class ProtocolSecurityServiceImpl implements ProtocolSecurityService {
|
||||||
public String decryptProtocol(String ciphertext) throws JsonProcessingException {
|
public String decryptProtocol(String ciphertext) throws JsonProcessingException {
|
||||||
JsonNode objRoot = objectMapper.readTree(ciphertext);
|
JsonNode objRoot = objectMapper.readTree(ciphertext);
|
||||||
|
|
||||||
|
// 判断协议请求是否超时
|
||||||
|
if(DisposeConfigValue.CHECK_PROTO_REQUEST_TIMEOUT) {
|
||||||
|
long timeStamp = objRoot.path("timeStamp").asLong();
|
||||||
|
long current = System.currentTimeMillis();
|
||||||
|
long diff = current - timeStamp;
|
||||||
|
|
||||||
|
if(diff > DisposeConfigValue.REQUEST_TIMEOUT_MS) {
|
||||||
|
String errMsg = String.format("Request times %d, current times %d, timeout configure %d ms, timeout %d",
|
||||||
|
timeStamp, current, DisposeConfigValue.REQUEST_TIMEOUT_MS, diff);
|
||||||
|
|
||||||
|
throw new ControllerRequestTimeoutException(ErrorCode.ERR_REQTIMEOUT, errMsg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int cryptoType = objRoot.path("cryptoType").asInt();
|
int cryptoType = objRoot.path("cryptoType").asInt();
|
||||||
|
|
||||||
// 协议未加密
|
// 协议未加密
|
||||||
|
|
Loading…
Reference in New Issue