Verify JWT token received from auth0

I want to verify the JWT token returned by auth0 in server side using Java. For that i am using auth0 java-jwt library. This is the example code they are providing.

RSAPublicKey publicKey = null; //Get the key instance
		RSAPrivateKey privateKey = null; //Get the key instance

		try {
			Algorithm algorithm = Algorithm.RSA256(publicKey, privateKey);
			JWTVerifier verifier = JWT.require(algorithm)
					.build(); //Reusable verifier instance
			DecodedJWT jwt = verifier.verify(token);
		} catch (JWTVerificationException exception){
			//Invalid signature/claims

I got the public key from URL,
How do i set this in code? Also from where i can get the private key?

Auth0 does not provide the private key, but you won’t need it for validation. Simply leave the privateKey parameter as null.

Take a look here for an example of creating the PublicKey instance from a file.


Tried it, issue while generating the public key.

This is the exception i am getting IOException: ObjectIdentifier() – data isn’t an object ID (tag = -96)
at com.ceino.supportapp.test.JWTClass.getPublicKey(
at com.ceino.supportapp.test.JWTClass.readPublicKeyFromFile(
at com.ceino.supportapp.test.JWTClass.main(
Caused by: IOException: ObjectIdentifier() – data isn’t an object ID (tag = -96)
… 4 more

1 Like

You could also use jwks-rsa-java library to retrieve public key at run time. Here is a complete sample. You need to get accessToken JWT from the request and add your own tenant url to make it work

    public static void main( String[] args )
    	String token = "YOUR_JWT_TOKEN";
    	JwkProvider provider = new UrlJwkProvider(""); 
    	try {
    		DecodedJWT jwt = JWT.decode(token);
    		// Get the kid from received JWT token
			Jwk jwk = provider.get(jwt.getKeyId());
    	    Algorithm algorithm = Algorithm.RSA256((RSAPublicKey) jwk.getPublicKey(), null);
    	    JWTVerifier verifier = JWT.require(algorithm)
    	    jwt = verifier.verify(token);
    	} catch (JWTVerificationException e){
    	    //Invalid signature/claims
    	} catch (JwkException e) {
			// invalid JWT token

Note: You should also consider caching of public key so you are not calling the JWKS endpoint for every single time you are verifying the JWT.

Here is a working sample if you prefer simply downloading and running it yourself

Make sure though you are also performing further checks such as permissions, standard claims etc. Code above only does JWT format check and signature check. Reference here


This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.