添加获取token接口
This commit is contained in:
parent
3b7c04e4d9
commit
e7bbd1a363
79
src/com/ruoyi/common/utils/rsa/RSAPublicUtils.java
Normal file
79
src/com/ruoyi/common/utils/rsa/RSAPublicUtils.java
Normal file
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
@ -54,4 +54,19 @@ public class AppUserLogonController {
|
||||
return ReturnMapUtil.rollback(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Path("/get/token")
|
||||
@GET
|
||||
public Map<String, Object> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -14,4 +14,7 @@ public interface AppUserLoginService {
|
||||
Map<String, Object> login(@Context HttpServletRequest request,
|
||||
@Context Transaction sqlca, @Context JBOTransaction tx,
|
||||
ReturnMapUtil ReturnMapUtil) throws JBOException;
|
||||
Map<String, Object> getToken(@Context HttpServletRequest request,
|
||||
@Context Transaction sqlca, @Context JBOTransaction tx,
|
||||
ReturnMapUtil ReturnMapUtil) throws Exception;
|
||||
}
|
||||
|
||||
@ -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<String, Object> 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<String, Object> body = new HashMap<String, Object>();
|
||||
body.put("token", messageEn);
|
||||
ReturnMapUtil.setReturnMap(body, (String) RestfullConstant.baseProperty
|
||||
.get("SUCCESS".toLowerCase()), "");
|
||||
return ReturnMapUtil.getReturnMap();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user