提交 d6250577 authored 作者: inroi's avatar inroi

微调

上级 2370dd26
package org.jeecg.modules.iost.ims.Util;
import javax.crypto.Cipher;
import java.io.ByteArrayOutputStream;
import java.nio.charset.StandardCharsets;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
/**
* Java RSA 加密工具类
*
* @author Inori
*/
public class RsaUtil {
/**
* 加密算法RSA
*/
public static final String KEY_ALGORITHM = "RSA";
/**
* 签名算法
*/
public static final String SIGNATURE_ALGORITHM = "SHA256withRSA";
/**
* RSA最大加密明文大小
*/
private static final int MAX_ENCRYPT_BLOCK = 117;
/**
* RSA最大解密密文大小
*/
private static final int MAX_DECRYPT_BLOCK = 128;
/**
* 私钥加密
*/
public static String encryptByPrivateKey(String data, String privateKey) throws Exception {
//base64编码的私钥
byte[] decoded = Base64.getDecoder().decode(privateKey);
PrivateKey priKey = KeyFactory.getInstance(KEY_ALGORITHM).generatePrivate(new PKCS8EncodedKeySpec(decoded));
Cipher cipher = Cipher.getInstance(KEY_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, priKey);
return Base64.getEncoder().encodeToString(cipher.doFinal(data.getBytes(StandardCharsets.UTF_8)));
}
/**
* 公钥解密
*/
public static String decryptByPublicKey(String data, String publicKey) throws Exception {
//base64编码的公钥
byte[] decoded = Base64.getDecoder().decode(publicKey);
PublicKey pubKey = KeyFactory.getInstance(KEY_ALGORITHM).generatePublic(new X509EncodedKeySpec(decoded));
Cipher cipher = Cipher.getInstance(KEY_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, pubKey);
return new String(cipher.doFinal(Base64.getDecoder().decode(data)));
}
/**
* 公钥加密
*/
public static String encryptByPublicKey(String data, String publicKey) throws Exception {
//base64编码的公钥
byte[] decoded = Base64.getDecoder().decode(publicKey);
PublicKey pubKey = KeyFactory.getInstance(KEY_ALGORITHM).generatePublic(new X509EncodedKeySpec(decoded));
//RSA加密
Cipher cipher = Cipher.getInstance(KEY_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
return Base64.getEncoder().encodeToString(cipher.doFinal(data.getBytes(StandardCharsets.UTF_8)));
}
/**
* 私钥解密
*/
public static String decryptByPrivateKey(String data, String privateKey) throws Exception {
//base64编码的私钥
byte[] decoded = Base64.getDecoder().decode(privateKey);
PrivateKey priKey = KeyFactory.getInstance(KEY_ALGORITHM).generatePrivate(new PKCS8EncodedKeySpec(decoded));
//RSA解密
Cipher cipher = Cipher.getInstance(KEY_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, priKey);
return new String(cipher.doFinal(Base64.getDecoder().decode(data)));
}
/**
* 私钥加密
*/
public static String encryptByPrivateKeyToLong(String data, String privateKey) throws Exception {
//base64编码的私钥
byte[] decoded = Base64.getDecoder().decode(privateKey);
PrivateKey priKey = KeyFactory.getInstance(KEY_ALGORITHM).generatePrivate(new PKCS8EncodedKeySpec(decoded));
Cipher cipher = Cipher.getInstance(KEY_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, priKey);
return purchaseToEncryption(cipher, data);
}
/**
* 公钥加密
*/
public static String encryptByPublicKeyToLong(String data, String publicKey) throws Exception {
//base64编码的公钥
byte[] decoded = Base64.getDecoder().decode(publicKey);
PublicKey pubKey = KeyFactory.getInstance(KEY_ALGORITHM).generatePublic(new X509EncodedKeySpec(decoded));
//RSA加密
Cipher cipher = Cipher.getInstance(KEY_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
return purchaseToEncryption(cipher, data);
}
private static String purchaseToEncryption(Cipher cipher, String data) throws Exception {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
int inputLen = data.getBytes(StandardCharsets.UTF_8).length;
int offSet = 0;
byte[] cache;
int i = 0;
// 对数据分段加密
try {
while (inputLen - offSet > 0) {
if (inputLen - offSet > MAX_ENCRYPT_BLOCK) {
cache = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8), offSet, MAX_ENCRYPT_BLOCK);
} else {
cache = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8), offSet, inputLen - offSet);
}
outputStream.write(cache, 0, cache.length);
i++;
offSet = i * MAX_ENCRYPT_BLOCK;
}
} finally {
outputStream.close();
}
return Base64.getEncoder().encodeToString(outputStream.toByteArray());
}
/**
* 公钥解密
*/
public static String decryptByPublicKeyToLong(String data, String publicKey) throws Exception {
//base64编码的公钥
byte[] decoded = Base64.getDecoder().decode(publicKey);
PublicKey pubKey = KeyFactory.getInstance(KEY_ALGORITHM).generatePublic(new X509EncodedKeySpec(decoded));
Cipher cipher = Cipher.getInstance(KEY_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, pubKey);
return purchaseToDecrypt(cipher, data);
}
/**
* 私钥解密
*/
public static String decryptByPrivateKeyToLong(String data, String privateKey) throws Exception {
//base64编码的私钥
byte[] decoded = Base64.getDecoder().decode(privateKey);
PrivateKey priKey = KeyFactory.getInstance(KEY_ALGORITHM).generatePrivate(new PKCS8EncodedKeySpec(decoded));
//RSA解密
Cipher cipher = Cipher.getInstance(KEY_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, priKey);
return purchaseToDecrypt(cipher, data);
}
private static String purchaseToDecrypt(Cipher cipher, String data) throws Exception {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
//64位解码加密后的字符串
byte[] inputByte = Base64.getMimeDecoder().decode(data);
int inputLen = inputByte.length;
int offSet = 0;
byte[] cache;
int i = 0;
// 对数据分段解密
try {
while (inputLen - offSet > 0) {
if (inputLen - offSet > MAX_DECRYPT_BLOCK) {
cache = cipher.doFinal(inputByte, offSet, MAX_DECRYPT_BLOCK);
} else {
cache = cipher.doFinal(inputByte, offSet, inputLen - offSet);
}
outputStream.write(cache, 0, cache.length);
i++;
offSet = i * MAX_DECRYPT_BLOCK;
}
} finally {
outputStream.close();
}
return new String(outputStream.toByteArray());
}
/**
* 私钥签名
*/
public static String sign(String privateKey, String plainText) throws Exception {
//64位解码加密后的字符串
byte[] byteKey = Base64.getDecoder().decode(privateKey);
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(byteKey);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
//签名
PrivateKey key = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
signature.initSign(key);
signature.update(plainText.getBytes());
return Base64.getEncoder().encodeToString(signature.sign());
}
/**
* 公钥验签
*/
public static boolean verifySign(String publicKey, String plainText, String sign) throws Exception {
//64位解码加密后的字符串
byte[] byteKey = Base64.getDecoder().decode(publicKey);
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(byteKey);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
//验签
PublicKey key = keyFactory.generatePublic(x509EncodedKeySpec);
Signature verifySign = Signature.getInstance(SIGNATURE_ALGORITHM);
verifySign.initVerify(key);
verifySign.update(plainText.getBytes());
return verifySign.verify(Base64.getDecoder().decode(sign));
}
}
package org.jeecg.modules.iost.ims.Util;
import java.util.Collection;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 字符串工具类
*
* @author ruoyi
*/
public class StringUtil extends org.apache.commons.lang3.StringUtils {
/**
* 空字符串
*/
private static final String NULL_STRING = "";
/**
* 下划线
*/
private static final char SEPARATOR = '_';
/**
* 正则验证字符串是否是数字
*/
private static final Pattern NUM_PATTERN = Pattern.compile("^[0-9]*$");
/**
* 获取参数不为空值
*
* @param value defaultValue 要判断的value
* @return value 返回值
*/
public static <T> T nvl(T value, T defaultValue) {
return value != null ? value : defaultValue;
}
/**
* * 判断一个Collection是否为空, 包含List,Set,Queue
*
* @param coll 要判断的Collection
* @return true:为空 false:非空
*/
public static boolean isEmpty(Collection<?> coll) {
return isNull(coll) || coll.isEmpty();
}
/**
* * 判断一个Collection是否非空,包含List,Set,Queue
*
* @param coll 要判断的Collection
* @return true:非空 false:空
*/
public static boolean isNotEmpty(Collection<?> coll) {
return !isEmpty(coll);
}
/**
* * 判断一个对象数组是否为空
*
* @param objects 要判断的对象数组
* @return true:为空 false:非空
*/
public static boolean isEmpty(Object[] objects) {
return isNull(objects) || (objects.length == 0);
}
/**
* * 判断一个对象数组是否非空
*
* @param objects 要判断的对象数组
* @return true:非空 false:空
*/
public static boolean isNotEmpty(Object[] objects) {
return !isEmpty(objects);
}
/**
* * 判断一个Map是否为空
*
* @param map 要判断的Map
* @return true:为空 false:非空
*/
public static boolean isEmpty(Map<?, ?> map) {
return isNull(map) || map.isEmpty();
}
/**
* * 判断一个Map是否为空
*
* @param map 要判断的Map
* @return true:非空 false:空
*/
public static boolean isNotEmpty(Map<?, ?> map) {
return !isEmpty(map);
}
/**
* * 判断一个字符串是否为空串
*
* @param str String
* @return true:为空 false:非空
*/
public static boolean isEmpty(String str) {
return isNull(str) || NULL_STRING.equals(str.trim());
}
/**
* * 判断一个字符串是否为非空串
*
* @param str String
* @return true:非空串 false:空串
*/
public static boolean isNotEmpty(String str) {
return !isEmpty(str);
}
/**
* * 判断一个对象是否为空
*
* @param object Object
* @return true:为空 false:非空
*/
public static boolean isNull(Object object) {
return object == null;
}
/**
* * 判断一个对象是否非空
*
* @param object Object
* @return true:非空 false:空
*/
public static boolean isNotNull(Object object) {
return !isNull(object);
}
/**
* * 判断一个对象是否是数组类型(Java基本型别的数组)
*
* @param object 对象
* @return true:是数组 false:不是数组
*/
public static boolean isArray(Object object) {
return isNotNull(object) && object.getClass().isArray();
}
/**
* 去空格
*/
public static String trim(String str) {
return (str == null ? "" : str.trim());
}
/**
* 截取字符串
*
* @param str 字符串
* @param start 开始
* @return 结果
*/
public static String substring(final String str, int start) {
if (str == null) {
return NULL_STRING;
}
if (start < 0) {
start = str.length() + start;
}
if (start < 0) {
start = 0;
}
if (start > str.length()) {
return NULL_STRING;
}
return str.substring(start);
}
/**
* 截取字符串
*
* @param str 字符串
* @param start 开始
* @param end 结束
* @return 结果
*/
public static String substring(final String str, int start, int end) {
if (str == null) {
return NULL_STRING;
}
if (end < 0) {
end = str.length() + end;
}
if (start < 0) {
start = str.length() + start;
}
if (end > str.length()) {
end = str.length();
}
if (start > end) {
return NULL_STRING;
}
if (start < 0) {
start = 0;
}
if (end < 0) {
end = 0;
}
return str.substring(start, end);
}
/**
* 下划线转驼峰命名
*/
public static String toUnderScoreCase(String str) {
if (str == null) {
return null;
}
StringBuilder sb = new StringBuilder();
// 前置字符是否大写
boolean preCharIsUpperCase = true;
// 当前字符是否大写
boolean curreCharIsUpperCase = true;
// 下一字符是否大写
boolean nexteCharIsUpperCase = true;
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (i > 0) {
preCharIsUpperCase = Character.isUpperCase(str.charAt(i - 1));
} else {
preCharIsUpperCase = false;
}
curreCharIsUpperCase = Character.isUpperCase(c);
if (i < (str.length() - 1)) {
nexteCharIsUpperCase = Character.isUpperCase(str.charAt(i + 1));
}
if (preCharIsUpperCase && curreCharIsUpperCase && !nexteCharIsUpperCase) {
sb.append(SEPARATOR);
} else if ((i != 0 && !preCharIsUpperCase) && curreCharIsUpperCase) {
sb.append(SEPARATOR);
}
sb.append(Character.toLowerCase(c));
}
return sb.toString();
}
/**
* 是否包含字符串
*
* @param str 验证字符串
* @param strs 字符串组
* @return 包含返回true
*/
public static boolean inStringIgnoreCase(String str, String... strs) {
if (str != null && strs != null) {
for (String s : strs) {
if (str.equalsIgnoreCase(trim(s))) {
return true;
}
}
}
return false;
}
/**
* 将下划线大写方式命名的字符串转换为驼峰式。如果转换前的下划线大写方式命名的字符串为空,则返回空字符串。 例如:HELLO_WORLD->HelloWorld
*
* @param name 转换前的下划线大写方式命名的字符串
* @return 转换后的驼峰式命名的字符串
*/
public static String convertToCamelCase(String name) {
StringBuilder result = new StringBuilder();
// 快速检查
if (name == null || name.isEmpty()) {
// 没必要转换
return "";
} else if (!name.contains("_")) {
// 不含下划线,仅将首字母大写
return name.substring(0, 1).toUpperCase() + name.substring(1);
}
// 用下划线将原始字符串分割
String[] camels = name.split("_");
for (String camel : camels) {
// 跳过原始字符串中开头、结尾的下换线或双重下划线
if (camel.isEmpty()) {
continue;
}
// 首字母大写
result.append(camel.substring(0, 1).toUpperCase());
result.append(camel.substring(1).toLowerCase());
}
return result.toString();
}
/**
* 驼峰式命名法 例如:user_name->userName
*/
public static String toCamelCase(String s) {
if (s == null) {
return null;
}
s = s.toLowerCase();
StringBuilder sb = new StringBuilder(s.length());
boolean upperCase = false;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == SEPARATOR) {
upperCase = true;
} else if (upperCase) {
sb.append(Character.toUpperCase(c));
upperCase = false;
} else {
sb.append(c);
}
}
return sb.toString();
}
/**
* 判断字符串是否是数字
*/
public static boolean isStr2Num(String str) {
Matcher matcher = NUM_PATTERN.matcher(str);
return matcher.matches();
}
/**
* java去除字符串中的空格、回车、换行符、制表符
*/
public static String replaceBlank(String str) {
String dest = "";
if (str != null) {
Pattern p = Pattern.compile("\\s*|\t|\r|\n");
Matcher m = p.matcher(str);
dest = m.replaceAll("");
}
return dest;
}
}
\ No newline at end of file
package org.jeecg.modules.iost.ims.vo;
/**
* @author Inori
*/
public interface Constants {
String TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
String FILE_FORMAT = "lic.tmp";
String EFFECTIVE_TIME = "effectiveTime";
String VERIFICATION_INTERVAL = "verificationInterval";
String CODE_NAME = "code";
String DATA_NAME = "data";
}
package org.jeecg.modules.system.controller;
import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.exceptions.ClientException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
......@@ -21,8 +22,8 @@ import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.*;
import org.jeecg.common.util.encryption.EncryptedString;
import org.jeecg.modules.base.service.BaseCommonService;
import org.jeecg.modules.iost.ims.Util.HttpUtil;
import org.jeecg.modules.iost.ims.Util.JsonUtil;
import org.jeecg.modules.iost.ims.Util.*;
import org.jeecg.modules.iost.ims.vo.Constants;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.model.SysLoginModel;
......@@ -34,6 +35,7 @@ import org.jeecg.modules.system.util.LocalMACUtil;
import org.jeecg.modules.system.util.RandImageUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
......@@ -67,6 +69,7 @@ public class LoginController {
@Resource
private BaseCommonService baseCommonService;
private static final String PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCDy/qpUuWG9IZarHPuohtHZmE6tyFmCvnv7MqovjqfBLkOR0Dkh8XfWaiF2ZLaUChJljlRVA7TmHCxasmIUaw/aqSOpv4QdTi+1cEZyh5J1kqEdQMd51va3lEwIixtD2iBfVJuh2i5bNyh7mMwiWAnCzdJixAZbJhv6MbqJTl1DQIDAQAB";
private static final String BASE_CHECK_CODES = "qwertyuiplkjhgfdsazxcvbnmQWERTYUPLKJHGFDSAZXCVBNM1234567890";
@ApiOperation("登录接口")
......@@ -134,16 +137,40 @@ public class LoginController {
}
public static String cda() throws IOException {
String localMac = LocalMACUtil.getLocalMac(InetAddress.getLocalHost());
try {
Map<String, String> hreader = new HashMap<>();
hreader.put("Content-Type", "application/json");
String url = "http://119.29.114.197/inspect/lqkj/inspect/mac";
Map<String, String> localMac1 = new HashMap<String, String>() {
{
put("localMac", localMac);
String localMac = LocalMACUtil.getLocalMac(InetAddress.getLocalHost());
Map<String, Object> request = new HashMap<>(1);
request.put("macAddress", localMac);
Map<String, Object> map = new HashMap<>(1);
map.put("data", RsaUtil.encryptByPublicKeyToLong(JsonUtils.toString(request), PUBLIC_KEY));
String url = "http://119.29.114.197:8090/lic/api/lic/online/verification";
String result = HttpUtil.post(url, JsonUtils.toString(map), hreader);
Map<String, Object> temp = JsonUtils.toMap(RsaUtil.decryptByPublicKeyToLong(result, PUBLIC_KEY), String.class, Object.class);
if (!CollectionUtils.isEmpty(temp)) {
if (temp.containsKey(Constants.DATA_NAME) && StringUtil.isNotNull(temp.get(Constants.DATA_NAME))) {
String sign = String.valueOf(temp.get(Constants.DATA_NAME));
temp.put(Constants.DATA_NAME, new Object());
if (RsaUtil.verifySign(PUBLIC_KEY, JSON.toJSONString(temp), sign)) {
if (temp.containsKey(Constants.CODE_NAME)) {
return String.valueOf(Integer.parseInt(String.valueOf(temp.get(Constants.CODE_NAME))) == 200);
} else {
return "false";
}
}
}
}
return "false";
} catch (Exception e) {
e.printStackTrace();
return "false";
}
};
return HttpUtil.post(url, JsonUtil.Mapjson(localMac1), hreader);
}
/**
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论