Grails 3.2.2 spring-security-ui override RegisterController not working

596 views Asked by At

I am working on a Grails 3.2.2 Plugin in which I am trying to override the RegisterController from spring-security-ui as well as the register.gsp. I have used the s2ui-override command which has created me the required controller and at the moment I only added a println to see it working, but it seems to be still going to the original one. So far I have:

RegisterController

import org.springframework.security.core.userdetails.UserDetailsService
import org.springframework.security.core.userdetails.UserDetails
import grails.plugin.springsecurity.ui.RegisterCommand

class RegisterController extends grails.plugin.springsecurity.ui.RegisterController {

static defaultAction = 'register'

UserDetailsService userDetailsService

@Override
def register(RegisterCommand registerCommand) {
    println "IN MY CONTROLLER!!"
    def registerReturn = super.register(registerCommand)
    if (registerReturn?.emailSent) {
        UserDetails createdUser = userDetailsService.loadUserByUsername(registerCommand.username)

        println "New user ${createdUser}"
    }
    registerReturn
}

def otherAction() {
    println "IN MY CONTROLLER!!"
    println "userDetailsService = ${userDetailsService}"
    "Hellloooo"
}

}

register.gsp same as the original, only added

<p style="color:red;"> HELLLOOOOOOO </p>

immediately after the body tag

UrlMappings

class UrlMappings {

static mappings = {
    "/$controller/$action?/$id?(.$format)?"{
        constraints {
            // apply constraints here
        }
    }

    "/"(view:"/index")
    "500"(view:'/error')
    "404"(view:'/notFound')
 }
}

Result when running the app: I can see my sample red text at the top, but cannot see the output in the console, although I am getting a warning when running the app. Console output when going to the "register" URL:

 Running application...
 WARNING: The [register] action accepts a parameter of type [grails.plugin.springsecurity.ui.RegisterCommand] which does not implement grails.validation.Validateable.  Data binding will still be applied to this command object but the instance will not be validateable.

   @Override
   ^

 Configuring Spring Security Core ...
 ... finished configuring Spring Security Core


 Configuring Spring Security UI ...
 ... finished configuring Spring Security UI


Configuring Spring Security REST 2.0.0.M2...
 ... finished configuring Spring Security REST

Grails application running at http://localhost:8060 in environment: development

Though when going to "register/otherAction" I am getting:

IN MY CONTROLLER
userDetailsService = grails.plugin.springsecurity.userdetails.GormUserDetailsService@10ed50b0

Interestingly, if I publish the plugin locally (my .m2 dir) and import it into an app, I am getting my printlns in the console, BUT my gsp is now not being used (I cannot see my red Hello text). Console output:

IN MY CONTROLLER!!
// unimportant mailing error
New user grails.plugin.springsecurity.userdetails.GrailsUser@364492: Username: test; Password: [PROTECTED]; Enabled: true; AccountNonExpired: true; credentialsNonExpired: true; AccountNonLocked: false; Granted Authorities: ROLE_NO_ROLES

Anyone can help on where am I going wrong? I am happy to provide any other resources which may help.

1

There are 1 answers

0
AVS On

So I have a fix (maybe a workaround):

RegisterController

class RegisterController extends grails.plugin.springsecurity.ui.RegisterController {

static namespace = 'my-great-plugin'

UserDetailsService userDetailsService

@Override
def register(RegisterCommand registerCommand) {
    println "IN MY CONTROLLER!!"
    def registerReturn = super.register(registerCommand)
    println "Got return from super call ${registerReturn}"
    if (registerReturn?.emailSent) {
        UserDetails createdUser = userDetailsService.loadUserByUsername(registerCommand.username)
        println "New user ${createdUser}"
    }
    registerReturn
}

def otherAction() {
    println "IN MY CONTROLLER"
    println "userDetailsService = ${userDetailsService}"
    "Hellloooo"
}

MyPluginUrlMappings.groovy

static mappings = {
    "/register/$action?/$id?(.$format)?" { 
        controller = 'register' 
        namespace = 'my-great-plugin' 
    }
}

MyPluginGrailsPlugn.groovy

 def loadAfter = ['springSecurityCore', 'springSecurityUi', 'springSecurityRest']

And it works now!

I am still open to suggestions on how to handle this better