Grails with SpringSecurity, check if the current user can access controller / action

前端 未结 8 2245
轮回少年
轮回少年 2021-02-15 13:57

I\'m currently developing a menu for my application that should be able to display only the controllers that the current user can access (requestmap defined in the database).

8条回答
  •  生来不讨喜
    2021-02-15 13:58

    Not sure of the situation when this question was originally asked, but now you can check to see if a user is in a specific role by using SpringSecurityUtils.ifAllGranted() which takes a single String which is a comma delimited list of roles. It will return true if the current user belongs to all of them.

    if(SpringSecurityUtils.ifAllGranted('ROLE_ADMIN,ROLE_USER')) {
    

    Obviously, you can simply pass one role to the function if that is all you need. SpringSecurityUtils also has methods like ifAnyGranted, ifNotGranted, etc, so it should work for whatever it is you are trying to accomplish.

    SpringSecurityUtils is a static API, so you don't need to create a private member named springSecurityUtils or anything like that.

提交回复
热议问题