diff --git a/src/com/ruoyi/common/utils/rsa/RSAPublicUtils.java b/src/com/ruoyi/common/utils/rsa/RSAPublicUtils.java new file mode 100644 index 000000000..7ed71bd4b --- /dev/null +++ b/src/com/ruoyi/common/utils/rsa/RSAPublicUtils.java @@ -0,0 +1,79 @@ +package com.ruoyi.common.utils.rsa; + +import com.alibaba.fastjson.JSONObject; + +import javax.crypto.Cipher; +import java.io.ByteArrayOutputStream; +import java.security.KeyFactory; +import java.security.interfaces.RSAPublicKey; +import java.security.spec.X509EncodedKeySpec; +import java.util.Base64; +import java.util.Date; + +/** + * token 加密工具类 + */ +public class RSAPublicUtils { + + private static String publicKey = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAJYJcyDY6o29eo4/5AII88CnalMb8OFtLiTBWxu4UYHR5TYttHJk5dPvK/0hsAnz8WZsfP4bc8kGrA60eQIikOcCAwEAAQ=="; + + /** + * RSA公钥加密 + * + * @param input 加密字符串 + * publicKey 公钥 + * @return 密文 + * @throws Exception 加密过程中的异常信息 + */ + public static String encrypt(String input) throws Exception { + ByteArrayOutputStream bops = null; + String encodeToString = null; + try{ + //base64编码的公钥 + byte[] decoded = Base64.getDecoder().decode(publicKey); + RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(decoded)); + //RSA加密 + Cipher cipher = Cipher.getInstance("RSA"); + cipher.init(Cipher.ENCRYPT_MODE, pubKey); + // 标识 + byte[] bytes = input.getBytes(); + int inputLen = bytes.length; + int offLen = 0;//偏移量 + int i = 0; + bops = new ByteArrayOutputStream(); + while(inputLen - offLen > 0){ + byte [] cache; + if(inputLen - offLen > 53){ + cache = cipher.doFinal(bytes, offLen,53); + }else{ + cache = cipher.doFinal(bytes, offLen,inputLen - offLen); + } + bops.write(cache); + i++; + offLen = 53 * i; + } + bops.close(); + byte[] encryptedData = bops.toByteArray(); + encodeToString = Base64.getEncoder().encodeToString(encryptedData); + }catch (Exception e){ + e.printStackTrace(); + throw new Exception("加密异常"); + }finally { + if(bops != null){ + bops.close(); + } + } + return encodeToString; + } + + public static void main(String[] args) throws Exception { + JSONObject param = new JSONObject(); + param.put("userId", "1111"); + param.put("date", new Date().getTime()); + String message = param.toJSONString(); + System.out.println("原文:" + message); + String messageEn = encrypt(message); + System.out.println("密文:" + messageEn); + } + +} diff --git a/src_app_fresh/apx/com/amarsoft/als/user/logon/controller/AppUserLogonController.java b/src_app_fresh/apx/com/amarsoft/als/user/logon/controller/AppUserLogonController.java index f98dd3853..ae7d09396 100644 --- a/src_app_fresh/apx/com/amarsoft/als/user/logon/controller/AppUserLogonController.java +++ b/src_app_fresh/apx/com/amarsoft/als/user/logon/controller/AppUserLogonController.java @@ -54,4 +54,19 @@ public class AppUserLogonController { return ReturnMapUtil.rollback(e); } } + + @Path("/get/token") + @GET + public Map getToken( + @Context HttpServletRequest request, @Context Transaction sqlca,@Context JBOTransaction tx) + throws Exception { + ReturnMapUtil ReturnMapUtil = new ReturnMapUtil(tx, sqlca); + ARE.getLog().info("[CONTROLLER] AppUserLogonController run ................."); + ARE.getLog().info("[Path] /logon/manager/get/token run ................."); + try { + return loginService.getToken(request, sqlca, tx, ReturnMapUtil); + } catch (Exception e) { + return ReturnMapUtil.rollback(e); + } + } } diff --git a/src_app_fresh/apx/com/amarsoft/als/user/logon/service/AppUserLoginService.java b/src_app_fresh/apx/com/amarsoft/als/user/logon/service/AppUserLoginService.java index 272bffdd1..c69c24676 100644 --- a/src_app_fresh/apx/com/amarsoft/als/user/logon/service/AppUserLoginService.java +++ b/src_app_fresh/apx/com/amarsoft/als/user/logon/service/AppUserLoginService.java @@ -14,4 +14,7 @@ public interface AppUserLoginService { Map login(@Context HttpServletRequest request, @Context Transaction sqlca, @Context JBOTransaction tx, ReturnMapUtil ReturnMapUtil) throws JBOException; + Map getToken(@Context HttpServletRequest request, + @Context Transaction sqlca, @Context JBOTransaction tx, + ReturnMapUtil ReturnMapUtil) throws Exception; } diff --git a/src_app_fresh/apx/com/amarsoft/als/user/logon/service/impl/AppUserLoginServiceImpl.java b/src_app_fresh/apx/com/amarsoft/als/user/logon/service/impl/AppUserLoginServiceImpl.java index e0f78b15c..b99abcfef 100644 --- a/src_app_fresh/apx/com/amarsoft/als/user/logon/service/impl/AppUserLoginServiceImpl.java +++ b/src_app_fresh/apx/com/amarsoft/als/user/logon/service/impl/AppUserLoginServiceImpl.java @@ -3,6 +3,7 @@ package apx.com.amarsoft.als.user.logon.service.impl; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.ArrayList; +import java.util.Date; import java.util.GregorianCalendar; import java.util.HashMap; import java.util.List; @@ -19,6 +20,7 @@ import jbo.sys.USER_ROLE; import apx.com.amarsoft.als.user.logon.service.AppUserLoginService; import cn.bean.Org; +import com.alibaba.fastjson.JSONObject; import com.amarsoft.are.ARE; import com.amarsoft.are.jbo.BizObject; import com.amarsoft.are.jbo.BizObjectManager; @@ -36,6 +38,7 @@ import com.base.util.DES; import com.base.util.DateUtil; import com.base.util.MD5Util; import com.base.util.ReturnMapUtil; +import com.ruoyi.common.utils.rsa.RSAPublicUtils; public class AppUserLoginServiceImpl implements AppUserLoginService { @@ -340,4 +343,23 @@ public class AppUserLoginServiceImpl implements AppUserLoginService { return sb.toString().toUpperCase(); } + @Override + public Map getToken(HttpServletRequest request, + Transaction sqlca, JBOTransaction tx, ReturnMapUtil ReturnMapUtil) + throws Exception{ + JSONObject param = new JSONObject(); + param.put("userId",request.getParameter("userid")); + param.put("date",request.getParameter("date")); + String message = param.toJSONString(); + System.out.println("原文:" + message); + String messageEn; + messageEn = RSAPublicUtils.encrypt(message); + System.out.println("密文:" + messageEn); + Map body = new HashMap(); + body.put("token", messageEn); + ReturnMapUtil.setReturnMap(body, (String) RestfullConstant.baseProperty + .get("SUCCESS".toLowerCase()), ""); + return ReturnMapUtil.getReturnMap(); + } + }