JWTUtil工具类


package com.codegzy.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;

import java.util.Calendar;
import java.util.Map;

public class JWTUtil {

    private static final String SIGN = "da5c4a56112326@564#546c1!?SCasd";

    //获取token
    public static String getToken(Map<String, String> map, Integer day) {
        //token的保质期
        Calendar instance = Calendar.getInstance();
        instance.add(Calendar.DATE, day);

        //创建jwt
        JWTCreator.Builder builder = JWT.create();

        //头部使用默认
        //添加payload
        map.forEach(builder::withClaim);

        //添加过期时间和签名
        return builder.withExpiresAt(instance.getTime()).sign(Algorithm.HMAC256(SIGN));
    }

    //验证token    //此步可能会有异常,外部抓一下
    public static DecodedJWT verifyToken(String token) {
        return JWT.require(Algorithm.HMAC256(SIGN)).build().verify(token);
    }
}


JWTUtil工具类

上一篇:WCF X509Certificates证书认证


下一篇:内存字符串暴力搜索定位代码