I am the author of this article.
Sure, there are several ways to do this, but as I usually do, it is to implement a custom UserDetails that knows about roles and permissions. Role and Permission are just the user classes you write. (Nothing unusual - Role has a name and a set of Permission instances, and Permission has a name.) Then getAuthorities() returns GrantedAuthority objects that look like this:
PERM_CREATE_POST , PERM_UPDATE_POST , PERM_READ_POST
instead of returning things like
ROLE_USER , ROLE_MODERATOR
Roles are still available if your UserDetails implementation has a getRoles() method. (I recommend having it.)
Ideally, you assign roles to the user, and the corresponding permissions are automatically populated. This is due to the presence of a custom UserDetailsService that knows how to perform this mapping, and all it needs to do is initial map from the database. (See Article for scheme.)
You can then define your authorization rules in terms of permissions instead of roles.
Hope this helps.
Willie Wheeler Jan 27 '12 at 6:44 2012-01-27 06:44
source share