gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 14:04:07 25 4
gpt4 key购买 nike

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

我可以使用Spring Security Core插件在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
}
}
}

任何帮助将不胜感激!

最佳答案

检查角色创建方法。尝试更改:
new Role(authority: 'ROLE_ADMIN').save(flush: true)
对于
new Role(authority: '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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com