grails - BootStrap.groovy 中的语法错误

标签 grails groovy spring-security

我正在使用 Grails 和 SpringSecurity 插件。我运行了 s2-quickstart 所以用户域类 被命名为“User.groovy”,角色域类被命名为“Role.groovy”。因此,映射类被命名为“UserRole.groovy”。 然后我修改了 BootStrap.groovy 以创建一个示例用户,这导致了一个令人讨厌的语法错误“Groovy:意外的 token :UserRole @ 第 19 行,第 2 列。”当调用“UserRole.create”时。

这是我的 BootStrap.groovy 文件:

import com.foo.Role
import com.foo.User
import com.foo.UserRole


class BootStrap {

    def springSecurityService

    def userSecRole = Role.findByAuthority("ROLE_USER") ?: new Role()(authority: "ROLE_USER").save()

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


    UserRole.create user, userSecRole     // <--- This is where the error happens


    def init = { servletContext ->
    }
    def destroy = {
    }
}

最佳答案

您已将代码放入主类定义中。

该代码应该位于 init 闭包内,即:

import com.foo.Role
import com.foo.User
import com.foo.UserRole

class BootStrap {

    def springSecurityService

    def init = { servletContext ->
      def userSecRole = Role.findByAuthority("ROLE_USER") ?: new Role()(authority: "ROLE_USER").save()

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

      UserRole.create user, userSecRole     // <--- This is where the error happens
    }
    def destroy = {
    }
}

关于grails - BootStrap.groovy 中的语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11709863/

相关文章:

grails - Grails-带有pdf附件的RESTful Web服务?

grails - 使用对象列表进行Grails自定义验证

date - Groovy - 将时间戳字符串转换为以毫秒为单位的纪元时间

grails - 使用findAllBy时返回唯一结果

grails - Grails发布关联列表中的重复姓氏

validation - Grails命令对象未验证

spring - TAM Webseal + spring 预认证

java - Spring 安全 3.x : Creating a custom authentication provider without a UserDetailsService

javascript - Spring 安全: dot in username on jsp page

unit-testing - 如何在 Grails 中测试 spring bean?