I must be missing something. I am using the JJWT library to create JWTs. The JWTs are created inconsistently from the library depending on the data set in the claims. My code:
Date now = new Date();
Date expiration = new Date(now.getTime() + TimeUnit.MINUTES.toMillis(30));
Claims claims = Jwts.claims();
JSONObject jsonObject = new JSONObject();
jsonObject.put("serviceName1", "serviceStatus1");
jsonObject.put("serviceName2", "serviceStatus2");
claims.put("services",jsonObject);
claims.setSubject("225544");
claims.setExpiration(expiration);
claims.setId(UUID.randomUUID().toString());
claims.setIssuedAt(now);
return Jwts.builder()
.setClaims(claims)
.signWith(SignatureAlgorithm.HS256, Base64.encodeBase64(secret.getBytes()))
.compact();
This code creates a JWT and signs it correctly. However, when the payload is decoded, the payload value is not always valid JSON. Most often it is missing the closing } causing any parsing of it to fail.
If the Subject is 8 characters, it works fine. If it is 7 or 6 characters long, it is invalid JSON. Or if I add other tags to the claim, sometimes it works and sometimes it doesn't. Am I doing something wrong?
I have also tried using Auth0 java-JWT library and get similar results, Payload not always valid JSON.
claims.put("services", jsonObject.toString ());or use aMap.JSONObject? Probably is a problem encoding the JSON object to string..toString()did not help. I am using net.sf.json-lib as the library. Instead of fighting with it more, I decided it was easier to build the tokens manually, then still validate them through the library. Thanks for helping