From 87adf2aca59a37b1916f023496ddd2c8addb5d57 Mon Sep 17 00:00:00 2001
From: zhaolongfei <2651195677@qq.com>
Date: Wed, 6 Nov 2024 09:03:06 +0800
Subject: [PATCH] =?UTF-8?q?=E5=9C=A8=E7=BA=BF=E8=A7=86=E9=A2=91=E6=92=AD?=
=?UTF-8?q?=E6=94=BE=E6=97=B6=E8=BF=9B=E8=A1=8Ctoken=E5=92=8C=E5=B7=A5?=
=?UTF-8?q?=E5=8F=B7=E9=AA=8C=E8=AF=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
servers/boe-server-all/pom.xml | 8 ++-
.../com/xboe/config/ConditionException.java | 25 ++++++++
.../main/java/com/xboe/config/JwtUtils.java | 63 +++++++++++++++++++
.../java/com/xboe/constants/CacheName.java | 2 +
.../xboe/module/course/api/CourseWareApi.java | 36 ++++++++---
5 files changed, 123 insertions(+), 11 deletions(-)
create mode 100644 servers/boe-server-all/src/main/java/com/xboe/config/ConditionException.java
create mode 100644 servers/boe-server-all/src/main/java/com/xboe/config/JwtUtils.java
diff --git a/servers/boe-server-all/pom.xml b/servers/boe-server-all/pom.xml
index de0b436d..ba338021 100644
--- a/servers/boe-server-all/pom.xml
+++ b/servers/boe-server-all/pom.xml
@@ -205,7 +205,11 @@
system
${project.basedir}/src/main/resources/aspose/aspose-cells-java-18.11.jar
-
+
+ com.alibaba
+ fastjson
+ 2.0.17.graal
+
com.github.ulisesbocchio
@@ -232,7 +236,7 @@
spring-retry
1.3.1
-
+
diff --git a/servers/boe-server-all/src/main/java/com/xboe/config/ConditionException.java b/servers/boe-server-all/src/main/java/com/xboe/config/ConditionException.java
new file mode 100644
index 00000000..872a71af
--- /dev/null
+++ b/servers/boe-server-all/src/main/java/com/xboe/config/ConditionException.java
@@ -0,0 +1,25 @@
+package com.xboe.config;
+
+public class ConditionException extends RuntimeException{
+ private Integer code;
+ private String message;
+
+
+ public ConditionException(Integer code, String message) {
+ this.code = code;
+ this.message = message;
+ }
+
+ public ConditionException(String message) {
+ this(600, message);
+ }
+
+ public Integer getCode() {
+ return this.code;
+ }
+
+ @Override
+ public String getMessage() {
+ return message;
+ }
+}
diff --git a/servers/boe-server-all/src/main/java/com/xboe/config/JwtUtils.java b/servers/boe-server-all/src/main/java/com/xboe/config/JwtUtils.java
new file mode 100644
index 00000000..bfccfc29
--- /dev/null
+++ b/servers/boe-server-all/src/main/java/com/xboe/config/JwtUtils.java
@@ -0,0 +1,63 @@
+package com.xboe.config;
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
+import com.alibaba.fastjson.TypeReference;
+import com.xboe.common.utils.Sha256Mac;
+import org.apache.commons.codec.binary.Base64;
+
+import java.util.Map;
+
+/**
+ * Jwt工具类
+ *
+ * @author ruoyi
+ */
+public class JwtUtils {
+
+ public static final String secretKey = "JDF_BOE";
+
+ /**
+ * 从数据声明生成令牌
+ *
+ * @param claims 数据声明
+ * @return 令牌
+ */
+ public static String createToken(Map claims) {
+ JSONObject header = new JSONObject();
+ header.put("alg", "HS256");
+ header.put("type", "token");
+ String payload64 = Base64.encodeBase64String(JSON.toJSONString(claims).getBytes());
+ String header64 = Base64.encodeBase64String(header.toString().getBytes());
+ String sign = Sha256Mac.sha256_mac(header64 + payload64, secretKey);
+ return header64 + "." + payload64 + "." + sign;
+ }
+
+ /**
+ * 从令牌中获取数据声明
+ *
+ * @param token 令牌
+ * @return 数据声明
+ */
+ public static Map parseToken(String token) throws ConditionException {
+ String[] tokens = token.split("\\.");
+ if (tokens.length != 3) {
+ throw new ConditionException("token不合法 : " + token);
+ }
+ String payload = new String(Base64.decodeBase64(tokens[1]));
+ String sign = Sha256Mac.sha256_mac(tokens[0] + tokens[1], secretKey);
+ if (sign.equals(tokens[2])) {
+ JSONObject jsonObject = JSON.parseObject(payload);
+ long exp = jsonObject.getLong("exp");
+ long now = System.currentTimeMillis() / 1000;
+ if (now > exp) {
+ throw new ConditionException("token过期 : " + token);
+ }
+ Map map = JSON.parseObject(payload, new TypeReference