package cn.huge.module.utils;
|
|
import com.auth0.jwt.JWT;
|
import com.auth0.jwt.algorithms.Algorithm;
|
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.stereotype.Component;
|
|
import java.util.Date;
|
|
/**
|
* @title: JWT工具类
|
* @description: JWT工具类
|
* @company: hugeinfo
|
* @author: liyj
|
* @time: 2021-11-05 16:51:48
|
* @version: 1.0.0
|
*/
|
@Component
|
public class JwtUtils {
|
|
/**
|
* jwt生成密钥
|
*/
|
public static String secretKey;
|
|
/**
|
* jwt生成密钥
|
*/
|
public static String issUser;
|
|
/**
|
* token过期时间:4小时
|
*/
|
public static long tokenExpireTime;
|
|
/**
|
* refreshToken过期时间:比token时间长一点
|
*/
|
public static long refreshTokenExpireTime;
|
|
/**
|
* refreshToken 存储key
|
*/
|
public static String jwtRefreshTokenKeyFormat;
|
|
/**
|
* token黑名单 存储key
|
*/
|
public static String jwtBlacklistKeyFormat;
|
|
@Value("${jwt.secret-key}")
|
public void setSecretKey(String secret_Key) {
|
secretKey = secret_Key;
|
}
|
|
@Value("${jwt.iss-user}")
|
public void setIssUser(String iss_user) {
|
issUser = iss_user;
|
}
|
|
@Value("${jwt.token.expire-time}")
|
public void setTokenExpireTime(long token_expire_time) {
|
tokenExpireTime = token_expire_time;
|
}
|
|
@Value("${jwt.refresh-token.expire-time}")
|
public void setRefreshTokenExpireTime(long refresh_token_expire_time) {
|
refreshTokenExpireTime = refresh_token_expire_time;
|
}
|
|
@Value("${jwt.refresh-token-key.format}")
|
public void setJwtRefreshTokenKeyFormat(String jwt_refresh_token_key_format) {
|
jwtRefreshTokenKeyFormat = jwt_refresh_token_key_format;
|
}
|
|
@Value("${jwt.blacklist-key.format}")
|
public void setJwtBlacklistKeyFormat(String jwt_blacklist_key_format) {
|
jwtBlacklistKeyFormat = jwt_blacklist_key_format;
|
}
|
|
/**
|
* 生成token
|
* @param userId 登录用户标识
|
* @return String
|
*/
|
public static String buildJWT(String userId){
|
//生成jwt
|
Date now = new Date();
|
Algorithm algo = Algorithm.HMAC256(secretKey);
|
String token = JWT.create()
|
.withIssuer(issUser)
|
.withIssuedAt(now)
|
.withExpiresAt(new Date(now.getTime() + tokenExpireTime))
|
.withClaim("userId", userId)
|
.sign(algo);
|
return token;
|
}
|
|
/**
|
* 生成token
|
* @param custId 平台客户标识
|
* @param userId 登录用户标识
|
* @return String
|
*/
|
public static String buildJWT(String custId, String userId){
|
//生成jwt
|
Date now = new Date();
|
Algorithm algo = Algorithm.HMAC256(secretKey);
|
String token = JWT.create()
|
.withIssuer(issUser)
|
.withIssuedAt(now)
|
.withExpiresAt(new Date(now.getTime() + tokenExpireTime))
|
.withClaim("custId", custId)
|
.withClaim("userId", userId)
|
.sign(algo);
|
return token;
|
}
|
}
|
/**
|
* -------------------_ooOoo_-------------------
|
* ------------------o8888888o------------------
|
* ------------------88" . "88------------------
|
* ------------------(| -_- |)------------------
|
* ------------------O\ = /O------------------
|
* ---------------____/`---'\____---------------
|
* -------------.' \\| |// `.-------------
|
* ------------/ \\||| : |||// \------------
|
* -----------/ _||||| -:- |||||- \-----------
|
* -----------| | \\\ - /// | |-----------
|
* -----------| \_| ''\---/'' | |-----------
|
* -----------\ .-\__ `-` ___/-. /-----------
|
* ---------___`. .' /--.--\ `. . __----------
|
* ------."" '< `.___\_<|>_/___.' >'"".-------
|
* -----| | : `- \`.;`\ _ /`;.`/ - ` : | |-----
|
* -----\ \ `-. \_ __\ /__ _/ .-` / /-----
|
* ======`-.____`-.___\_____/___.-`____.-'======
|
* -------------------`=---='
|
* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
* ---------佛祖保佑---hugeinfo---永无BUG----------
|
*/
|