【问题标题】:Grails and spring security core plugin - editing userGrails 和 spring 安全核心插件 - 编辑用户
【发布时间】:2016-04-16 22:44:35
【问题描述】:

我在编辑用户时遇到了验证问题。一切正常,但每次我想编辑选定的用户时,我都会收到第二个密码(密码确认)为空的验证错误。我知道它是空的,因为我没有在我的数据库中存储密码确认。我的应用程序具有与 Grails 文档的 Spring Security Core 插件中几乎相同的 User 类,我没有更改默认 UserController 中的任何内容。

如何关闭该验证消息?

【问题讨论】:

    标签: grails spring-security


    【解决方案1】:

    在您的情况下,它更依赖于您的业务逻辑。如果我假设您在创建页面上需要confirmPassword,但在编辑页面上不需要它,我会说使用CommandObjects

    从您的域中删除 confirmPassword。使用@Validatable annotation 创建一个命令对象。请在那里编写您的自定义验证以确认密码。根据您的需要进行调整。 此外,您可以为一个域拥有多个命令对象,或者我们可以说这些是独立于域的,可用于任何自定义验证或操作。

    建议通过this 了解命令对象及其用法。

    对于您的情况,co(简称)可能如下所示:

    @Validatable
    class UserCO {
        def springSecurityService
    
        String username
        String password
        String passwordConfirmation
        boolean enabled = true
        boolean accountExpired
        boolean accountLocked
        boolean passwordExpired
    
    static constraints = {
            username blank: false, unique: true
            password blank: false, size: 5..60, password: true, display: false, validator: { val, obj ->
                if (!obj.id && !obj.password) {
                    return 'validation.user.password'
                }
            }
            passwordConfirmation bindable: true, blank: false, password: true, display: false, validator: { val, obj ->
                if (obj.id && (!obj.password )) {
                    return true
                }
                if (!(obj.password == obj.passwordConfirmation)) {
                    return 'passwordMismatch'
                }
                return true
            }
        }
    }
    

    希望对你有帮助!

    【讨论】:

    • 我会尽快检查并通知您!
    • 我已经在 src/groovy 中创建了那个类并且遇到了约束问题。有类似的警告:“'username' cannot be applied to ...”其他我不能用@Validatable注释类的东西。也许是因为圣杯 3?
    【解决方案2】:

    您的域类中有 passwordConfirmation 字段并带有约束:

    passwordConfirmation bindable: true, blank: false, password: true, display: false, validator: { val, obj ->
                if (obj.id && (!obj.password )) {
                    return true
                }
                if (!(obj.password == obj.passwordConfirmation)) {
                    return 'passwordMismatch'
                }
                return true
            }
    

    它不能是“空白”,并且必须与“密码”相同。 所以,你有两种方法:

    1. 从域类中删除 passwordConfirmation 并运行迁移脚本(如果使用)。
    2. 将约束更改为passwordConfirmation nullable: true 并手动启动迁移脚本或更新基础(需要删除非空约束)。

    【讨论】:

    • 1.如果我从域类中删除 passwordConfirmation 在将用户添加到数据库时应该去哪里使它工作并检查两个密码是否相同? 2. 我没有在数据库中存储 passwordConfirmation,因此无需将其更改为 nullable: true (我试过了,但没有用)。一旦我点击编辑用户,就会出现confirmPassword不能为空的消息。
    • 因为你有它不应该为空的约束,默认情况下所有字段都不为空。如果您添加 nullable: true 它必须解决您的问题。您可以在操作中比较 UI 中的两个密码的所有验证逻辑
    猜你喜欢
    • 2015-12-09
    • 2018-11-02
    • 2015-04-09
    • 2012-06-01
    • 2014-09-17
    • 2016-06-12
    • 2013-11-24
    • 2011-11-23
    • 2011-12-17
    相关资源
    最近更新 更多