-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Description
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
- "ROLE_" is defined once as constant in
RoleAuthority
-> Huge reduction of coupling. - Type safety: Filter
grantedAuthrorities
for instance types instead of.startsWith(...)
. - Much better readability and type safety with
@HasRole
. - 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.