Prechádzať zdrojové kódy

新增接口数据加密逻辑

codingliang 1 rok pred
rodič
commit
94839ba68e

+ 7 - 0
pom.xml

@@ -390,6 +390,13 @@
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-data-jpa</artifactId>
         </dependency>
+
+        <dependency>
+            <groupId>icu.xuyijie</groupId>
+            <artifactId>secure-api-spring-boot-starter</artifactId>
+            <version>2.1.5</version>
+        </dependency>
+
         <!--<dependency>-->
         <!--<groupId>com.baidu.aip</groupId>-->
         <!--            <artifactId>java-sdk</artifactId>-->

+ 9 - 0
src/main/java/com/sqx/common/exception/SqxExceptionHandler.java

@@ -1,6 +1,7 @@
 package com.sqx.common.exception;
 
 import com.sqx.common.utils.Result;
+import icu.xuyijie.secureapi.exception.SecureApiException;
 import org.apache.shiro.authz.AuthorizationException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -12,6 +13,8 @@ import org.springframework.web.bind.annotation.ExceptionHandler;
 import org.springframework.web.bind.annotation.RestControllerAdvice;
 import org.springframework.web.servlet.NoHandlerFoundException;
 
+import javax.servlet.http.HttpServletRequest;
+
 /**
  * 异常处理器
  *
@@ -63,6 +66,12 @@ public class SqxExceptionHandler {
 		return Result.error("请求参数缺失");
 	}
 
+	@ExceptionHandler(value = SecureApiException.class)
+	public Result secureApiException(HttpServletRequest request, SecureApiException e) {
+		logger.error("SecureApi异常:{}", e.getErrorMsg());
+		return Result.error(e.getErrorMsg());
+	}
+
 	@ExceptionHandler(Exception.class)
 	public Result handleException(Exception e){
 		logger.error(e.getMessage(), e);

+ 26 - 0
src/main/resources/application-dev.yml

@@ -32,4 +32,30 @@ spring:
                 wall:
                     config:
                         multi-statement-allow: true
+secure-api:
+    # 开启SecureApi功能,如果为false则其余配置项均不生效
+    enabled: true
+    # 开启加解密日志打印,会打印出接口名、加密模式、算法、明文和密文等信息
+    show-log: true
+    url-safe: true
+    # 加密模式,common和session_key可选,session_key是会话密钥模式,用于每次请求都使用不同的密钥,需要前端配合
+    mode: common
+    # 加密算法
+    cipher-algorithm: aes_cbc_pkcs5
+    # 对称算法用于加解密的密钥,Base64格式,cipher-algorithm选择对称加密算法时配置,也可为空,组件会随机生成一个
+    key: oURKtBcNU6axnNoebKkuPqkCn6ivhE9qbg9FpeNnfpo=
+    # 对称算法用于加解密的偏移量,Base64格式,cipher-algorithm选择对称加密算法时配置,也可为空,组件会随机生成一个
+    iv: eW1xeThJSkprcEZUTGh1Ng==
+    # 需要加密的接口路径匹配,遵循spring boot拦截器的正则规则,留空或者不配置代表不使用url匹配,只对注解的接口进行解密
+    encrypt-url:
+        # 配置了此项,接口有无注解都将进行返回值加密
+        include-urls: /**
+        # 即使配置了排除,注解的优先级也高于此项
+        exclude-urls:
+    # 需要解密的接口路径匹配,遵循spring boot拦截器的正则规则,留空或者不配置代表不使用url匹配,只对注解的接口、参数、字段进行解密
+    decrypt-url:
+        # 配置了此项,接口、参数、字段有无注解都将进行解密
+        include-urls: /**
+        # 即使配置了排除,注解的优先级也高于此项
+        exclude-urls: