Skip to content

Reduce usage of "ROLE_" prefix to RoleAuthority which implements GrantedAuthority #4912

@HerrDerb

Description

@HerrDerb

Current state

Currently a role is defined by prefixing it with "ROLE_". Ca. 293 classes within spring-security currently contain a role definition string prefixed with "ROLE_". Not that it's only very hard to re-factor, it's also not type safe, requires string comparing and is ugly and hard to extend.

Suggestion

A class based extendible solution is needed:
Create a new RoleAuthority class which implements GrantedAuthority.
Create a new RoleSecurityConfig class which implements ConfigAttribute.
Create @HasRole annotation which adds ROLE_ to avoid usage of @PreAuthorize with a unsafe EL string.
Create HasRoleMetadataExtractor which returns a Collection of RoleSecurityConfig
Benefits

  1. "ROLE_" is defined once as constant in RoleAuthority -> Huge reduction of coupling.
  2. Type safety: Filter grantedAuthrorities for instance types instead of .startsWith(...) .
  3. Much better readability and type safety with @HasRole.
  4. Increase modularity.

Sample

RoleAuthority roleAuthority = new RoleAuthority("observer");
assertTrue(roleAuthority.getAuthority().equals("ROLE_observer"));
assertTrue(roleAuthority.getRole().equals("observer"));
@HasRole("observer")
public void protectedMethod(){
    ...
}

Next steps

With this base, the spring-security framework will be opened to introduce permission based security more easily.

Metadata

Metadata

Assignees

Labels

status: declinedA suggestion or change that we don't feel we should currently apply

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions