-
Notifications
You must be signed in to change notification settings - Fork 38.9k
Closed as not planned
Closed as not planned
Copy link
Labels
for: stackoverflowA question that's better suited to stackoverflow.comA question that's better suited to stackoverflow.com
Description
Summary
When a filter extends GenericFilterBean or OncePerRequestFilter and is annotated with @Component (or created as a @Bean with constructor injection), the init() method throws a NullPointerException because this.logger is null.
Expected Behavior
The filter should initialize correctly when used as a Spring bean with dependency injection. The logger should be properly initialized before any method tries to use it.
Actual Behavior
java.lang.NullPointerException: Cannot invoke "org.apache.commons.logging.Log.isDebugEnabled()" because "this.logger" is null
at org.springframework.web.filter.GenericFilterBean.init(GenericFilterBean.java:235) ~[spring-web-7.0.1.jar:7.0.1]
at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:236) ~[tomcat-embed-core-11.0.14.jar:11.0.14]
// ... full stack traceSteps to Reproduce
- Create a filter extending
OncePerRequestFilterorGenericFilterBeanwith constructor injection - Annotate it with
@Componentand use@RequiredArgsConstructoror@Autowiredconstructor - Inject the filter into Spring Security configuration via
addFilterBefore() - Start the Spring Boot application
Root Cause Analysis
The issue occurs because:
- When
@Componentis used with constructor injection, Spring creates a proxy - The
GenericFilterBean.init()method is marked asfinal - When the proxy tries to call
super.init(), the logger field hasn't been initialized yet - The
loggerfield inGenericFilterBeanistransientand may not be properly initialized in proxy scenarios
Environment
- Spring Boot Version: 4.0.0
- Spring Framework Version: 7.0.1
- Java Version: 21.0.9
- Servlet API: Jakarta Servlet 6.0
- Tomcat Version: 11.0.14
- Build Tool: Maven 3.9+
- Operating System: Linux/Windows/macOS (all affected)
Additional Context
This bug particularly affects:
- JWT authentication filters (very common use case)
- Logging filters
- Security filters integrated with Spring Security
- Any filter using modern Spring practices (constructor injection,
@Component)
The error message in logs also shows a warning about CGLIB proxies:
Unable to proxy interface-implementing method [public final void org.springframework.web.filter.GenericFilterBean.init(jakarta.servlet.FilterConfig) throws jakarta.servlet.ServletException] because it is marked as final, consider using interface-based JDK proxies instead.
This suggests the framework is aware of the proxying issue but the NPE still occurs.
Metadata
Metadata
Assignees
Labels
for: stackoverflowA question that's better suited to stackoverflow.comA question that's better suited to stackoverflow.com