0

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

Community
  • 1
  • 1
Raziel
  • 53
  • 1
  • 8

2 Answers2

4

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.

micha
  • 47,774
  • 16
  • 73
  • 80
  • 2
    It's worth noting that there is a method injected in every controller: getAuthenticatedUser() that works in the same way. It's also accessible like a controller property: authenticatedUser. It's mentioned [here](http://grails-plugins.github.io/grails-spring-security-core/docs/manual.1273/guide/single.html#24%20Controller%20MetaClass%20Methods) – Manuel Vio Jan 28 '14 at 15:41
0

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.

gsrandy
  • 36
  • 2