alvarosanchez/grails-spring-security-rest — pull request #131 by aeischeid (2014-10-23 22:16:08+00:00)
Title: example of customized gormTokenStorage with markers.

I took some stab at what I think a solution to #125 might look like in Gorm based apps.

All this essentially does is pass off extra data to the userDetailsService. This requires a custom userDetailsService of course. The details on how to do that are in the Spring Security Core docs, but I will include an example for the sake of discussion:

```groovy
class CustomUserDetailsService implements GrailsUserDetailsService {

/**
* Some Spring Security classes (e.g. RoleHierarchyVoter) expect at least
* one role, so we give a user with no granted roles this one which gets
* past that restriction but doesn't grant anything. */
static final List NO_ROLES = [new GrantedAuthorityImpl(SpringSecurityUtils.NO_ROLE)]

UserDetails loadUserByUsername(String username, boolean loadRoles) throws UsernameNotFoundException {
return loadUserByUsername(username)
}

UserDetails loadUserByUsername(String username, String vendorMarker, String switchMarker) throws UsernameNotFoundException {
AppUser.withTransaction(readOnly:true) { status ->
AppUser user = AppUser.findByUsernameOrEmail(username, username)
if (!user) throw new UsernameNotFoundException( 'User not found', username)
def authorities = user.authorities.collect { new GrantedAuthorityImpl(it.authority) }
return new CustomUserDetails(user.username, user.password, user.enabled,
!user.accountExpired, !user.passwordExpired,
!user.accountLocked, authorities ?: NO_ROLES,
user.id, user.otherCustomProp, vendorMarker, switchMarker)
}
}
}
```

and the CustomUserDetails:

```groovy
class CustomUserDetails extends GrailsUser {

final String otherCustomProp
final String vendorMarker
final String switchMarker

GvlUserDetails(String username, String password, boolean enabled,
boolean accountNonExpired, boolean credentialsNonExpired,
boolean accountNonLocked, Collection authorities,
long id, String otherCustomProp, String vendorMarker, String switchMarker) {
super(username, password, enabled, accountNonExpired, credentialsNonExpired,
accountNonLocked, authorities, id)

this.otherCustomProp = otherCustomProp
this.vendorMarker = vendorMarker
this.switchMarker = switchMarker
}
}