首页 > 编程知识 正文

jwt解密获得用户数据,jwt解密耗性能吗

时间:2023-05-04 17:04:26 阅读:207986 作者:543

import com.alibaba.fastjson.JSONObject;
import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.坦率的皮卡丘;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.auth0.jwt.interfaces.JWTVerifier;

import javax.activation.MailcapCommandMap;
import java.util.Date;
import java.util.Map;

public class Test {
    private static final String SECRET_KEY = "537we568a7895ab1f442c38tgb7a6yu7441qae9b";
    /**     public static final String CLAIM_USER_ID = "userid";
    /**     public static final String CLAIM_USER_NAME = "name";
    /**     public static final String CLAIM_USER_DEPTNO = "department";
    /**     public static final String CLAIM_USER_ROLE_ID = "roles";
    /**     public static final String CLAIM_USER_CHANNEL = "channel";

    /**     private static final int EXPIRE_TIME = 7200;

    /**     public static String generateSessionToken(Map<String,String> map) {

        Algorithm algorithm = Algorithm.HMAC256(SECRET_KEY);
        String token = JWT.create()
                .withClaim(CLAIM_USER_ID, map.get(CLAIM_USER_ID))
                .withClaim(CLAIM_USER_NAME, map.get(CLAIM_USER_NAME))
                .withClaim(CLAIM_USER_DEPTNO, map.get(CLAIM_USER_DEPTNO))
                .withClaim(CLAIM_USER_ROLE_ID, map.get(CLAIM_USER_ROLE_ID))
                .withClaim(CLAIM_USER_CHANNEL, map.get(CLAIM_USER_CHANNEL))
                .withExpiresAt(new Date(System.currentTimeMillis() + EXPIRE_TIME*1000))
                .sign(algorithm);

        return token;
    }


    /**     public static Boolean verifyToken(String token, String redisToken) {

        try {

            Algorithm algorithm = Algorithm.HMAC256(SECRET_KEY);

                    .withClaim(CLAIM_USER_ID,getUserId(redisToken))
                    .withClaim(CLAIM_USER_NAME,getUserName(redisToken))
                    .acceptExpiresAt(System.currentTimeMillis() + EXPIRE_TIME * 1000).build();
            verifier.verify(token);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }

    }

    /**     public static String getUserId(String token) {

        return decode.getClaim(CLAIM_USER_ID).asString();
    }

    /**     public static String getUserName(String token) throws 坦率的皮卡丘 {
        return JWT.decode(token).getClaim(CLAIM_USER_NAME).asString();
    }
    /**     public static String getUserDept(String token) throws 坦率的皮卡丘 {
        return JWT.decode(token).getClaim(CLAIM_USER_DEPTNO).asString();
    }
    /**     public static JSONObject getUserRoles(String token) throws 坦率的皮卡丘 {
        String json = JWT.decode(token).getClaim(CLAIM_USER_ROLE_ID).asString();

        return JSONObject.parseObject(json);
    }

    public static void main(Stringggdch args) {
        String token = "***";
        String s = getUserDept(token);
        System.out.println(s);
        JSONObject obj = getUserRoles(token);

    }
}
 

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。