Jared
Jared

Reputation: 39887

Injection of springSecurityService does not work

In the following code snippet from a controler I get a null pointer exception when trying to access springSecurityService.currentUser. I would expect def springSecurityService to automatically inject the service, what am I missing?

@Transactional(readOnly = true)
@Secured('ROLE_USER')
class TaskController {

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

    def index(Integer max) {
        params.max = Math.min(max ?: 10, 100)
def springSecurityService
def user = springSecurityService.currentUser

Upvotes: 0

Views: 221

Answers (1)

Joshua Moore
Joshua Moore

Reputation: 24776

Injecting other Spring beans into your controllers or services is done at the class level and not within a method.

For example, your code should look like:

@Transactional(readOnly = true)
@Secured('ROLE_USER')
class TaskController {

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]
    def springSecurityService // inject the bean here, at the class level

    def index(Integer max) {
        params.max = Math.min(max ?: 10, 100)
        def user = springSecurityService.currentUser

Upvotes: 5

Related Questions