Add association to SpringSecurity User class
-
13-07-2021 - |
Domanda
I'm trying to add a reference from the default Spring Security user domain class to another class which should hold additional user info.
class User {
transient springSecurityService
String username
String password
boolean enabled
boolean accountExpired
boolean accountLocked
boolean passwordExpired
Profile profile
...
...and...
class Profile {
static constraints = {
}
static belongsTo = [user : User]
String firstName
String lastName
}
From what I read this should be the way it works, but I get the following error :
ERROR context.GrailsContextLoader - Error executing bootstraps: null
Message: null
Line | Method
->> 32 | create in com.app.UserRole
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 15 | doCall in BootStrap$_closure1
| 301 | evaluateEnvironmentSpecificBlock in grails.util.Environment
| 294 | executeForEnvironment in ''
| 270 | executeForCurrentEnvironment . . in ''
| 303 | innerRun in java.util.concurrent.FutureTask$Sync
| 138 | run . . . . . . . . . . . . . . in java.util.concurrent.FutureTask
| 886 | runTask in java.util.concurrent.ThreadPoolExecutor$Worker
| 908 | run . . . . . . . . . . . . . . in ''
^ 662 | run in java.lang.Thread
Any idea?
Soluzione
We need to see your BootStrap
to be sure, but I suspect it's because you're doing something like
def user = new User(....).save()
def role = new Role(....).save()
UserRole.create(user, role)
and your new User
is failing validation (which causes save
to return null
).
If you use save(failOnError:true)
you should get a different exception with a better indication of what the real problem is. Check that you have the right constraints
in your User
class, in particular note that GORM properties are by default non-nullable so if you want to be able to save a User
that doesn't have a Profile
you will need to add a constraint of profile(nullable:true)
.