0

我一直注意到我正在关注的很多教程都使用了这个:

def springSecurityService

并且由于我只想通过当前登录的用户获取记录,因此我使用:

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

而且在我的引导程序中,我想创建一个用户名和密码,例如

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

但是我注意到没有创建密码,并且 Spring Source Tools 没有找到方法 .principal.id 或 .encodePassword (它们在 STS 中保持下划线)并且希望在点击 CTL+SPACE 时使用带有大写 S 的 SpringSecurityService (和没有完成 .principal.id 或 .encodePassword)。

所以我有点迷茫,因为教程似乎已经过时了

那么我怎样才能用当前支持的方法来做我所描述的呢?还是我错过了一些非常简单的东西?:)

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
                }
            }
4

1 回答 1

0

使用注入的 SpringSecurityService 实例是正确的方法。

def springSecurityService

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

如果 IDE 无法识别它,则说明您的 IDE 存在问题。

于 2012-11-15T22:22:32.213 回答