diff --git a/pom.xml b/pom.xml
index ee65be1..37eeb49 100644
--- a/pom.xml
+++ b/pom.xml
@@ -17,6 +17,7 @@
17
3.5.1
6.0.0-SNAPSHOT
+ 0.11.5
@@ -72,6 +73,26 @@
caffeine
+
+
+ io.jsonwebtoken
+ jjwt-api
+ ${jjwt.version}
+
+
+ io.jsonwebtoken
+ jjwt-impl
+ ${jjwt.version}
+ runtime
+
+
+ io.jsonwebtoken
+ jjwt-jackson
+ ${jjwt.version}
+ runtime
+
+
+
org.springframework.boot
spring-boot-devtools
diff --git a/src/main/java/io/github/xxyopen/novel/core/constant/SystemConfigConsts.java b/src/main/java/io/github/xxyopen/novel/core/constant/SystemConfigConsts.java
index bd5946b..77fafed 100644
--- a/src/main/java/io/github/xxyopen/novel/core/constant/SystemConfigConsts.java
+++ b/src/main/java/io/github/xxyopen/novel/core/constant/SystemConfigConsts.java
@@ -12,6 +12,21 @@ public class SystemConfigConsts {
throw new IllegalStateException("Constant class");
}
+ /**
+ * 前台门户系统标识
+ * */
+ public static final String NOVEL_FRONT_KEY = "front";
+
+ /**
+ * 作家管理系统标识
+ * */
+ public static final String NOVEL_AUTHOR_KEY = "author";
+
+ /**
+ * 后台管理系统标识
+ * */
+ public static final String NOVEL_ADMIN_KEY = "admin";
+
/**
* 小说前台门户系统域
* */
diff --git a/src/main/java/io/github/xxyopen/novel/core/util/JwtUtils.java b/src/main/java/io/github/xxyopen/novel/core/util/JwtUtils.java
new file mode 100644
index 0000000..5bb1bdd
--- /dev/null
+++ b/src/main/java/io/github/xxyopen/novel/core/util/JwtUtils.java
@@ -0,0 +1,77 @@
+package io.github.xxyopen.novel.core.util;
+
+import io.jsonwebtoken.Claims;
+import io.jsonwebtoken.Jws;
+import io.jsonwebtoken.JwtException;
+import io.jsonwebtoken.Jwts;
+import io.jsonwebtoken.security.Keys;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
+import org.springframework.stereotype.Component;
+
+import java.nio.charset.StandardCharsets;
+import java.util.Objects;
+
+/**
+ * JWT 工具类
+ *
+ * @author xiongxiaoyang
+ * @date 2022/5/17
+ */
+@ConditionalOnProperty("jwt.secret")
+@Component
+@Slf4j
+public class JwtUtils {
+
+ /**
+ * 注入JWT加密密钥
+ */
+ @Value("${jwt.secret}")
+ private String secret;
+
+ /**
+ * 定义系统标识头常量
+ */
+ private static final String HEADER_SYSTEM_KEY = "systemKeyHeader";
+
+ /**
+ * 根据用户ID生成JWT
+ * @param uid 用户ID
+ * @param systemKey 系统标识
+ * @return JWT
+ */
+ public String generateToken(Long uid, String systemKey) {
+ return Jwts.builder()
+ .setHeaderParam(HEADER_SYSTEM_KEY, systemKey)
+ .setSubject(uid.toString())
+ .signWith(Keys.hmacShaKeyFor(secret.getBytes(StandardCharsets.UTF_8)))
+ .compact();
+ }
+
+ /**
+ * 解析JWT返回用户ID
+ * @param token JWT
+ * @param systemKey 系统标识
+ * @return 用户ID
+ */
+ public Long parseToken(String token, String systemKey) {
+ Jws claimsJws;
+ try {
+ claimsJws = Jwts.parserBuilder()
+ .setSigningKey(Keys.hmacShaKeyFor(secret.getBytes(StandardCharsets.UTF_8)))
+ .build()
+ .parseClaimsJws(token);
+ // OK, we can trust this JWT
+ // 判断该 JWT 是否属于指定系统
+ if (Objects.equals(claimsJws.getHeader().get(HEADER_SYSTEM_KEY), systemKey)) {
+ return Long.parseLong(claimsJws.getBody().getSubject());
+ }
+ } catch (JwtException e) {
+ log.warn("JWT解析失败:{}", token);
+ // don't trust the JWT!
+ }
+ return null;
+ }
+
+}
diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml
index f49b686..e8452a7 100644
--- a/src/main/resources/application.yml
+++ b/src/main/resources/application.yml
@@ -17,7 +17,7 @@ server:
---
spring:
datasource:
- url: jdbc:mysql://localhost:3306/novel?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
+ url: jdbc:mysql://localhost:3306/novel_test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
username: root
password: test123456
config:
@@ -36,3 +36,12 @@ spring:
activate:
on-profile: dev
+
+---
+spring:
+ config:
+ activate:
+ on-profile: dev
+# JWT密钥
+jwt:
+ secret: E66559580A1ADF48CDD928516062F12E
\ No newline at end of file