Browse Source

第三方接口签名验证DEMO
返回值的规范修改

wub 4 years ago
parent
commit
f32361a3d7

+ 28 - 7
src/main/java/com/uas/eis/controller/ApiSignLoginInterceptor.java

@@ -1,8 +1,9 @@
 package com.uas.eis.controller;
 
 import com.uas.eis.core.support.TokenProperties;
-import com.uas.eis.entity.ErrorMsg;
-import com.uas.eis.exception.SystemException;
+import com.uas.eis.entity.ErrorMessage;
+import com.uas.eis.exception.ApiSystemException;
+import com.uas.eis.sdk.entity.ApiResult;
 import com.uas.eis.utils.MD5Util;
 import org.apache.commons.lang.StringUtils;
 import org.springframework.stereotype.Service;
@@ -36,27 +37,47 @@ public class ApiSignLoginInterceptor extends HandlerInterceptorAdapter {
         String accessSecret = tokenConfig.get(accessKey);
 
         if (!StringUtils.isNumeric(timestamp)) {
-            throw new SystemException(ErrorMsg.TIMESTAMP_ILLEGAL);
+            ApiResult apiResult = new ApiResult();
+            apiResult.setCode(ErrorMessage.TIMESTAMP_ILLEGAL.getCode());
+            apiResult.setMessage(ErrorMessage.TIMESTAMP_ILLEGAL.getMessage());
+            apiResult.setRequestId(requestId);
+            throw new ApiSystemException(apiResult);
         }
 
         // 检查KEY是否合理
         if (StringUtils.isEmpty(accessKey) || StringUtils.isEmpty(accessSecret)) {
-            throw new SystemException(ErrorMsg.ACCESSKEY_ILLEGAL);
+            ApiResult apiResult = new ApiResult();
+            apiResult.setCode(ErrorMessage.ACCESSKEY_ILLEGAL.getCode());
+            apiResult.setMessage(ErrorMessage.ACCESSKEY_ILLEGAL.getMessage());
+            apiResult.setRequestId(requestId);
+            throw new ApiSystemException(apiResult);
         }
 
         Long ts = Long.valueOf(timestamp);
         // 禁止超时签名
         if (System.currentTimeMillis() - ts > SIGN_EXPIRED_TIME) {
-            throw new SystemException(ErrorMsg.TIMEOUT_ILLEGAL);
+            ApiResult apiResult = new ApiResult();
+            apiResult.setCode(ErrorMessage.TIMEOUT_ILLEGAL.getCode());
+            apiResult.setMessage(ErrorMessage.TIMEOUT_ILLEGAL.getMessage());
+            apiResult.setRequestId(requestId);
+            throw new ApiSystemException(apiResult);
         }
 
         String regex = "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$";
         if (!requestId.matches(regex)) {
-            throw new SystemException(ErrorMsg.REQUESTID_ILLEGAL);
+            ApiResult apiResult = new ApiResult();
+            apiResult.setCode(ErrorMessage.REQUESTID_ILLEGAL.getCode());
+            apiResult.setMessage(ErrorMessage.REQUESTID_ILLEGAL.getMessage());
+            apiResult.setRequestId(requestId);
+            throw new ApiSystemException(apiResult);
         }
 
         if (!verificationSign(request, accessKey, accessSecret)) {
-            throw new SystemException(ErrorMsg.SIGNATURE_ILLEGAL);
+            ApiResult apiResult = new ApiResult();
+            apiResult.setCode(ErrorMessage.SIGNATURE_ILLEGAL.getCode());
+            apiResult.setMessage(ErrorMessage.SIGNATURE_ILLEGAL.getMessage());
+            apiResult.setRequestId(requestId);
+            throw new ApiSystemException(apiResult);
         }
         return true;
     }

+ 41 - 0
src/main/java/com/uas/eis/entity/ErrorMessage.java

@@ -0,0 +1,41 @@
+package com.uas.eis.entity;
+
+import java.util.Objects;
+
+/**
+ * Created by luhg on 2018/4/20.
+ * 异常处理返回信息
+ */
+public enum ErrorMessage {
+
+    TIMESTAMP_ILLEGAL(1001,"请求时间戳不合法"),
+    ACCESSKEY_ILLEGAL(1002,"加密KEY不合法"),
+    TIMEOUT_ILLEGAL(1003,"请求超时"),
+    REQUESTID_ILLEGAL(1004,"随机字符串不合法"),
+    SIGNATURE_ILLEGAL(1005,"签名错误");
+
+    private int code;
+    private String message;
+
+    ErrorMessage(int  code,String message){
+        this.code = code;
+        this.message = message;
+    }
+
+    public int getCode() {
+        return code;
+    }
+
+    public void setCode(int code) {
+        this.code = code;
+    }
+
+    public String getMessage() {
+        return message;
+    }
+
+    public void setMessage(String message) {
+        this.message = message;
+    }
+
+}

