繁体   English   中英

Grails 2.1.1和Spring Security Core插件

[英]Grails 2.1.1 and Spring Security Core plugin

我一直注意到,我正在关注的许多教程都使用此方法:

def springSecurityService

由于我只想由当前登录用户获取记录,因此我使用:

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

而且我想在Bootstrap中创建用户名和密码,例如

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

但是我注意到没有创建密码,并且Spring Source Tools找不到方法.principal.id或.encodePassword(它们在STS中始终带有下划线),并且想在击中CTL + SPACE时将SpringSecurityService与大写S一起使用(和未完成.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
                }
            }

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

def springSecurityService

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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