abdul
abdul

Reputation: 1581

Cannot invoke method isLoggedIn() on null object

I actually have two spring security related question.

1, I have the below code in my controller for changing password

def changePassword() {

        if (!securityService.isLoggedIn()) {
            redirect(action: 'auth', controller: 'login')
            return
        }
}

when i hit save button after confirming the new password it generates te following error

NullPointerException occurred when processing request: [POST] 
Cannot invoke method isLoggedIn() on null object.

Now My question is where am i wrong?

2, my second question is also similar am trying to get the current user like this

User currentUser = securityService.getCurrentUser()
            currentUser.gender = gender
            currentUser.save()

But still its generating the same error
Cannot invoke method getCurrentUser() on null object

NOTE:- I am using grails 2.4.4 and spring security 2.0-RC4.

UPDATE:- I did include def securityService in my controller/service

Upvotes: 0

Views: 973

Answers (2)

Anant Kolvankar
Anant Kolvankar

Reputation: 1050

Just add 'def springSecurityService' in your controller

Class UserController{

  def springSecurityService

  def changePassword(){
      //You can access springSecurityService now
    if (!springSecurityService.isLoggedIn()) {
        redirect(action: 'auth', controller: 'login')
        return
    }
  }
}

Upvotes: 1

Mike Sickler
Mike Sickler

Reputation: 34431

The SpringSecurityService is autowired to controllers this way:

   def springSecurityService

And with Groovy, you can call springSecurityService.currentUser instead of getCurrentUser()

Upvotes: 1

Related Questions