经过大量的试验和错误,我把所学的东西“回到了绘图板上”。我决定最好的结果是简单地通过用我自己的一个包含重复电子邮件检查的“附带”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
}
}
有两点值得评论:
- 不确定在顶部包含许可证文本的协议,但我认为我所做的是正确的;
- 有可能我可以在顶部声明一个静态 String emailPropertyName 而不是使用硬编码的键(并且依赖于其他一些代码来实现正确的值),但现在这似乎是最直接的解决方案。
一个评论可能不起作用 - 这么多年来,这个美妙的 Spring Security UI 已经可用,为什么还没有发现和记录呢?