【问题标题】:Grails 3.3.9 Spring Security UI 3.1.2 generate error for duplicate emailGrails 3.3.9 Spring Security UI 3.1.2 为重复电子邮件生成错误
【发布时间】:2021-01-19 07:37:26
【问题描述】:

在我的 User.groovy 域类中,我添加了我想在注册时捕获的字段字符串电子邮件。我定义了这些约束:

email email:true, blank: false, maxsize: 255, unique: true

当我注册时,如果我尝试重复使用现有的用户名,我会在用户名字段旁边收到一个很好的错误,指出用户名已被使用。我在生成的 HTML 中看到它来自

<span class='s2ui_error'>The username is taken</span>

但是,当我尝试重复使用现有电子邮件地址时,我会在屏幕右上角看到一个带有黄色文本的黑色窗格,上面写着:

“抱歉,处理您的注册时出现问题”。

只有在没有其他错误的情况下才会出现此消息。

我想导致重复电子邮件错误生成类似于重复用户名错误的字段错误。我似乎无法找到这些附加标签的生成位置。

提前感谢您的任何建议。

【问题讨论】:

    标签: grails-spring-security grails-3.3.x


    【解决方案1】:

    经过大量的尝试和错误,我接受了所学并“回到绘图板上”。我决定最好的结果是简单地通过用我自己的一个包含重复电子邮件检查的“随附”Spring Security UI 替换默认的 RegisterCommand.groovy 来完成这项工作。

    我还决定去掉 s2ui-override 进程提供的存根 RegisterController.groovy,以防万一受到干扰。

    这种方法有效。新的RegisterCommand.groovy 进入

    src/main/groovy/grails/plugin/springsecurity/ui
    

    在项目目录中,它看起来像这样:

    /* Copyright 2015-2016 the original author or authors.
     *
     * Licensed under the Apache License, Version 2.0 (the "License");
     * you may not use this file except in compliance with the License.
     * You may obtain a copy of the License at
     *
     *      http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     */
    
    package grails.plugin.springsecurity.ui
    
    
    class RegisterCommand implements CommandObject, grails.validation.Validateable {
    
        protected static Class<?> User
        protected static String usernamePropertyName
    
        String username
        String email
        String password
        String password2
    
        static constraints = {
            username validator: { value, command ->
                if (!value) {
                    return
                }
    
                if (User.findWhere((usernamePropertyName): value)) {
                    return 'registerCommand.username.unique'
                }
            }
            // Original begins
            // email email: true
            // Original ends
            // Modifications begin
            email email: true, validator: { value, command ->
                if (!value) {
                    return
                }
    
                if (User.findWhere('email': value)) {
                    return 'registerCommand.email.unique'
                }
            }
            // Modifications end
            password validator: RegisterController.passwordValidator
            password2 nullable: true, validator: RegisterController.password2Validator
        }
    }
    

    值得制作的两个cmets:

    1. 不确定在顶部包含许可证文本的协议,但我认为我所做的是正确的;
    2. 有可能我可以在顶部声明一个静态字符串 emailPropertyName 而不是使用硬编码的键(并依赖其他一些代码来实现正确的值),但目前这似乎是最直接的解决方案。

    一个评论可能不起作用 - 这么多年来,这个美妙的 Spring Security UI 已经可用,为什么还没有发现和记录呢?

    【讨论】:

      猜你喜欢
      • 2020-04-06
      • 2017-05-20
      • 2012-08-19
      • 2017-11-09
      • 2014-11-20
      • 1970-01-01
      • 2012-12-12
      • 2013-02-13
      • 1970-01-01
      相关资源
      最近更新 更多