How to get current user role with spring security plugin?

前端 未结 6 1931
一个人的身影
一个人的身影 2020-12-29 05:21

I am using the spring-security-core plugin in my grails app. I need to know the current user\'s role in a controller action. How can I retrieve that?

6条回答
  •  死守一世寂寞
    2020-12-29 06:03

    From a controller you can use two methods the plugin adds to the metaclass, getPrincipal and isLoggedIn:

    def myAction = {
       if (loggedIn) {
          // will be a List of String
          def roleNames = principal.authorities*.authority
       }
    }
    

    If the action is secured you can skip the loggedIn/isLoggedIn() check.

提交回复
热议问题