Grails and Spring Security: How catch user into controller?

i read this, this and this but not resolve my problem.

This is my error: Cannot invoke method getCurrentUser() on null object. Stacktrace follows: Message: Cannot invoke method getCurrentUser() on null object

My code:

import org.springframework.dao.DataIntegrityViolationException
import grails.plugin.springsecurity.annotation.Secured;

@Secured('ROLE_SYSTEMADMIN')
class InstanceController {

...

def save() {
    def instanceInstance = new Instance(params)
def springSecurityService

println("user is"+springSecurityService.getCurrentUser());
...

I use:

  • GGTS 3.4.0.
  • compile ':spring-security-core:2.0-RC2'

I'm logged. I'm sure. Thanks for reply.

Raziel

Answers


you defined springSecurityService as method variable. You should define it as instance variable:

class InstanceController {
  def springSecurityService
  def save() {
    def instanceInstance = new Instance(params)
    println("user is"+springSecurityService.getCurrentUser());
    ..
  }
}

Now spring can inject the springSecurityService once the controller is created.


The service is not being Injected because you are not stating as a global property of the controller. Declare it out of your save method and then call currentuser property, That should work.


Need Your Help

NoSuchMethodError Bundle.getString()

android

I get some wierd logs, which obviously only occurs on Android-Devices with Versions below 3(checked with emulator).

How to import a csv file into MySQL server?

mysql import export

I am very new to MySQL. I have started learning MySQL today only.