简体   繁体   中英

Grails 2.1.1 and Spring Security Core plugin

I've been noticing that a lot of the tutorials I'm following use this:

def springSecurityService

and since I want to get records only by current logged in user I use:

def user = params.id ? User.findByUserId(params.id) : User.get(springSecurityService.principal.id)

and also in my Bootstrap I want to create a username and password, so for instance

def user = new User( username: username, password: springSecurityService.encodePassword("tops3kr17"), enabled: true)

However I noticed that the password is not being created, and Spring Source Tools does not find the method .principal.id or .encodePassword (they stay underlined in STS) and wants to use SpringSecurityService with a capital S when hitting CTL+SPACE (and doesn't complete .principal.id or .encodePassword).

So i'm a little lost because it seems that the tutorials are out of date

So how can I do what I described with what the current supported methods are? Or am I missing something really simple? : )

class BootStrap {
def springSecurityService

def init = { servletContext ->

    def demo = [
        'jack' : [ fullName: 'Jack Demo Salesman'],
        'jill' : [ fullName: 'Jill Demo Saleswoman']]

    def now = new Date()
    def random = new Random()

    def userRole = SecRole.findByAuthority("ROLE_SALES") ?: new SecRole(authority: "ROLE_SALES").save()
    def adminRole = SecRole.findByAuthority("ROLE_ADMIN") ?: new SecRole(authority: "ROLE_ADMIN").save()

    def users = User.list() ?: []
    if (!users) {
        demo.each { username, password, userAttrs ->
            def user = new User(
                username: username,
                password: springSecurityService.encodePassword('secret'),
                enabled: true)
            if (user.validate()) {
                println "DEBUG: Creating user ${username}..."
                println "DEBUG: and their password is ${password}"
                user.save(flush:true)

                SecUserSecRole.create user, userRole
                users << user
            }
            else {
                println("\n\n\nError in account bootstrap for ${username}!\n\n\n")
                user.errors.each {err -> 
                    println err
                }
            }

Using the injected instance of SpringSecurityService is the right approach.

def springSecurityService

def foo() {
   springSecurityService.principal
   springSecurityService.encodePassword('fdsfads')
   ....
}

If the IDE isn't recognizing it, there is an issue with your IDE.

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