mirror of
https://gitee.com/dromara/sa-token.git
synced 2025-09-18 17:48:03 +08:00
整合 jwt 临时令牌鉴权
This commit is contained in:
@@ -22,6 +22,7 @@
|
||||
<module>sa-token-spring-aop</module>
|
||||
<!-- <module>sa-token-oauth2</module> -->
|
||||
<module>sa-token-quick-login</module>
|
||||
<module>sa-token-temp-jwt</module>
|
||||
</modules>
|
||||
|
||||
</project>
|
12
sa-token-plugin/sa-token-temp-jwt/.gitignore
vendored
Normal file
12
sa-token-plugin/sa-token-temp-jwt/.gitignore
vendored
Normal file
@@ -0,0 +1,12 @@
|
||||
target/
|
||||
|
||||
node_modules/
|
||||
bin/
|
||||
.settings/
|
||||
unpackage/
|
||||
.classpath
|
||||
.project
|
||||
|
||||
.factorypath
|
||||
|
||||
.idea/
|
40
sa-token-plugin/sa-token-temp-jwt/pom.xml
Normal file
40
sa-token-plugin/sa-token-temp-jwt/pom.xml
Normal file
@@ -0,0 +1,40 @@
|
||||
<?xml version='1.0' encoding='utf-8'?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<groupId>cn.dev33</groupId>
|
||||
<artifactId>sa-token-plugin</artifactId>
|
||||
<version>1.19.0</version>
|
||||
</parent>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>sa-token-temp-jwt</name>
|
||||
<artifactId>sa-token-temp-jwt</artifactId>
|
||||
<description>sa-token-temp-jwt</description>
|
||||
|
||||
<dependencies>
|
||||
<!-- sa-token-spring-boot-starter -->
|
||||
<dependency>
|
||||
<groupId>cn.dev33</groupId>
|
||||
<artifactId>sa-token-core</artifactId>
|
||||
<version>${sa-token-version}</version>
|
||||
</dependency>
|
||||
<!-- jwt -->
|
||||
<dependency>
|
||||
<groupId>io.jsonwebtoken</groupId>
|
||||
<artifactId>jjwt</artifactId>
|
||||
<version>0.9.1</version>
|
||||
</dependency>
|
||||
<!-- spring-boot-configuration -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-configuration-processor</artifactId>
|
||||
<version>2.0.0.RELEASE</version>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
@@ -0,0 +1,114 @@
|
||||
package cn.dev33.satoken.temp.jwt;
|
||||
|
||||
import cn.dev33.satoken.dao.SaTokenDao;
|
||||
import cn.dev33.satoken.exception.SaTokenException;
|
||||
import io.jsonwebtoken.Claims;
|
||||
import io.jsonwebtoken.JwtBuilder;
|
||||
import io.jsonwebtoken.Jwts;
|
||||
import io.jsonwebtoken.SignatureAlgorithm;
|
||||
|
||||
/**
|
||||
* jwt操作工具类
|
||||
* @author kong
|
||||
*
|
||||
*/
|
||||
public class SaJwtUtil {
|
||||
|
||||
/**
|
||||
* key: value
|
||||
*/
|
||||
public static final String KEY_VALUE = "value";
|
||||
|
||||
/**
|
||||
* key: 有效期 (时间戳)
|
||||
*/
|
||||
public static final String KEY_EFF = "eff";
|
||||
|
||||
/** 当有效期被设为此值时,代表永不过期 */
|
||||
public static final long NEVER_EXPIRE = SaTokenDao.NEVER_EXPIRE;
|
||||
|
||||
/**
|
||||
* 根据指定值创建 jwt-token
|
||||
* @param value 要保存的值
|
||||
* @param timeout token有效期 (单位 秒)
|
||||
* @param keyt 秘钥
|
||||
* @return jwt-token
|
||||
*/
|
||||
public static String createToken(Object value, long timeout, String keyt) {
|
||||
// 计算eff有效期
|
||||
long eff = timeout;
|
||||
if(timeout != NEVER_EXPIRE) {
|
||||
eff = timeout * 1000 + System.currentTimeMillis();
|
||||
}
|
||||
// 在这里你可以使用官方提供的claim方法构建载荷,也可以使用setPayload自定义载荷,但是两者不可一起使用
|
||||
JwtBuilder builder = Jwts.builder()
|
||||
// .setHeaderParam("typ", "JWT")
|
||||
.claim(KEY_VALUE, value)
|
||||
.claim(KEY_EFF, eff)
|
||||
.signWith(SignatureAlgorithm.HS256, keyt.getBytes());
|
||||
// 生成jwt-token
|
||||
return builder.compact();
|
||||
}
|
||||
|
||||
/**
|
||||
* 从一个 jwt-token 解析出载荷
|
||||
* @param jwtToken JwtToken值
|
||||
* @param keyt 秘钥
|
||||
* @return Claims对象
|
||||
*/
|
||||
public static Claims parseToken(String jwtToken, String keyt) {
|
||||
// 解析出载荷
|
||||
Claims claims = Jwts.parser()
|
||||
.setSigningKey(keyt.getBytes())
|
||||
.parseClaimsJws(jwtToken).getBody();
|
||||
// 返回
|
||||
return claims;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从一个 jwt-token 解析出载荷, 并取出数据
|
||||
* @param jwtToken JwtToken值
|
||||
* @param keyt 秘钥
|
||||
* @return 值
|
||||
*/
|
||||
public static Object getValue(String jwtToken, String keyt) {
|
||||
// 取出数据
|
||||
Claims claims = parseToken(jwtToken, keyt);
|
||||
|
||||
// 验证是否超时
|
||||
Long eff = claims.get(KEY_EFF, Long.class);
|
||||
if((eff == null || eff < System.currentTimeMillis()) && eff != NEVER_EXPIRE) {
|
||||
throw new SaTokenException("Token已超时");
|
||||
}
|
||||
|
||||
// 获取数据
|
||||
return claims.get(KEY_VALUE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 从一个 jwt-token 解析出载荷, 并取出其剩余有效期
|
||||
* @param jwtToken JwtToken值
|
||||
* @param keyt 秘钥
|
||||
* @return 值
|
||||
*/
|
||||
public static long getTimeout(String jwtToken, String keyt) {
|
||||
// 取出数据
|
||||
Claims claims = parseToken(jwtToken, keyt);
|
||||
|
||||
// 验证是否超时
|
||||
Long eff = claims.get(KEY_EFF, Long.class);
|
||||
|
||||
// 永不过期
|
||||
if(eff == NEVER_EXPIRE) {
|
||||
return NEVER_EXPIRE;
|
||||
}
|
||||
// 已经超时
|
||||
if(eff == null || eff < System.currentTimeMillis()) {
|
||||
return SaTokenDao.NOT_VALUE_EXPIRE;
|
||||
}
|
||||
|
||||
// 计算timeout
|
||||
return (eff - System.currentTimeMillis()) / 1000;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,51 @@
|
||||
package cn.dev33.satoken.temp.jwt;
|
||||
|
||||
import cn.dev33.satoken.SaManager;
|
||||
import cn.dev33.satoken.exception.SaTokenException;
|
||||
import cn.dev33.satoken.temp.SaTempInterface;
|
||||
import cn.dev33.satoken.util.SaFoxUtil;
|
||||
|
||||
/**
|
||||
* Sa-Token 临时令牌验证模块接口 JWT实现类
|
||||
* @author kong
|
||||
*
|
||||
*/
|
||||
public class SaTempForJwt implements SaTempInterface {
|
||||
|
||||
/**
|
||||
* 根据value创建一个token
|
||||
*/
|
||||
public String createToken(Object value, long timeout) {
|
||||
String token = SaJwtUtil.createToken(value, timeout, getJwtSecretKey());
|
||||
return token;
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析token获取value
|
||||
*/
|
||||
public Object parseToken(String token) {
|
||||
Object value = SaJwtUtil.getValue(token, getJwtSecretKey());
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回指定token的剩余有效期,单位:秒
|
||||
*/
|
||||
public long getTimeout(String token) {
|
||||
long timeout = SaJwtUtil.getTimeout(token, getJwtSecretKey());
|
||||
return timeout;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取jwt秘钥
|
||||
* @return jwt秘钥
|
||||
*/
|
||||
public String getJwtSecretKey() {
|
||||
String jwtSecretKey = SaManager.getConfig().getJwtSecretKey();
|
||||
if(SaFoxUtil.isEmpty(jwtSecretKey)) {
|
||||
throw new SaTokenException("请配置:jwtSecretKey");
|
||||
}
|
||||
return jwtSecretKey;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=cn.dev33.satoken.temp.jwt.SaTempForJwt
|
Reference in New Issue
Block a user