I am trying to generate a JWK from jjwt-api with the below code. It is just a simple snippet that is trying to generate a JWK with RSA 256 (2048 bits)
import com.google.gson.Gson;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.security.Jwks;
import io.jsonwebtoken.security.RsaPrivateJwk;
import java.nio.charset.StandardCharsets;
import java.security.interfaces.RSAPrivateKey;
public class Test {
public static void main(String[] args) {
RsaPrivateJwk privJwk = Jwks.builder()
.id("test-key-id")
.key((RSAPrivateKey) Jwts.SIG.RS256.keyPair().build().getPrivate()) //2048 bits
.build();
String jwkJson = new String(new Gson().toJson(privJwk).getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8);
System.out.println(jwkJson);
}
}
This is my pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>artifact</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-api</artifactId>
<version>0.12.6</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-impl</artifactId>
<version>0.12.6</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-gson</artifactId>
<version>0.12.6</version>
</dependency>
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.9.0</version>
</dependency>
</dependencies>
</project>
I am getting the below exception in spite of having jjwt-impl as a runtime dependency. I did try several things such as cleaning and reimporting the project but that doesn’t to work.
/usr/lib/jvm/jdk-17.0.12-oracle-x64/bin/java -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:57679,suspend=y,server=n -javaagent:/home/abhi/Downloads/idea-IC-242.20224.387/plugins/java/lib/rt/debugger-agent.jar -Dkotlinx.coroutines.debug.enable.creation.stack.trace=false -Ddebugger.agent.enable.coroutines=true -Dfile.encoding=UTF-8 -classpath /home/abhi/Projects/lib-service-java/target/classes:/home/abhi/.m2/repository/io/jsonwebtoken/jjwt-api/0.12.6/jjwt-api-0.12.6.jar:/home/abhi/.m2/repository/com/google/code/gson/gson/2.9.0/gson-2.9.0.jar:/home/abhi/.m2/repository/commons-cli/commons-cli/1.9.0/commons-cli-1.9.0.jar:/home/abhi/Downloads/idea-IC-242.20224.387/lib/idea_rt.jar Test
Connected to the target VM, address: '127.0.0.1:57679', transport: 'socket'
Exception in thread "main" io.jsonwebtoken.lang.UnknownClassException: Unable to load class named [io.jsonwebtoken.impl.security.DefaultDynamicJwkBuilder] from the thread context, current, or system/application ClassLoaders. All heuristics have been exhausted. Class could not be found. Have you remembered to include the jjwt-impl.jar in your runtime classpath?
at io.jsonwebtoken.lang.Classes.forName(Classes.java:90)
at io.jsonwebtoken.lang.Classes.newInstance(Classes.java:173)
at io.jsonwebtoken.security.Jwks.builder(Jwks.java:56)
at Test.main(Test.java:12)
Disconnected from the target VM, address: '127.0.0.1:57679', transport: 'socket'
Process finished with exit code 1