From 1b764077114451973c8c1ed3802e1adc7e915bf9 Mon Sep 17 00:00:00 2001 From: sxu <602087911@qq.com> Date: Tue, 3 Sep 2024 09:15:46 +0800 Subject: [PATCH] =?UTF-8?q?=E6=99=BA=E6=85=A7=E5=B7=A5=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sgzb-common/sgzb-common-security/pom.xml | 22 +++++++++ .../security/utils/GetTokenByAppKey.java | 49 +++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 sgzb-common/sgzb-common-security/src/main/java/com/bonus/sgzb/common/security/utils/GetTokenByAppKey.java diff --git a/sgzb-common/sgzb-common-security/pom.xml b/sgzb-common/sgzb-common-security/pom.xml index e531c52..5e07dc3 100644 --- a/sgzb-common/sgzb-common-security/pom.xml +++ b/sgzb-common/sgzb-common-security/pom.xml @@ -34,6 +34,28 @@ sgzb-common-redis + + cn.hutool + hutool-all + 4.6.16 + + + com.alibaba.fastjson2 + fastjson2 + 2.0.43 + + + + + + + + + + + + + diff --git a/sgzb-common/sgzb-common-security/src/main/java/com/bonus/sgzb/common/security/utils/GetTokenByAppKey.java b/sgzb-common/sgzb-common-security/src/main/java/com/bonus/sgzb/common/security/utils/GetTokenByAppKey.java new file mode 100644 index 0000000..88117ce --- /dev/null +++ b/sgzb-common/sgzb-common-security/src/main/java/com/bonus/sgzb/common/security/utils/GetTokenByAppKey.java @@ -0,0 +1,49 @@ +package com.bonus.sgzb.common.security.utils; + +import cn.hutool.crypto.Mode; +import cn.hutool.crypto.Padding; +import cn.hutool.crypto.symmetric.AES; +import com.alibaba.fastjson2.JSONObject; +import java.nio.charset.StandardCharsets; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; + +public class GetTokenByAppKey { + public static void main(String[] args) { + String token = getToken(); + System.err.println(token); + } + + /** + * 获取Token + * @return Token + * appKey、aesKey联系智慧工程系统提供 + */ + private static String getToken() { + String appKey = "abc123"; + String aesKey = "abcdefghijklmnop"; + Map signatureMap = new HashMap(); + signatureMap.put("appKey", appKey); + signatureMap.put("timestamp", String.valueOf(System.currentTimeMillis())); + Iterator iterator = signatureMap.keySet().iterator(); + //Map 转成 JSONObject 字符串 + JSONObject jsonObj = new JSONObject(signatureMap); + String jsonStr = jsonObj.toJSONString(); + + AES aes = new AES(Mode.ECB, Padding.PKCS5Padding, aesKey.getBytes()); + byte[] aesByte = aes.encrypt(jsonStr.getBytes(StandardCharsets.UTF_8)); + String token = byteToHex(aesByte); + return token; + } + + public static String byteToHex(byte[] bytes) { + String strHex = ""; + StringBuilder sb = new StringBuilder(""); + for (int n = 0; n < bytes.length; n++) { + strHex = Integer.toHexString(bytes[n] & 0xFF); + sb.append((strHex.length() == 1) ? "0" + strHex : strHex); // 每个字节由两个字符表示,位数不够,高位补0 + } + return sb.toString().trim(); + } +} \ No newline at end of file