create users with spring security plug-in for Grails
I'm using the spring security core plug-in in a Grails app. I managed to install the plug-in and provide a default user in the bootstrap which is working fine. However I want to provide the option for cre开发者_Python百科ate a new user (signup) in the application. What is the best way to accomplish this? I tried the following code in the UserController:
def save = {
def userRole = SecRole.findByAuthority("ROLE_USER")?: new SecRole(authority:"ROLE_USER").save(failOnError:true)
def adminRole = SecRole.findByAuthority("ROLE_ADMIN") ?: new SecRole(authority:"ROLE_ADMIN").save(failOnError:true)
def newUser = new User(
username: params.username,
password: springSecurityService.encodePassword(params.password),
enabled: true)
SecUserSecRole.create newUser, userRole
}
but it doesn't work and throws the java.lang.NullPointerException. Any ideas? Thanks in advance.
You need to call save()
on the User.
I tried to load not existing Roles. I tested it with the above comment (save the user and not so save after fixing my bug and both versions worked). So SecUserSecRole.create were sufficient for me.
精彩评论