package com.cm;

import com.alibaba.fastjson.JSONObject;
import com.auth0.jwt.JWT;
import com.auth0.jwt.impl.JWTParser;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;
import org.junit.Test;

import java.util.Map;

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: JwtTest
 * @Description:
 * @Author: WangGeng
 * @Date: 2019/8/29 3:22 下午
 * @Version: 1.0
 **/
public class JwtTest {

    @Test
    public void jwtParse() {
        String accessToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2luZm8iOnsicGFzc3dvcmQiOm51bGwsInVzZXJuYW1lIjoiYWRtaW4iLCJhdXRob3JpdGllcyI6W3sicm9sZUlkIjoiIiwicm9sZU5hbWUiOiIiLCJhcGlTYXZlTWVudSI6bnVsbCwiYXBpRGVsZXRlTWVudSI6bnVsbCwiYXBpVXBkYXRlTWVudSI6bnVsbCwiYXBpUXVlcnlNZW51IjpudWxsLCJyZXNvdXJjZVNhdmVNZW51IjpudWxsLCJyZXNvdXJjZURlbGV0ZU1lbnUiOm51bGwsInJlc291cmNlVXBkYXRlTWVudSI6bnVsbCwicmVzb3VyY2VRdWVyeU1lbnUiOm51bGwsInJvdXRlU2F2ZU1lbnUiOm51bGwsInJvdXRlVXBkYXRlTWVudSI6bnVsbCwicm91dGVRdWVyeU1lbnUiOm51bGwsImF1dGhvcml0eSI6IlJPTEVfQUxMIn0seyJyb2xlSWQiOiIiLCJyb2xlTmFtZSI6IiIsImFwaVNhdmVNZW51IjpudWxsLCJhcGlEZWxldGVNZW51IjpudWxsLCJhcGlVcGRhdGVNZW51IjpudWxsLCJhcGlRdWVyeU1lbnUiOm51bGwsInJlc291cmNlU2F2ZU1lbnUiOm51bGwsInJlc291cmNlRGVsZXRlTWVudSI6bnVsbCwicmVzb3VyY2VVcGRhdGVNZW51IjpudWxsLCJyZXNvdXJjZVF1ZXJ5TWVudSI6bnVsbCwicm91dGVTYXZlTWVudSI6bnVsbCwicm91dGVVcGRhdGVNZW51IjpudWxsLCJyb3V0ZVF1ZXJ5TWVudSI6bnVsbCwiYXV0aG9yaXR5IjoiUk9MRV9HUk9VUF9BTEwifV0sImFjY291bnROb25FeHBpcmVkIjp0cnVlLCJhY2NvdW50Tm9uTG9ja2VkIjp0cnVlLCJjcmVkZW50aWFsc05vbkV4cGlyZWQiOnRydWUsImVuYWJsZWQiOnRydWUsInVzZXJJZCI6IjEiLCJ1c2VyTmFtZSI6ImFkbWluIiwidXNlclBob25lIjoiMTg4ODg4ODg4ODgiLCJyb2xlcyI6bnVsbCwiZ3JvdXBzIjpudWxsLCJkZXBhcnRtZW50cyI6bnVsbH0sInVzZXJfbmFtZSI6ImFkbWluIiwic2NvcGUiOlsiYWxsIl0sImV4cCI6MTU2NzA2NzgyNSwiYXV0aG9yaXRpZXMiOlsie1wiYXV0aG9yaXR5XCI6XCJST0xFX0FMTFwiX3dnX1wicm9sZUlkXCI6XCJcIl93Z19cInJvbGVOYW1lXCI6XCJcIn0iLCJ7XCJhdXRob3JpdHlcIjpcIlJPTEVfR1JPVVBfQUxMXCJfd2dfXCJyb2xlSWRcIjpcIlwiX3dnX1wicm9sZU5hbWVcIjpcIlwifSJdLCJqdGkiOiI0MmRiMGY2Ny03Yzc0LTRmMzctYTljZS1kZTA2N2U3MDA2NTYiLCJjbGllbnRfaWQiOiJkMjlmNDg4MGI4ZGY0NmVhYjA5MDMzOWNhYzA1NjgwOSJ9.gMsExojfKVgiHx8SOCWWU55DfQAB-xftIokbMETnKvE";
        DecodedJWT decodedJWT = JWT.decode(accessToken);
        Claim claim = decodedJWT.getClaim("user_info");
        JSONObject cliamObj = claim.as(JSONObject.class);
        String userId = cliamObj.getString("userId");
        String userName = cliamObj.getString("userName");
        System.out.println(String.format("%s:%s", userId, userName));
    }

}
