Jwt生成和解析工具类(万用版,可作为数据存储容器来传输)

Jwt生成和解析工具类(万用版,可作为数据存储容器来传输)packagecom.ciih.authcenter.client.util.jwt;importcom.alibaba.fastjson.JSON;importcom.auth0.jwt.JWT;importcom.auth0.jwt.JWTCreator;importcom.auth0.jwt.JWTVerifier;importcom.auth0.jwt.algorithms.Algorithm;importcom.auth0.jwt.interfaces.Claim;.

大家好,又见面了,我是你们的朋友全栈君。

package com.ciih.authcenter.client.util.jwt;

import com.alibaba.fastjson.JSON;
import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Lenovo
 */
@Slf4j
@Component
public class JwtTokenUtilPlusJ {
    /**
     * 盐
     */
    public static String SECRET = "authcenter";
    private static final long EXPIRATION = 28800000L;

    /*生成token*/
    public static <T> String generateToken(T t) {
        Date expireDate = new Date(System.currentTimeMillis() + EXPIRATION * 1000);
        Date now = new Date();
        Map<String, Object> map = new HashMap<>();
        map.put("alg", "HS256");
        map.put("typ", "JWT");
        JWTCreator.Builder token = JWT.create()
                .withHeader(map)
                .withExpiresAt(expireDate)
                .withIssuedAt(now)
                .withNotBefore(now);

        token.withClaim("data",JSON.toJSONString(t));
        return token.sign(Algorithm.HMAC256(SECRET));
    }

    /*解析token*/
    public static <T> T parseToken(String token, Class<T> aclass) {
        JWTVerifier verifier = JWT.require(Algorithm.HMAC256(SECRET)).build();
        DecodedJWT jwt = verifier.verify(token);
        Map<String, Claim> claims = jwt.getClaims();
        String string = claims.get("data").asString();
        T t = JSON.parseObject(string, aclass);
        log.info("解析Token的内容:" + t);
        return t;
    }

//    public static void main(String[] args) {
//        ArrayList<Map<String,Object>> list = new ArrayList<>();
//        HashMap<String, Object> hashMap = new HashMap<>();
//        HashMap<String, Object> hashMap1 = new HashMap<>();
//        hashMap.put("name", "怕科技的首付款");
//        hashMap.put("nam1e", "怕科技的首付款");
//        hashMap.put("na1me", "怕科技的首付款");
//        hashMap.put("na2me", "怕科技的首付款");
//        hashMap.put("na3me", "怕科技的首付款");
//        hashMap.put("na4me", "怕科技的首付款");
//        hashMap1.put("pas1sword", "水电煤气");
//        hashMap1.put("pas3sword", "水电煤气");
//        hashMap1.put("pass2word", "水电煤气");
//        hashMap1.put("pa4ssword", "水电煤气");
//        hashMap1.put("pa57ssword", "水电煤气");
//        hashMap1.put("passw8ord", "水电煤气");
//        list.add(hashMap);
//        list.add(hashMap1);
//        String s = generateToken(list);
//        System.out.println(s);
//        ArrayList arrayList = parseToken(s, ArrayList.class);
//        System.out.println(arrayList);
//    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/148535.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号