spring-projects / spring-projects/spring-security

Consider preventing role names from including the role prefix in `SecurityExpressionRoot`

Open
#17,783 0 comments 0 reactions 1 assignee View on GitHub

@rwinch is already working on this.

Since Aug 21, 2025.

in: core type: breaks-passivity type: enhancement
Dominant language
Java
Stars
9.6k
Forks
6.3k
Avg merge
2d 11h
Merged PRs (30d)
52

Description

As a follow-up to gh-17585, consider preventing role names from including the role prefix in SecurityExpressionRoot for more consistent behavior. This would break passivity in Spring Security 7.

Context:

Currently, SecurityExpressionRoot allows the defaultRolePrefix to be included in a given role name. For example, the SpEL expression in @PreAuthorize("hasRole('ROLE_A')") is allowed, and works the same as @PreAuthorize("hasRole('A')") (assuming the default role prefix is ROLE_).

When switching from standalone logic in SecurityExpressionRoot to using an AuthorizationManager created by AuthorizationManagerFactory, we pick up the behavior of AuthorityAuthorizationManager.hasAnyRole(String rolePrefix, String[] roles) (here), which does not allow a role to start with the given role prefix.

Now, the SpEL expression in @PreAuthorize("hasRole('ROLE_A')") would throw an IllegalArgumentException with the message:

ROLE_A should not start with ROLE_ since ROLE_ is automatically prepended when using hasAnyRole. Consider using hasAnyAuthority instead.

To work around this for passivity, we can strip the role prefix from any role name before passing it to this method. See updates to SecurityExpressionRoot in #17673 for context.

Instead, we can consider preventing role names from including the role prefix, which would be enforced by AuthorityAuthorizationManager in the absence of any workaround.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.