+ 49 - 0
src/main/java/com/uas/eis/exception/ApiSystemException.java

@@ -0,0 +1,49 @@
+package com.uas.eis.exception;
+
+import com.uas.eis.sdk.entity.ApiResult;
+
+/**
+ * 系统程序执行异常
+ * 
+ * @author yingp
+ * 
+ */
+public class ApiSystemException extends RuntimeException {
+
+	/**
+	 *
+	 */
+	private static final long serialVersionUID = 4218425517031998401L;
+
+	private ApiResult apiResult;
+
+	public ApiSystemException() {
+	}
+
+	public ApiSystemException(String paramString) {
+		super(paramString);
+	}
+
+	public ApiSystemException(ApiResult apiResult) {
+		super(apiResult.getMessage());
+		this.apiResult = apiResult;
+	}
+
+	public ApiSystemException(ApiResult apiResult, Throwable paramThrowable) {
+		super(apiResult.getMessage(), paramThrowable);
+		this.apiResult = apiResult;
+	}
+
+	public ApiSystemException(Throwable paramThrowable) {
+		super(paramThrowable);
+	}
+
+
+	public ApiResult getApiResult() {
+		return apiResult;
+	}
+
+	public void setApiResult(ApiResult apiResult) {
+		this.apiResult = apiResult;
+	}
+}

+ 24 - 6
src/main/java/com/uas/eis/exception/ExceptionHandlerAdvice.java

@@ -1,18 +1,16 @@
 package com.uas.eis.exception;
 
-import javax.servlet.http.HttpServletRequest;
-
 import com.uas.eis.entity.ErrorMsg;
+import com.uas.eis.sdk.entity.ApiResult;
 import org.apache.log4j.Logger;
 import org.springframework.http.HttpStatus;
 import org.springframework.ui.ModelMap;
-import org.springframework.util.StringUtils;
 import org.springframework.web.bind.annotation.ControllerAdvice;
 import org.springframework.web.bind.annotation.ExceptionHandler;
 import org.springframework.web.bind.annotation.ResponseBody;
 import org.springframework.web.bind.annotation.ResponseStatus;
 
-import com.uas.eis.utils.StringUtil;
+import javax.servlet.http.HttpServletRequest;
 
 
 @ControllerAdvice
@@ -38,10 +36,11 @@ public class ExceptionHandlerAdvice {
 		map.put("errDesc",ex.getMessage());
 		return map;
 	}
-	
+
+
 	/**
 	 * 处理通过BaseUtil.showError抛出的异常
-	 * 
+	 *
 	 * @param ex
 	 * @return
 	 */
@@ -56,6 +55,25 @@ public class ExceptionHandlerAdvice {
 		map.put("errDesc",errorMsg.getErrDesc());
 		return map;
 	}
+	
+	/**
+	 * 处理通过Api接口抛出的异常
+	 * 
+	 * @param ex
+	 * @return
+	 */
+	@ExceptionHandler(ApiSystemException.class)
+	@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
+	@ResponseBody
+	public ModelMap handleApiSystemError(ApiSystemException ex, HttpServletRequest request) {
+		ModelMap map = new ModelMap();
+		ApiResult apiResult = ex.getApiResult();
+		map.put("code", apiResult.getCode());
+		map.put("message", apiResult.getMessage());
+		map.put("requestId",apiResult.getRequestId());
+		map.put("data",apiResult.getData());
+		return map;
+	}
 
 
 	

+ 37 - 0
src/main/java/com/uas/eis/sdk/entity/ApiResult.java

@@ -0,0 +1,37 @@
+package com.uas.eis.sdk.entity;
+
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+@Data
+@NoArgsConstructor
+public class ApiResult<T> {
+
+    /**
+     * 自定义业务码
+     */
+    private int code;
+
+    /**
+     * 自定义业务提示说明
+     */
+    private String message;
+
+
+    private String requestId;
+
+    /**
+     * 自定义返回 数据结果集
+     */
+    private T data;
+
+    @Override
+    public String toString() {
+        return "Result{" +
+                ", code=" + code +
+                ", message=" + message +
+                ", requestId='" + requestId + '\'' +
+                ", data=" + data +
+                '}';
+    }
+}