I am using jose4j to validate and process a JWT. The JWT looks like the following and it passes the validation in the JWT homepage.
However, I can't do the same using jose4j java library. The exception complains about the verification key I set. But there are many types of keys defined in the library and I tried them but no luck. The code is as following:
import java.util.Map;
import org.jose4j.jwt.JwtClaims;
import org.jose4j.jwt.consumer.InvalidJwtException;
import org.jose4j.jwt.consumer.JwtConsumer;
import org.jose4j.jwt.consumer.JwtConsumerBuilder;
import org.jose4j.keys.HmacKey;
public class YGJWT {
public static void main(String args[]) throws InvalidJwtException {
String jwt = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ";
String secret = "secret";
JwtConsumer jwtConsumer = new JwtConsumerBuilder()
.setVerificationKey(new HmacKey(secret.getBytes())) //what kind of key do i need to use it here?
.build();
JwtClaims jwtClaims = jwtConsumer.processToClaims(jwt);
Map<String, Object> claimsMap = jwtClaims.getClaimsMap();
claimsMap.forEach((String key, Object val) -> {
System.out.println(key + ": " + val.toString());
});
}
}
Any help is appreciated.