Fail fast when spring-security-access is missing - #19460
Conversation
The "Move Core Access API" refactoring (spring-projectsgh-17847) relocated MethodSecurityMetadataSourceAdvisor and MethodSecurityInterceptor from spring-security-core, a mandatory dependency of spring-security-config, into the new spring-security-access module, which spring-security-config only depends on optionally. GlobalMethodSecuritySelector (backing the deprecated @EnableGlobalMethodSecurity) and ReactiveMethodSecuritySelector (backing @EnableReactiveMethodSecurity(useAuthorizationManager = false)) still unconditionally import configuration that constructs those classes: MethodSecurityMetadataSourceAdvisorRegistrar in proxy mode, GlobalMethodSecurityConfiguration in both proxy and aspectj mode, and ReactiveMethodSecurityConfiguration for the legacy reactive path. Applications that use any of these deprecated configuration paths without explicitly adding spring-security-access now fail at startup with a confusing NoClassDefFoundError deep inside Spring's configuration-processing machinery, instead of an actionable message. @EnableMethodSecurity and @EnableReactiveMethodSecurity's default (AuthorizationManager-based) mode, the non-deprecated replacements, never reference these classes and are unaffected either way. Add a ClassUtils.isPresent check to both selectors so that, whenever a legacy configuration path that needs it is chosen (proxy mode, aspectj mode, or the legacy reactive interceptor), a missing spring-security-access dependency now fails fast with a clear IllegalStateException that names the missing dependency and points to the supported alternative, rather than a NoClassDefFoundError. This preserves spring-projectsgh-17847's footprint-reduction intent: the check only runs for the deprecated legacy annotations, so the majority of applications using @EnableMethodSecurity see no change in behavior or dependencies. @EnableGlobalMethodSecurity remains deprecated; this change adds no new investment in it beyond giving existing users of it a clear diagnostic instead of a confusing crash. Closes spring-projectsgh-19441 Signed-off-by: jyx-07 <s25069@gsm.hs.kr>
This commit adds an IllegalStateException guard when spring-security-access is missing and the application is using @EnableGlobalMethodSecurity with jsr250Enabled. Issue spring-projectsgh-19441 Signed-off-by: Josh Cummings <3627351+jzheaux@users.noreply.github.com>
|
Thanks for the contribution, @jyx-07! Note I've rebased this onto 7.0.x since I've confirmed the bug there as well. Regarding the solution, the |
|
Thanks, @jyx-07, for the PR! This is now merged into |
Summary
Fixes #19441.
spring-security-config's deprecated legacy method-security support throws a rawNoClassDefFoundErrorat startup instead of a clear error whenspring-security-accessisn't on the classpath.Root cause
#17847 moved
MethodSecurityMetadataSourceAdvisorandMethodSecurityInterceptorfromspring-security-core(a mandatory dependency ofspring-security-config) into the newspring-security-accessmodule, whichspring-security-configonly depends on optionally. Several deprecated method-security paths still construct these types unconditionally:GlobalMethodSecuritySelector→MethodSecurityMetadataSourceAdvisorRegistrar(proxy mode) andGlobalMethodSecurityConfiguration(imported for both proxy and aspectj mode)ReactiveMethodSecuritySelector→ReactiveMethodSecurityConfiguration(@EnableReactiveMethodSecurity(useAuthorizationManager = false))@EnableMethodSecurityand@EnableReactiveMethodSecurity's default mode — the non-deprecated replacements — never reference these classes and are unaffected.Fix
Added a
ClassUtils.isPresent(...)guard (the same pattern already used for other optional dependencies in these selectors) so the legacy paths fail fast with a clearIllegalStateExceptionnaming the missing dependency, instead of aNoClassDefFoundErrordeep in Spring's configuration-processing machinery.Chose this over making
spring-security-accessmandatory again, since that would silently reintroduce the footprint #17847 removed for the majority of apps using@EnableMethodSecurity.@EnableGlobalMethodSecuritystays deprecated; this adds no new investment in it beyond a clear diagnostic.Testing
Added
Gh19441Tests, using the existing@ClassPathExclusionsharness to simulatespring-security-accessbeing absent:@EnableMethodSecurityand@EnableReactiveMethodSecuritydefault mode start cleanly (locks in that the modern paths are unaffected).@EnableGlobalMethodSecurityin proxy mode, aspectj mode, and@EnableReactiveMethodSecurity(useAuthorizationManager = false)all now fail with the clear message instead ofNoClassDefFoundError/ClassNotFoundException.Full
spring-security-configsuite passes (3640 tests; the only 3 failures are pre-existing, locale-related, and unrelated to this change)../gradlew :spring-security-config:checkpasses.