Grails Spring安全UI注册附加字段
我试图在插件提供的用户注册字段中添加其他字段。我相信重写RegisterController是添加更多字段的正确方法(如果我错了,请纠正我)。我已经从原始文件创建了一个新的RegisterController.groovy,其中包含以下内容。在这一点上,我没有添加新的字段属性,只是试图模仿插件的功能Grails Spring安全UI注册附加字段,grails,spring-security,Grails,Spring Security,我试图在插件提供的用户注册字段中添加其他字段。我相信重写RegisterController是添加更多字段的正确方法(如果我错了,请纠正我)。我已经从原始文件创建了一个新的RegisterController.groovy,其中包含以下内容。在这一点上,我没有添加新的字段属性,只是试图模仿插件的功能 package myPackage import grails.plugin.springsecurity.authentication.dao.NullSaltSource import gra
package myPackage
import grails.plugin.springsecurity.authentication.dao.NullSaltSource
import grails.plugin.springsecurity.ui.CommandObject
import grails.plugin.springsecurity.ui.RegistrationCode
import grails.plugin.springsecurity.ui.strategy.MailStrategy
import grails.plugin.springsecurity.ui.strategy.PropertiesStrategy
import grails.plugin.springsecurity.ui.strategy.RegistrationCodeStrategy
class RegisterController extends grails.plugin.springsecurity.ui.RegisterController {
static defaultAction = 'register'
def saltSource
MailStrategy uiMailStrategy
RegistrationCodeStrategy uiRegistrationCodeStrategy
PropertiesStrategy uiPropertiesStrategy
def register(RegisterCommand registerCommand) {
if (!request.post) {
return [registerCommand: new RegisterCommand()]
}
if (registerCommand.hasErrors()) {
return [registerCommand: registerCommand]
}
def user = uiRegistrationCodeStrategy.createUser(registerCommand)
String salt = saltSource instanceof NullSaltSource ? null : registerCommand.username
RegistrationCode registrationCode = uiRegistrationCodeStrategy.register(user, registerCommand.password, salt)
if (registrationCode == null || registrationCode.hasErrors()) {
// null means problem creating the user
flash.error = message(code: 'spring.security.ui.register.miscError')
return [registerCommand: registerCommand]
}
sendVerifyRegistrationMail registrationCode, user, registerCommand.email
[emailSent: true, registerCommand: registerCommand]
}
}
class RegisterCommand implements CommandObject {
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'
}
}
email email: true
password validator: RegisterController.passwordValidator
password2 nullable: true, validator: RegisterController.password2Validator
}
}
任何解决这个问题的想法都将不胜感激 grails.plugin.springsecurity.ui.RegisterController具有以下包私有方法:
void afterPropertiesSet() {
super.afterPropertiesSet()
RegisterCommand.User = User
RegisterCommand.usernamePropertyName = usernamePropertyName
forgotPasswordEmailBody = conf.ui.forgotPassword.emailBody ?: ''
registerEmailBody = conf.ui.register.emailBody ?: ''
registerEmailFrom = conf.ui.register.emailFrom ?: ''
registerEmailSubject = conf.ui.register.emailSubject ?: ''
registerPostRegisterUrl = conf.ui.register.postRegisterUrl ?: ''
registerPostResetUrl = conf.ui.register.postResetUrl ?: ''
successHandlerDefaultTargetUrl = conf.successHandler.defaultTargetUrl ?: '/'
passwordMaxLength = conf.ui.password.maxLength instanceof Number ? conf.ui.password.maxLength : 64
passwordMinLength = conf.ui.password.minLength instanceof Number ? conf.ui.password.minLength : 8
passwordValidationRegex = conf.ui.password.validationRegex ?: '^.*(?=.*\\d)(?=.*[a-zA-Z])(?=.*[!@#$%^&]).*$'
}
其中设置了
注册表命令.User
的值。在新的RegistrationController中也要这样做。是否希望此额外字段在db中、用户域类中保持不变?是的,这就是目标。我还需要弄清楚如何使注册页面遵守用户域类约束。
void afterPropertiesSet() {
super.afterPropertiesSet()
RegisterCommand.User = User
RegisterCommand.usernamePropertyName = usernamePropertyName
forgotPasswordEmailBody = conf.ui.forgotPassword.emailBody ?: ''
registerEmailBody = conf.ui.register.emailBody ?: ''
registerEmailFrom = conf.ui.register.emailFrom ?: ''
registerEmailSubject = conf.ui.register.emailSubject ?: ''
registerPostRegisterUrl = conf.ui.register.postRegisterUrl ?: ''
registerPostResetUrl = conf.ui.register.postResetUrl ?: ''
successHandlerDefaultTargetUrl = conf.successHandler.defaultTargetUrl ?: '/'
passwordMaxLength = conf.ui.password.maxLength instanceof Number ? conf.ui.password.maxLength : 64
passwordMinLength = conf.ui.password.minLength instanceof Number ? conf.ui.password.minLength : 8
passwordValidationRegex = conf.ui.password.validationRegex ?: '^.*(?=.*\\d)(?=.*[a-zA-Z])(?=.*[!@#$%^&]).*$'
}