All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gccloud.starter.common.utils.JwtUtils Maven / Gradle / Ivy

package com.gccloud.starter.common.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.util.Base64Utils;

/**
 * jwt工具类
 *
 * @author liuchengbiao
 * @date 2021/7/6 2:17 下午
 */
@Slf4j
public class JwtUtils {

    /**
     * 解析jwt,不进行安全校验
     *
     * @param token
     * @return
     */
    public static JSONObject parseWithOutValidate(String token) {
        try {
            String payloadBase64 = StringUtils.substringBetween(token, ".");
            String payload = new String(Base64Utils.decodeFromString(payloadBase64));
            JSONObject tokenObj = JSON.parseObject(payload);
            return tokenObj;
        } catch (Exception e) {
            log.error(ExceptionUtils.getStackTrace(e));
        }
        return null;
    }

    public static void main(String[] args) {
        JSONObject data = parseWithOutValidate("eyJhbGciOiJIUzI1NiJ9.eyJ1bmFtZSI6ImFkbWluIiwicm5hbWUiOiLnrqHnkIblkZgiLCJpc3MiOiJnYyIsIm9pZCI6IjEiLCJ0aWQiOiIyIiwiZHBUeXBlIjo1LCJ1aWQiOiIyIiwiYXVkIjoiMiIsInRtIjp0cnVlLCJpZCI6IjE0MTIyNTAwNTkyMjI4OTI1NDYiLCJleHAiOjE2MjU1NDg4MjcsImlhdCI6MTYyNTU0MTYyNywianRpIjoiMTQxMjI1MDA1OTIyMjg5MjU0NiJ9.9mfbDvXSjP28gNBINzimFFUHj7UNmLaqms_PcDmYrpY");
        System.out.println(data);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy