Different behaviour of @RolesAllowed
annotation on @EnableMethodSecurity
vs @EnableGlobalMethodSecurity
#11701
Labels
in: core
An issue in spring-security-core
status: waiting-for-triage
An issue we've not yet triaged
type: bug
A general bug
Describe the bug
Consider the following controller:
When legacy
@EnableGlobalMethodSecurity(jsr250Enabled = true)
is used, both endpoints are accessible when request is authenticated withROLE_GUEST
:ROLE_
prefix is added only when not already present in@RolesAllowed
value (code).After switching to the new annotation (
@EnableMethodSecurity(jsr250Enabled = true)
,/rolesAllowed_GUEST
works ok, but/rolesAllowed_ROLE_GUEST
returns 403 instead of 200:ROLE_
prefix is added unconditionally (code),authorities
in AuthorityAuthorizationManager#isAuthorized method isROLE_ROLE_GUEST
in this case (would expectROLE_GUEST
for both endpoints).To Reproduce
@EnableMethodSecurity(jsr250Enabled = true)
.@RolesAllowed("ROLE_GUEST")
.UserDetailsService
:@EnableMethodSecurity
annotation to@EnableGlobalMethodSecurity
and retry the test. Observe the response code - it is 200.Tested on Spring Security 5.7.2, but the code that unconditionally adds the prefix seems to be the same on
main
.See the sample repository provided below for a complete example.
Expected behavior
@EnableMethodSecurity
should behave in the same way as@EnableGlobalMethodSecurity
regarding conditional adding ofROLE_
prefix.Alternatively, the change should be documented in Method Security docs, as it can be breaking for some applications.
Sample
https://github.com/mgr32/spring-security-method-security-issue
Summary including other annotations (assuming HTTP request with proper
Authorization
header):@EnableGlobalMethodSecurity
@RolesAllowed("GUEST")
@EnableMethodSecurity
@RolesAllowed("GUEST")
@EnableGlobalMethodSecurity
@RolesAllowed("ROLE_GUEST")
@EnableMethodSecurity
@RolesAllowed("ROLE_GUEST")
@EnableGlobalMethodSecurity
@Secured("GUEST")
@EnableMethodSecurity
@Secured("GUEST")
@EnableGlobalMethodSecurity
@Secured("ROLE_GUEST")
@EnableMethodSecurity
@Secured("ROLE_GUEST")
@EnableGlobalMethodSecurity
@PreAuthorize("hasRole('GUEST')")
@EnableMethodSecurity
@PreAuthorize("hasRole('GUEST')")
@EnableGlobalMethodSecurity
@PreAuthorize("hasRole('ROLE_GUEST')")
@EnableMethodSecurity
@PreAuthorize("hasRole('ROLE_GUEST')")
The text was updated successfully, but these errors were encountered: