I’m trying to generate a JWT
with base64
encoded json
string.
When the json
string is not encoded as base64
everything works but when it does I get an error about Unexpected JWS content
.
This is the code that creates the JWT
:
<code>public String generateJWT(Object data) throws Exception {
PrivateKey privateKey = getPrivateKey();
String dataJson = objectMapper.writeValueAsString(data);
String dataJson64Base = Base64.getEncoder().encodeToString(dataJson.getBytes(StandardCharsets.UTF_8));
return Jwts.builder()
.content(dataJson64Base)
.signWith(privateKey)
.compact();
}
</code>
<code>public String generateJWT(Object data) throws Exception {
PrivateKey privateKey = getPrivateKey();
String dataJson = objectMapper.writeValueAsString(data);
String dataJson64Base = Base64.getEncoder().encodeToString(dataJson.getBytes(StandardCharsets.UTF_8));
return Jwts.builder()
.content(dataJson64Base)
.signWith(privateKey)
.compact();
}
</code>
public String generateJWT(Object data) throws Exception {
PrivateKey privateKey = getPrivateKey();
String dataJson = objectMapper.writeValueAsString(data);
String dataJson64Base = Base64.getEncoder().encodeToString(dataJson.getBytes(StandardCharsets.UTF_8));
return Jwts.builder()
.content(dataJson64Base)
.signWith(privateKey)
.compact();
}
And this the reading code:
<code>private Claims readClaimsFromPayload(String jwt) throws Exception{
PublicKey publicKey = getPublicKey();
Jws<Claims> claims = Jwts.parser()
.verifyWith(publicKey)
.build()
//.parseEncryptedClaims(jwt);
.parseSignedClaims(jwt);
return claims.getPayload();
}
</code>
<code>private Claims readClaimsFromPayload(String jwt) throws Exception{
PublicKey publicKey = getPublicKey();
Jws<Claims> claims = Jwts.parser()
.verifyWith(publicKey)
.build()
//.parseEncryptedClaims(jwt);
.parseSignedClaims(jwt);
return claims.getPayload();
}
</code>
private Claims readClaimsFromPayload(String jwt) throws Exception{
PublicKey publicKey = getPublicKey();
Jws<Claims> claims = Jwts.parser()
.verifyWith(publicKey)
.build()
//.parseEncryptedClaims(jwt);
.parseSignedClaims(jwt);
return claims.getPayload();
}
I also tried using parseEncryptedClaims
to no avail. The base64
string is a requirement so I have no other option…
I’m new to JWT
🙁