注入springSecurityService不起作用

Jar*_*red 0 grails grails-spring-security

在以下来自控制器的代码片段中,我在尝试访问springSecurityService.currentUser时遇到空指针异常.我希望def springSecurityService自动注入服务,我错过了什么?

@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
Run Code Online (Sandbox Code Playgroud)

Jos*_*ore 5

将其他Spring bean注入您的控制器或服务是在类级别而不是在方法中完成的.

例如,您的代码应如下所示:

@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
Run Code Online (Sandbox Code Playgroud)