Search code examples
grailsspring-securitygrails-plugin

Grails - Spring Security UI Email for Username


I'm using Grails 3.2.4 and am attempting to use the email property of my User class as the username for registration.

So far, I've managed to get Spring Security Core to use the email as the username using the configuration setting below:

grails.plugin.springsecurity.userLookup.usernamePropertyName='email'

However, the registration functionality doesn't seem to take this into account and won't let me register a new user using only an email and password.

I've made a few attempts at overriding the RegisterController but I continue to experience different errors regarding null usernames.

It seems like I must be missing something very simple. Any help / direction is greatly appreciated.


Solution

  • It appears that in version spring-security-ui-3.0.0.M2 the username property might not be override-able.

    String paramNameToPropertyName(String paramName, String controllerName) {
        // Properties in the ACL classes and RegistrationCode aren't currently
        // configurable, and the PersistentLogin class is generated but its
        // properties also aren't configurable. Since this method is only by
        // the controllers to be able to hard-code param names and lookup the
        // actual domain class property name we can short-circuit the logic here.
        // Additionally there's no need to support nested properties (e.g.
        // 'aclClass.className' for AclObjectIdentity search) since those are
        // not used in GSP for classes with configurable properties.
    
        if (!paramName) {
            return paramName
        }
    
        Class<?> clazz = domainClassesByControllerName[controllerName]
        String name
        if (clazz) {
            String key = paramName
            if (paramName.endsWith('.id')) {
                key = paramName[0..-4]
            }
            name = classMappings[clazz][key]
        }
        name ?: paramName
    }
    

    PS I'm getting around this now by doing something like this in my user domain class:

    static transients = ["migrating"]]
    String username = null
    
    public void setEmail(String email) {
        this.email = email
        this.username = email
    }