mirror of
https://github.com/201206030/novel.git
synced 2025-04-27 07:30:50 +00:00
build: 集成 JWT 支持
This commit is contained in:
parent
ceea4b58e6
commit
3092635141
21
pom.xml
21
pom.xml
@ -17,6 +17,7 @@
|
|||||||
<java.version>17</java.version>
|
<java.version>17</java.version>
|
||||||
<mybatis-plus.version>3.5.1</mybatis-plus.version>
|
<mybatis-plus.version>3.5.1</mybatis-plus.version>
|
||||||
<spring.version>6.0.0-SNAPSHOT</spring.version>
|
<spring.version>6.0.0-SNAPSHOT</spring.version>
|
||||||
|
<jjwt.version>0.11.5</jjwt.version>
|
||||||
</properties>
|
</properties>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
@ -72,6 +73,26 @@
|
|||||||
<artifactId>caffeine</artifactId>
|
<artifactId>caffeine</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!-- JWT 相关 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.jsonwebtoken</groupId>
|
||||||
|
<artifactId>jjwt-api</artifactId>
|
||||||
|
<version>${jjwt.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.jsonwebtoken</groupId>
|
||||||
|
<artifactId>jjwt-impl</artifactId>
|
||||||
|
<version>${jjwt.version}</version>
|
||||||
|
<scope>runtime</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.jsonwebtoken</groupId>
|
||||||
|
<artifactId>jjwt-jackson</artifactId> <!-- or jjwt-gson if Gson is preferred -->
|
||||||
|
<version>${jjwt.version}</version>
|
||||||
|
<scope>runtime</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-devtools</artifactId>
|
<artifactId>spring-boot-devtools</artifactId>
|
||||||
|
@ -12,6 +12,21 @@ public class SystemConfigConsts {
|
|||||||
throw new IllegalStateException("Constant class");
|
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";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 小说前台门户系统域
|
* 小说前台门户系统域
|
||||||
* */
|
* */
|
||||||
|
@ -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<Claims> 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -17,7 +17,7 @@ server:
|
|||||||
---
|
---
|
||||||
spring:
|
spring:
|
||||||
datasource:
|
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
|
username: root
|
||||||
password: test123456
|
password: test123456
|
||||||
config:
|
config:
|
||||||
@ -36,3 +36,12 @@ spring:
|
|||||||
activate:
|
activate:
|
||||||
on-profile: dev
|
on-profile: dev
|
||||||
|
|
||||||
|
|
||||||
|
---
|
||||||
|
spring:
|
||||||
|
config:
|
||||||
|
activate:
|
||||||
|
on-profile: dev
|
||||||
|
# JWT密钥
|
||||||
|
jwt:
|
||||||
|
secret: E66559580A1ADF48CDD928516062F12E
|
Loading…
x
Reference in New Issue
Block a user