RoleHierarchy not automatically inject in overwritten MethodSecurityExpressionHandler bean #16307
Closed as not planned
Closed as not planned
Description
If you manually define both a RoleHierarchy
bean and a MethodSecurityExpressionHandler
, the RoleHierarchy
isn't injected in the MethodSecurityExpressionHandler
bean. As a result, the role hierarchy isn't applied when calling hasRole()
when securing a method with @PreAuthorize
.
To Reproduce
- Define a
RoleHierarchy
bean - Manually define the bean for the
MethodSecurityExpressionHandler
. For example, by inheritingDefaultMethodSecurityExpressionHandler
.
Expected behavior
The RoleHierarchy
bean is injected in the manually defined MethodSecurityExpressionHandler
and can be used with @PreAuthorize
and hasRole()
Current behavior
When you launch the application the following happens in order:
PrePostMethodSecurityConfiguration
initializeexpressionHandler
with a manually createdDefaultMethodSecurityExpressionHandler
- The
RoleHierarchy
bean is injected inPrePostMethodSecurityConfiguration
andexpressionHandler.setRoleHierarchy
is called - The manually defined
MethodSecurityExpressionHandler
. bean is injected inPrePostMethodSecurityConfiguration
and the configuration is adapted, but the methodsetRoleHierarchy
isn't called on the new bean.