首先要导入对应的Maven依赖
对Token进行加密
先定义两个常量
private final static String secret = "yunhao"; // 目的是加盐,内容随意
private JWTVerifier verifier;
// 加密过程
public ApiTokenResult createToken(Integer userId, String userUuid) {
try {
Algorithm algorithm = Algorithm.HMAC256(secret);
String token = JWT.create().withClaim("uid", userUuid).sign(algorithm); // 创建token
ApiTokenResult result = new ApiTokenResult(userId.toString(), userUuid, token);
redisTemplete.boundValueOps(userUuid).set(result, Constants.TOKEN_EXPIRES_DAY, TimeUnit.DAYS);
return result;
} catch (Exception exception) {
throw new AppRuntimeException("token生成失败");