4

How can I get the name and email from a token .

Structure of the token using jwt.io http://prntscr.com/yzyf2b

Any help is appreciated.

Update full solution with the help of below posts so credits to them .

            String jwtToken = token;
            System.out.println("------------ Decode JWT ------------");
            String[] split_string = jwtToken.split("\\.");
            String base64EncodedHeader = split_string[0];
            String base64EncodedBody = split_string[1];
            String base64EncodedSignature = split_string[2];

            System.out.println("~~~~~~~~~ JWT Header ~~~~~~~");
            Base64 base64Url = new Base64(true);
            String header = new String(base64Url.decode(base64EncodedHeader));
            System.out.println("JWT Header : " + header);

            System.out.println("~~~~~~~~~ JWT Body ~~~~~~~");
            String body = new String(base64Url.decode(base64EncodedBody));
            System.out.println("JWT Body : " + body);

            JSONObject jsonObject = new JSONObject(body);
            System.out.println(jsonObject.get("email"));
            System.out.println(jsonObject.get("name"));
        
Frank333
  • 75
  • 2
  • 6

1 Answers1

5

A JWToken has the following structure Header.Body.Signature. Hence, first you should split the token into three parts, namely Header, Body and Signature.

For that you can use

String[] token_part = jwtToken.split("\\.");

then apply what you already have done but for the token_part[1] (i.e., the payload or Body), namely:

sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();
String a = new String(decoder.decodeBuffer(token_part[1]));

For the decoding part, alternatively, you can try this SO thread.

After that you can use the org.json.JSONObject to parse the JSON. An example:

public class Token {

    public static void main(String[] args) {
        String token = "{\"name\":\"john doe\", \"email\": \"john@mail.com\"}";
        JSONObject jsonObject = new JSONObject(token);
        System.out.println(jsonObject.get("email"));
        System.out.println(jsonObject.get("name"));
    }
}

Output:

john@mail.com
john doe

Full Example:

    String jwtToken = token;
    System.out.println("------------ Decode JWT ------------");
    String[] split_string = jwtToken.split("\\.");
    String base64EncodedHeader = split_string[0];
    String base64EncodedBody = split_string[1];
    String base64EncodedSignature = split_string[2];

    System.out.println("~~~~~~~~~ JWT Header ~~~~~~~");
    Base64 base64Url = new Base64(true);
    String header = new String(base64Url.decode(base64EncodedHeader));
    System.out.println("JWT Header : " + header);

    System.out.println("~~~~~~~~~ JWT Body ~~~~~~~");
    String body = new String(base64Url.decode(base64EncodedBody));
    System.out.println("JWT Body : " + body);

    JSONObject jsonObject = new JSONObject(body);
    System.out.println(jsonObject.get("email"));
    System.out.println(jsonObject.get("name"));
dreamcrash
  • 47,137
  • 25
  • 94
  • 117