package cn.huge.base.common.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; /** * 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.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; } /** * jwt用户 */ private static String ISSUSER = "HUGEINFO"; /** * 生成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; } /** * 生成讯飞token * @return */ public static String buildXfJWT(){ //生成jwt Date time = new Date(); String secret = "D34F84C9963548A9BA4A70AD6D61A99E"; String appId = "1000000001"; Algorithm algorithm = Algorithm.HMAC256(secret); String auth = JWT.create() .withIssuer(appId) .withClaim("time", time+"") .sign(algorithm); return "Bearer "+auth; } public static void main(String[] args) { String auth = buildXfJWT(); System.out.println(auth); } } /** * -------------------_ooOoo_------------------- * ------------------o8888888o------------------ * ------------------88" . "88------------------ * ------------------(| -_- |)------------------ * ------------------O\ = /O------------------ * ---------------____/`---'\____--------------- * -------------.' \\| |// `.------------- * ------------/ \\||| : |||// \------------ * -----------/ _||||| -:- |||||- \----------- * -----------| | \\\ - /// | |----------- * -----------| \_| ''\---/'' | |----------- * -----------\ .-\__ `-` ___/-. /----------- * ---------___`. .' /--.--\ `. . __---------- * ------."" '< `.___\_<|>_/___.' >'"".------- * -----| | : `- \`.;`\ _ /`;.`/ - ` : | |----- * -----\ \ `-. \_ __\ /__ _/ .-` / /----- * ======`-.____`-.___\_____/___.-`____.-'====== * -------------------`=---=' * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ * ---------佛祖保佑---hugeinfo---永无BUG---------- */