简体   繁体   中英

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 create 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM