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?
相关问题
- Grails External Configuration. Can't access to
- grails unit test + Thread
- Is it possible to use HTTPS only for login in Spri
- Null being passed to Spring Security UserDetailsSe
- JMS Job queue with Grails
相关文章
- Securing REST endpoint using spring security
- Listening to successful login with Spring Security
- How is SecurityContextLogoutHandler's clearAut
- Grails: How to make everything I create Upper Case
- Exception sending context initialized event to lis
- Spring java.lang.IllegalStateException: Cannot cre
- Java Spring Security: 401 Unauthorized for token O
- what is the use of auto-config=true in spring secu
If you simply need to check to see if a user is in a specific role then use
SpringSecurityUtils.ifAllGranted
which takes a single String as an argument which contains a comma-delimited list of roles. It will return true if the current user belongs to all of them.SpringSecurityUtils
also has methods likeifAnyGranted
,ifNotGranted
, etc, so it should work for whatever it is you are trying to accomplish.To get the user
You can inject
springSecurityService
into your controller:def springSecurityService
and then in your action, call:
def roles = springSecurityService.getPrincipal().getAuthorities()
See the docs here.
From a controller you can use two methods the plugin adds to the metaclass,
getPrincipal
andisLoggedIn
:If the action is secured you can skip the
loggedIn
/isLoggedIn()
check.SecurityContextHolder knows that:
You can also use
getAuthenticatedUser()
by itself. This method is automatically injected in every controller, and thus only available from controllers. You will have to use one of the other methods if you want to access the current logged in user from anywhere else.