spring-attic / spring-native

Spring Native is now superseded by Spring Boot 3 official native support
https://docs.spring.io/spring-boot/docs/current/reference/html/native-image.html
Apache License 2.0
2.74k stars 356 forks source link

`java.lang.ClassNotFoundException: javax.servlet.Filter` when using Spring Security #709

Closed UkonnRa closed 3 years ago

UkonnRa commented 3 years ago

In this is the minimal demo: https://github.com/UkonnRa/native-test

In my case, the reason is that there are something wrong with Spring Security Starter. And the error is: Caused by: java.lang.ClassNotFoundException: javax.servlet.Filter.

If removing Security related dependency, everything is working fine

UkonnRa commented 3 years ago

To make Spring Security work, I have to add javax.servlet:javax.servlet-api:4.0.1. But Why?

aclement commented 3 years ago

if I build that sample project, as it is... it works for me:

mvn spring-boot:build-image
...
docker run -p 8080:8080 native-test:0.0.1-SNAPSHOT
...
c.u.nativetest.NativeTestApplication     : Started NativeTestApplication in 0.049
...
curl -i --user user:668509da-4af5-482b-accd-91a77147f028 localhost:8080
Hello World

The exception included in the README:

[INFO]     [creator]     Fatal error:java.lang.TypeNotPresentException: Type javax.servlet.Filter not present
[INFO]     [creator]            at java.base/sun.reflect.generics.factory.CoreReflectionFactory.makeNamedType(CoreReflectionFactory.java:117)
[INFO]     [creator]            at java.base/sun.reflect.generics.visitor.Reifier.visitClassTypeSignature(Reifier.java:125)
[INFO]     [creator]            at java.base/sun.reflect.generics.tree.ClassTypeSignature.accept(ClassTypeSignature.java:49)
[INFO]     [creator]            at java.base/sun.reflect.generics.visitor.Reifier.reifyTypeArguments(Reifier.java:68)
[INFO]     [creator]            at java.base/sun.reflect.generics.visitor.Reifier.visitClassTypeSignature(Reifier.java:138)
[INFO]     [creator]            at java.base/sun.reflect.generics.tree.ClassTypeSignature.accept(ClassTypeSignature.java:49)
[INFO]     [creator]            at java.base/sun.reflect.generics.visitor.Reifier.reifyTypeArguments(Reifier.java:68)
[INFO]     [creator]            at java.base/sun.reflect.generics.visitor.Reifier.visitClassTypeSignature(Reifier.java:138)
[INFO]     [creator]            at java.base/sun.reflect.generics.tree.ClassTypeSignature.accept(ClassTypeSignature.java:49)
[INFO]     [creator]            at java.base/sun.reflect.generics.repository.MethodRepository.computeReturnType(MethodRepository.java:75)

is like an error we saw months and months ago. It would happen when something in the GraalVM compiler is not properly coping with a type being missing whilst the option --allow-incomplete-classpath is supplied. My first reaction is whether it is using the right level of GraalVM but I guess that is fixed by the buildpack. I'm thinking it might be related to everything being snapshots in there (boot snapshots and spring native snapshots). If @eleftherias tries it, I'd be interested to hear if it runs. I have no reference to javax.servlet:servlet-api in there.

aclement commented 3 years ago

If it was recreatable we can breakpoint in the GraalVM compiler and discover what it is having trouble unwrapping when it hits this and why a surrounding try catch isn't handling it.

UkonnRa commented 3 years ago

Weird, I tried today and it's working fine too... I will close this issue and dive deeper into it to see what the problem is