我正在使用 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/