利用hutool生成和验证JWT

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.jwt.JWT;
import cn.hutool.jwt.JWTPayload;
import cn.hutool.jwt.JWTUtil;

import java.util.Map;

public class JwtUtil {

    private static final String KEY = "wtw";

    public static String createToken(Map<String, Object> payload, DateField dateField, int time) {
        DateTime now = DateTime.now();
        DateTime newTime = now.offsetNew(dateField, time);

        //签发时间
        payload.put(JWTPayload.ISSUED_AT, now);

        //过期时间
        payload.put(JWTPayload.EXPIRES_AT, newTime);

        //生效时间
        payload.put(JWTPayload.NOT_BEFORE, now);

        String token = JWTUtil.createToken(payload, KEY.getBytes());

        return token;
    }

    public static JWTPayload parseToken(String token) {
        JWT jwt = JWTUtil.parseToken(token);
        if(!jwt.setKey(KEY.getBytes()).verify()) {
            throw new RuntimeException("token 异常");
        }

        if(!jwt.validate(0)) {
            throw new RuntimeException("token 已过期请重新登录");
        }

        return jwt.getPayload();
    }

}

你可能感兴趣的:(Java,java,spring,servlet)