jwtk / jjwt

Java JWT: JSON Web Token for Java and Android
Apache License 2.0
10.22k stars 1.32k forks source link

UnknownClassException on DefaultJwtParserBuilder when in AWS Lambda #645

Closed ryber closed 3 years ago

ryber commented 3 years ago

Hello, I am using this fine library in a AWS lambda and getting a weird error that is NOT happening in other situations (like a EC2-Spring Boot app). I don't THINK it's this lib's fault in any way but I'm grasping at straws now.

Basically we get this error:

io.jsonwebtoken.lang.UnknownClassException: Unable to load class named [io.jsonwebtoken.impl.DefaultJwtParserBuilder] 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:92)
at io.jsonwebtoken.lang.Classes.newInstance(Classes.java:136)
at io.jsonwebtoken.Jwts.parserBuilder(Jwts.java:130)

The pom for the project has all of the jars:

<dependency>
    <groupId>io.jsonwebtoken</groupId>
    <artifactId>jjwt-api</artifactId>
    <version>0.11.2</version>
</dependency>
<dependency>
    <groupId>io.jsonwebtoken</groupId>
    <artifactId>jjwt-impl</artifactId>
    <version>0.11.2</version>
    <scope>runtime</scope>
</dependency>
<dependency>
    <groupId>io.jsonwebtoken</groupId>
    <artifactId>jjwt-jackson</artifactId> <!-- or jjwt-gson if Gson is preferred -->
    <version>0.11.2</version>
    <scope>runtime</scope>
</dependency>

in fact, if I break open the shaded jar that is being used I can clearly see the /io/jsonwebtoken/impl/DefaultJwtParserBuilder.class file I can also see the copied META-INF/services files. So I'm baffled at why it cannot be found at runtime.

Have you heard of other people getting this error? Or have a tip on what to look at?

amol-can commented 3 years ago

@ryber is this resolved? I'm facing the same issue with the latest version. I downgraded the version to 0.10.8 and it worked (though we need to adjust the parser method)

lhazlewood commented 3 years ago

Do you have a quick/tiny sample app that demonstrates this problem? Your Maven config looks fine.

ryber commented 3 years ago

Sorry, we ended up switching the lambda to an EC2 in Fargate, exact same code, and no error 🤷 I don't have a public example unfortunately.

bdemers commented 3 years ago

@amol-can are you using a shaded jar? If so you probably need to configure the transformer: https://maven.apache.org/plugins/maven-shade-plugin/examples/resource-transformers.html#ServicesResourceTransformer

That said, I'd highly recommend against using the shade plugin, and instead of using something like the maven-spring-boot-plugin. NOTE: This CAN be used for non-spring applications, and doesn't add Spring as a dependency. (it creates a jar of jars and adds a special class loader to deal with them, the result is similar to a war file). YMMV ;)

https://docs.spring.io/spring-boot/docs/current/maven-plugin/reference/htmlsingle/#goals-repackage

stale[bot] commented 3 years ago

This issue has been automatically marked as stale due to inactivity for 60 or more days. It will be closed in 7 days if no further activity occurs.

stale[bot] commented 3 years ago

Closed due to inactivity.