Przeglądaj źródła

加解密提供API

zhoutg 5 lat temu
rodzic
commit
6139777b86

+ 1 - 1
common/src/main/java/com/diagbot/util/RSAEncrypt.java

@@ -30,7 +30,7 @@ public class RSAEncrypt {
         //生成公钥和私钥
         // genKeyPair();
         //加密字符串
-//        String message = "2019-11-21 00:00:00";
+//        String message = "300";
         String message = "2019-11-21 00:00:00";
         String messageEn = encrypt(message, pubKey);
         System.out.println(message + "\t加密后的字符串为:" + messageEn);

+ 57 - 0
tran-service/src/main/java/com/diagbot/facade/RsaFacade.java

@@ -0,0 +1,57 @@
+package com.diagbot.facade;
+
+import com.diagbot.exception.CommonErrorCode;
+import com.diagbot.exception.CommonException;
+import com.diagbot.util.RSAEncrypt;
+import com.diagbot.vo.EncryptVO;
+import com.diagbot.vo.DecodeVO;
+import org.springframework.stereotype.Component;
+
+/**
+ * @description:
+ * @author: zhoutg
+ * @date: 2019/11/21 10:06
+ */
+@Component
+public class RsaFacade {
+
+    /**
+     * 加密
+     *
+     * @param encryptVO
+     * @return
+     */
+    public String encrypt(EncryptVO encryptVO) {
+        String regex = "";
+        if (1 == encryptVO.getType()) {
+            // licence 截止日期格式校验
+            regex = "\\d{4}-\\d{2}-\\d{2} [0-2][0-9]:[0-5][0-9]:[0-5][0-9]";
+            if (!encryptVO.getStr().matches(regex)) {
+                throw new CommonException(CommonErrorCode.SERVER_IS_ERROR, "日期格式错误!");
+            }
+        } else if (2 == encryptVO.getType()) {
+            // licence 剩余天数格式校验
+            Integer.parseInt(encryptVO.getStr()); // 不能转换成整形,就抛异常
+        }
+        try {
+            return RSAEncrypt.encrypt(encryptVO.getStr());
+        } catch (Exception e) {
+            throw new CommonException(CommonErrorCode.SERVER_IS_ERROR, "加密失败:" + encryptVO.getStr());
+        }
+    }
+
+
+    /**
+     * 解密
+     *
+     * @param rsaVO
+     * @return
+     */
+    public String decode(DecodeVO rsaVO) {
+        try {
+            return RSAEncrypt.decrypt(rsaVO.getStr());
+        } catch (Exception e) {
+            throw new CommonException(CommonErrorCode.SERVER_IS_ERROR, "解密失败:" + rsaVO.getStr());
+        }
+    }
+}

+ 24 - 0
tran-service/src/main/java/com/diagbot/vo/DecodeVO.java

@@ -0,0 +1,24 @@
+package com.diagbot.vo;
+
+import lombok.Getter;
+import lombok.Setter;
+
+import javax.validation.constraints.NotBlank;
+import javax.validation.constraints.NotEmpty;
+
+/**
+ * @description: RSA入参
+ * @author: zhoutg
+ * @date: 2019/11/21 9:57
+ */
+@Getter
+@Setter
+public class DecodeVO {
+
+    /**
+     * 文本
+     */
+    @NotBlank(message = "内容不能为空")
+    private String str;
+
+}

+ 30 - 0
tran-service/src/main/java/com/diagbot/vo/EncryptVO.java

@@ -0,0 +1,30 @@
+package com.diagbot.vo;
+
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Getter;
+import lombok.Setter;
+
+import javax.validation.constraints.NotBlank;
+
+/**
+ * @description: RSA 加密入参
+ * @author: zhoutg
+ * @date: 2019/11/21 9:57
+ */
+@Getter
+@Setter
+public class EncryptVO {
+
+    /**
+     * 文本
+     */
+    @NotBlank(message = "内容不能为空")
+    private String str;
+
+    /**
+     * 加密类型
+     */
+    @ApiModelProperty(hidden = true)
+    private Integer type = 0; // 1:licence截止日期专用,2:licence剩余天数专用,其他:普通加密
+
+}

+ 71 - 0
tran-service/src/main/java/com/diagbot/web/RsaController.java

@@ -0,0 +1,71 @@
+package com.diagbot.web;
+
+import com.diagbot.annotation.SysLogger;
+import com.diagbot.dto.RespDTO;
+import com.diagbot.facade.RsaFacade;
+import com.diagbot.vo.EncryptVO;
+import com.diagbot.vo.DecodeVO;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import javax.validation.Valid;
+
+/**
+ * @description: RSA加解密
+ * @author: zhoutg
+ * @date: 2019/11/21 10:56
+ */
+@RestController
+@RequestMapping("/rsa")
+@Api(value = "RSA加解密相关API", tags = { "RSA加解密相关API" })
+@SuppressWarnings("unchecked")
+public class RsaController {
+
+    @Autowired
+    RsaFacade rsaFacade;
+
+    @ApiOperation(value = "licence截止日期加密:[by:zhoutg]",
+            notes = "licence截止日期加密,格式如下:2019-12-31 12:00:00, 英文状态")
+    @PostMapping("/encryptLicenceExpire")
+    @SysLogger("encryptLicenceExpire")
+    public RespDTO<String> encryptLicenceExpire(@Valid @RequestBody EncryptVO encryptVO) {
+        encryptVO.setType(1);
+        String res = rsaFacade.encrypt(encryptVO);
+        return RespDTO.onSuc(res);
+    }
+
+
+    @ApiOperation(value = "licence剩余天数加密:[by:zhoutg]",
+            notes = "licence剩余天数加密,格式如下:365,纯数字")
+    @PostMapping("/encryptLicenceReamin")
+    @SysLogger("encryptLicenceReamin")
+    public RespDTO<String> encryptLicenceReamin(@Valid @RequestBody EncryptVO encryptVO) {
+        encryptVO.setType(2);
+        String res = rsaFacade.encrypt(encryptVO);
+        return RespDTO.onSuc(res);
+    }
+
+
+    @ApiOperation(value = "普通文本加密:[by:zhoutg]",
+            notes = "普通文本加密,格式如下:365,纯数字")
+    @PostMapping("/encrypt")
+    @SysLogger("encrypt")
+    public RespDTO<String> encrypt(@Valid @RequestBody EncryptVO encryptVO) {
+        String res = rsaFacade.encrypt(encryptVO);
+        return RespDTO.onSuc(res);
+    }
+
+
+    @ApiOperation(value = "解密:[by:zhoutg]", notes = "解密")
+    @PostMapping("/decode")
+    @SysLogger("decode")
+    public RespDTO<String> decode(@Valid @RequestBody DecodeVO decodeVO) {
+        String res = rsaFacade.decode(decodeVO);
+        return RespDTO.onSuc(res);
+    }
+}