Fail fast when spring-security-access is missing - #19460
Open
jyx-07 wants to merge 1 commit into
Open
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 file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.