I'm trying to convert a Stripes web app to Grails. The Stripes app uses Spring Security, but I would like the Grails app to use the Spring Security Grails plugin.
The app already has User
and Role
(Java) classes that I need to reuse, i.e. I cannot use the Grails domain classes that the s2-quickstart script generates.
The Spring Security plugin docs describe how to use an existing User
domain class. The steps seem to be:
- define a
UserDetails
implementation that reads from the existing User
domain class
- define a custom
UserDetailsService
implementation that returns instances of (1)
- register an instance of (2) as a Spring bean named
userDetailsService
.
However the docs don't provide any information about how to use an existing Role
class and the class that represents the many-to-many relationship between User
and Role
.
What other steps are necessary to use existing Role
, User
, and UserRole
classes with the Grails Spring Security plugin? Is there any reason for me to run the s2-quickstart script if I don't want to generate any domain classes?
Follow-Up Questions to Burt's Answer
In the end, what you need is a new GrailsUser
Presumably GrailsUser
here refers to the custom UserDetails
implementation? In my case I'll probably just implement the interface directly. Does something like this seem reasonable?
class UserAdapter implements UserDetails {
private String password
private Collection<GrantedAuthority> springRoles
UserAdapter(User user) {
this.password = user.password
Collection<Role> roles = // load legacy Role objects
this.springRoles = roles.collect { new GrantedAuthorityImpl(it.authority) }
}
// If using password hashing, presumably this is the hashed password?
String getPassword() {
password
}
///////// other UserDetails methods omitted
Collection<GrantedAuthority> getAuthorities() {
springRoles
}
}
I'm not storing the whole User
object within UserAdapter
because of your warning about storing a potentially large object in the HTTP session.
what you need is.....and a List of GrantedAuthority instances (and the id if it's a GrailsUser)
If I use my own UserDetails
implementation as above, then presumably I can ignore this comment about providing an id
?
Finally, if I follow the approach outlined above, should I set these properties in Config.groovy
and do I need to run the s2-quickstart
script (or any others)?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…