From 2b89841bebdcd968b39bdd3137b99ed9983b6120 Mon Sep 17 00:00:00 2001 From: luoluogit <1014532975@qq.com> Date: Thu, 19 Dec 2024 16:06:25 +0800 Subject: [PATCH] =?UTF-8?q?update:=E7=AC=AC=E4=B8=89=E6=96=B9=E5=BE=85?= =?UTF-8?q?=E5=8A=9E=E6=B7=BB=E5=8A=A0=E7=94=A8=E6=88=B7=E5=90=8D=E5=90=8E?= =?UTF-8?q?=E7=BC=80=E5=B9=B6=E5=8A=A0=E5=AF=86=EF=BC=8C=E7=AC=AC=E4=B8=89?= =?UTF-8?q?=E6=96=B9=E7=82=B9=E5=87=BB=E5=8F=AF=E8=B7=B3=E8=BD=AC=E8=87=B3?= =?UTF-8?q?=E5=AE=A1=E6=89=B9=E5=A4=84=E7=90=86=E9=A1=B5=E9=9D=A222?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../shkd/sys/sys/eoss/SSOPluginLogin.java | 6 +- .../backlogTack/ToDoResendTack.java | 7 +- .../servicehandler/BacklogServiceHandle.java | 5 +- .../main/java/shkd/sys/sys/utils/RSAUtil.java | 72 +++++++++++++++++++ 4 files changed, 80 insertions(+), 10 deletions(-) create mode 100644 sys/shkd-sys-sys/src/main/java/shkd/sys/sys/utils/RSAUtil.java diff --git a/sys/shkd-sys-sys/src/main/java/shkd/sys/sys/eoss/SSOPluginLogin.java b/sys/shkd-sys-sys/src/main/java/shkd/sys/sys/eoss/SSOPluginLogin.java index a4f2316..39e8e64 100644 --- a/sys/shkd-sys-sys/src/main/java/shkd/sys/sys/eoss/SSOPluginLogin.java +++ b/sys/shkd-sys-sys/src/main/java/shkd/sys/sys/eoss/SSOPluginLogin.java @@ -13,6 +13,7 @@ import kd.bos.login.thirdauth.UserProperType; import kd.bos.servicehelper.user.UserServiceHelper; import org.apache.commons.codec.binary.Base64; import org.apache.commons.lang3.StringUtils; +import shkd.sys.sys.utils.RSAUtil; import shkd.sys.sys.utils.RSAUtils; import javax.servlet.http.*; @@ -110,9 +111,8 @@ public class SSOPluginLogin implements ThirdSSOAuthHandler { } }else if (StringUtils.isNotEmpty(userName)){ try{ - PrivateKey privateKey1 = RSAUtils.getPrivateKey(Base64.decodeBase64(RSAUtils.privateKey)); - String sourceData = new String(RSAUtils.decryptByKey(privateKey1, Base64.decodeBase64(userName.getBytes()))); - result.setUser(sourceData); + String user = RSAUtil.decrypt(userName,RSAUtil.getPrivateKeyFromString()); + result.setUser(user); result.setSucess(true); logger.info("SSO用户登录成功,进入苍穹系统"); }catch (Exception e){ diff --git a/sys/shkd-sys-sys/src/main/java/shkd/sys/sys/midservice/backlogTack/ToDoResendTack.java b/sys/shkd-sys-sys/src/main/java/shkd/sys/sys/midservice/backlogTack/ToDoResendTack.java index 316cedc..85500ee 100644 --- a/sys/shkd-sys-sys/src/main/java/shkd/sys/sys/midservice/backlogTack/ToDoResendTack.java +++ b/sys/shkd-sys-sys/src/main/java/shkd/sys/sys/midservice/backlogTack/ToDoResendTack.java @@ -21,6 +21,7 @@ import shkd.sys.sys.midservice.handler.CreateToDoHandler; import shkd.sys.sys.midservice.handler.DealToDoHandler; import shkd.sys.sys.midservice.handler.deleteToDoHandler; import shkd.sys.sys.midservice.utils.GetUrlUtils; +import shkd.sys.sys.utils.RSAUtil; import shkd.sys.sys.utils.RSAUtils; import java.util.*; @@ -175,13 +176,11 @@ public class ToDoResendTack extends AbstractTask { "phone,username", new QFilter[]{new QFilter("id", "=", next.getString("freceiveuserid"))}); String userName; try { - userName = new String(org.apache.commons.codec.binary.Base64.encodeBase64(RSAUtils.encryptByKey( - RSAUtils.getPublicKey(Base64.decodeBase64(RSAUtils.publicKey)), - dynamicObject.getString("username").getBytes()))); + userName = RSAUtil.decrypt(dynamicObject.getString("username"),RSAUtil.getPrivateKeyFromString()); }catch (Exception e){ ErrorCode errorCode = new ErrorCode("error_code", "公钥加密出现异常,请联系运维人员排查!"); throw new KDException(errorCode, e); - }// String number = dynamicObject.getString("number"); + } switch (t_status) { case "0": //标题 diff --git a/sys/shkd-sys-sys/src/main/java/shkd/sys/sys/midservice/servicehandler/BacklogServiceHandle.java b/sys/shkd-sys-sys/src/main/java/shkd/sys/sys/midservice/servicehandler/BacklogServiceHandle.java index a20c3d0..240175f 100644 --- a/sys/shkd-sys-sys/src/main/java/shkd/sys/sys/midservice/servicehandler/BacklogServiceHandle.java +++ b/sys/shkd-sys-sys/src/main/java/shkd/sys/sys/midservice/servicehandler/BacklogServiceHandle.java @@ -15,6 +15,7 @@ import org.apache.commons.codec.binary.Base64; import shkd.sys.sys.midservice.handler.CreateToDoHandler; import shkd.sys.sys.midservice.handler.DealToDoHandler; import shkd.sys.sys.midservice.handler.deleteToDoHandler; +import shkd.sys.sys.utils.RSAUtil; import shkd.sys.sys.utils.RSAUtils; import java.util.List; @@ -45,9 +46,7 @@ public class BacklogServiceHandle extends AbstractServiceHandler { for (DynamicObject query_one : query) { String userName; try { - userName = new String(Base64.encodeBase64(RSAUtils.encryptByKey( - RSAUtils.getPublicKey(Base64.decodeBase64(RSAUtils.publicKey)), - query_one.getString("username").getBytes()))); + userName = RSAUtil.decrypt(query_one.getString("username"),RSAUtil.getPrivateKeyFromString()); }catch (Exception e){ ErrorCode errorCode = new ErrorCode("error_code", "公钥加密出现异常,请联系运维人员排查!"); throw new KDException(errorCode, e); diff --git a/sys/shkd-sys-sys/src/main/java/shkd/sys/sys/utils/RSAUtil.java b/sys/shkd-sys-sys/src/main/java/shkd/sys/sys/utils/RSAUtil.java new file mode 100644 index 0000000..a94dc8c --- /dev/null +++ b/sys/shkd-sys-sys/src/main/java/shkd/sys/sys/utils/RSAUtil.java @@ -0,0 +1,72 @@ +package shkd.sys.sys.utils; + +import javax.crypto.Cipher; +import java.security.KeyFactory; +import java.security.PublicKey; +import java.security.PrivateKey; +import java.security.spec.X509EncodedKeySpec; +import java.security.spec.PKCS8EncodedKeySpec; +import java.util.Base64; + +public class RSAUtil { + + private static String key = "kingdee20241219"; + + /** + * 加密 + * + * @param data 需加密的数据 + * @param key 公钥 + * @return 加密后的数据 + * @throws Exception 异常 + */ + public static String encrypt(String data, PublicKey key) throws Exception { + Cipher cipher = Cipher.getInstance("RSA"); + cipher.init(Cipher.ENCRYPT_MODE, key); + byte[] encryptedBytes = cipher.doFinal(data.getBytes()); + return Base64.getEncoder().encodeToString(encryptedBytes); + } + + /** + * 解密 + * + * @param data 需加密的数据 + * @param key 私密 + * @return 解密后的数据 + * @throws Exception 异常 + */ + public static String decrypt(String data, PrivateKey key) throws Exception { + Cipher cipher = Cipher.getInstance("RSA"); + cipher.init(Cipher.DECRYPT_MODE, key); + byte[] original = cipher.doFinal(Base64.getDecoder().decode(data)); + return new String(original); + } + + + /** + * 获取公钥 + * + * @return 公钥 + * @throws Exception 异常 + */ + public static PublicKey getPublicKeyFromString() throws Exception { + byte[] keyBytes = Base64.getDecoder().decode(key); + X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes); + KeyFactory keyFactory = KeyFactory.getInstance("RSA"); + return keyFactory.generatePublic(keySpec); + } + + /** + * 获取秘钥 + * + * @return 私钥 + * @throws Exception 异常 + */ + public static PrivateKey getPrivateKeyFromString() throws Exception { + byte[] keyBytes = Base64.getDecoder().decode(key); + PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes); + KeyFactory keyFactory = KeyFactory.getInstance("RSA"); + return keyFactory.generatePrivate(keySpec); + } + +}