JWT工具类

<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.7.15</version>
</dependency>
import cn.hutool.core.date.DateUtil;
import cn.hutool.jwt.JWT;
import cn.hutool.jwt.signers.JWTSignerUtil;

/**
 * @author 王令
 */
public class JwtUtils {

    /**
     * 秘钥
     */
    private static final String TOKEN_SECRET = "ERcoohpGCIrDY";
    /**
     * 默认过期时间
     */
    private static final Integer EXPIRE = 7;

    /**
     * 获取默认过期时间的jwt签名器
     * @return
     */
    public static JWT getJwt(){
        return getJwt(EXPIRE);
    }

    /**
     * 获取自定义过期时间的jwt签名器
     * @param expire
     * @return
     */
    public static JWT getJwt(Integer expire){
        return getJWT().setExpiresAt(DateUtil.offsetDay(DateUtil.date(), expire));
    }

    /**
     * 生成jwt签名器
     * @return
     */
    private static JWT getJWT(){
        return JWT.create().setSigner(JWTSignerUtil.hs256(TOKEN_SECRET.getBytes()));
    }

    /**
     * 解析token
     * @param token
     * @return
     */
    public static JWT parse(String token){
        return getJWT().parse(token);
    }
}
posted @ 2021-10-27 09:47  王^.^令  阅读(513)  评论(0)    收藏  举报