【问题标题】:Grails 2.1.1 and Spring Security Core pluginGrails 2.1.1 和 Spring Security Core 插件
【发布时间】:2012-11-04 14:35:05
【问题描述】:

我注意到我关注的很多教程都使用这个:

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+ 时使用带有大写 S 的 SpringSecurityService SPACE(并且不完成 .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
                }
            }

【问题讨论】:

    标签: grails spring-security


    【解决方案1】:

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

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

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

    【讨论】:

    • 弄明白了。我把代码放在我原来的帖子里。至少我认为我会。 :P 似乎解决了一个问题以找到另一个问题。 :)
    猜你喜欢
    • 2016-03-23
    • 2012-09-29
    • 2015-06-18
    • 1970-01-01
    • 1970-01-01
    • 2014-11-06
    • 1970-01-01
    • 2011-04-22
    • 2016-05-24
    相关资源
    最近更新 更多