How to get current_user by using Spring Security Grails plugin in GSP

后端 未结 4 1004
花落未央
花落未央 2021-01-13 13:42

I am newbie in Grails. I am using Spring Security Grails plugin for Authentication purpose. I want to get current user in my view gsp file.

I am trying like this ..

4条回答
  •  耶瑟儿~
    2021-01-13 14:24

    Another way would be to create a Filter and put the User in the request scope as part of the filter, like this:

    class SetCurrentUserFilters {
        def springSecurityService
        def filters = {
            all(controller: '*', action: '*') {
                before = {
                    if (springSecurityService.isLoggedIn()){
                        request.setAttribute("current_user", springSecurityService.currentUser);
                    }
                }
                after = { Map model ->
    
                }
                afterView = { Exception e ->
    
                }
            }
        }
    }
    

    Then your GSP just needs to look for the 'current_user' attribute, like this:

     ... 
    

提交回复
热议问题