【问题标题】:Grails - Spring Security save UserRoleGrails - Spring Security 保存 UserRole
【发布时间】:2017-08-20 08:36:39
【问题描述】:

我是 Grails 和 Spring Security 的新手。我尝试制作一个简单的注册视图。我使用 spring-security 中的 s2-quickstart 脚本创建了 User、Role 和 UserRole 域类。

我的问题是只有用户会被保存在数据库中。 但我想保存用户和用户角色。

那是我的控制器:

@Secured('permitAll')

类注册控制器 {

def index() {
}

def register() {

    def user = new User(params)
    user.save()

    def role = Role.findByAuthority('ROLE_USER') // ROLE_USER will be created in the bootstrap class
    UserRole.create(user, role)

    redirect view: 'index'
}

}

如果我将相同的代码放在引导类中,UserRole 也会被保存。

希望有人能帮忙解释一下,为什么这不起作用。

【问题讨论】:

    标签: grails spring-security


    【解决方案1】:

    更新您的代码:

    def register() {
    
        def user = new User(params)
        user.save(flush: true, failOnError: true)
    
    new UserRole(user: admin, role: Role.findByAuthority('ROLE_USER')).save(flush: true, failOnError: true)
    
        redirect view: 'index'
    }
    

    如果在保存 UserRole 的过程中出现任何问题,然后 failOnError: true 抛出错误,那么您可以跟踪问题。

    【讨论】:

    • 乐于助人:)
    猜你喜欢
    • 1970-01-01
    • 2015-12-30
    • 2013-09-21
    • 2019-05-09
    • 2012-02-19
    • 2012-09-09
    • 1970-01-01
    • 2014-02-09
    • 2013-01-26
    相关资源
    最近更新 更多