grails - 为什么 grails-2.0.1 无法在 BootStrap 类 init 方法中创建 User、Role 和 UserRole 域对象?

标签 grails bootstrapping

嗨,我是 Grails 平台的新手...

我可以使用 Spring Security Core Plugin 在 grails-1.3.7 版本的 BootStrap 类 init 方法中创建 User、Role 和 UserRole 域对象...但不能在 grails-2.0.1 中执行相同操作版本..

以下是我尝试执行的代码...

import com.beshto.Role
import com.beshto.User
import com.beshto.UserRole
import grails.util.Environment

class BootStrap {

  def init = {  servletContext ->
    switch (Environment.current) {
      case Environment.DEVELOPMENT:
        createInitialUsersIfRequired()
        break;
      case Environment.PRODUCTION:
        println "No special config or Bootstrapping required for the Production version..."
        break;
    }
  }

  def destroy = {
  }

  void createInitialUsersIfRequired() {
    println "Creating special config / Bootstrapping for Development version admin/4321 and user/1234..."

    if(Role.list().size() == 0) {
      new Role(authority: 'ROLE_ADMIN').save(flush: true)
      new Role(authority: 'ROLE_USER').save(flush: true)
    }

    if(User.count() == 0){
      def newAdmin = new User(username: 'admin', enabled: true, password: '4321').save(flush: true)
      def newUser = new User(username: 'user', enabled: true, password: '1234').save(flush: true)

      UserRole.create newAdmin, Role.findByAuthority('ROLE_ADMIN'), true
      UserRole.create newUser, Role.findByAuthority('ROLE_USER'), true

      assert User.count() == 2
      assert Role.count() == 2
    }
  }
}

任何帮助将不胜感激!

最佳答案

检查角色创建方法。尝试改变:

新角色(权限:'ROLE_ADMIN').save(flush:true)

对于

新角色(权限:'ROLE_USER').save(failOnError:true)

对于用户来说也是同样的情况。例如:

new User(username: 'admin', enabled: true, password: '4321').save(failOnError: true)

而不是

new User(username: 'admin', enabled: true, password: '4321').save(flush: true)

关于grails - 为什么 grails-2.0.1 无法在 BootStrap 类 init 方法中创建 User、Role 和 UserRole 域对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9501934/

相关文章:

c# - OpenOffice uno.util.Bootstrap.bootstrap() 挂起并且不返回

java - 从另一个 jar 中运行一个 jar

ajax - 文档中未列出Grails 3.0.x AJAX标签

java - Grails 2.4.5 解决获取依赖项的错误

forms - Grails如何为所有者创建所有者的一对多关系创建表单

c# - ASP.net - C# 设置输入类型 : datetime-local to current date

grails - GORM 无法认识到插件中的域类是 GORM 类

grails - 在Grails中使用Jasper插件在新标签页中打开报告

compiler-construction - 编译器自托管是否有实际原因?

angularjs - 模块之间的 Angular 依赖关系