Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get current user role with spring security plugin?

Tags:

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?

like image 571
laxmi Avatar asked Jun 24 '11 11:06

laxmi


People also ask

How do you find the current user role?

If the user is logged in, we use wp_get_current_user to return the WP_User object. This provides us with a stack of information about the data and we can access their user role(s) via $user->roles . Note that I've cast this to an array for safety. At this stage, you can choose how to return the roles.

How do I get principal role in Spring Security?

The first way to check for user roles in Java is to use the @PreAuthorize annotation provided by Spring Security. This annotation can be applied to a class or method, and it accepts a single string value that represents a SpEL expression. Before we can use this annotation, we must first enable global method security.


1 Answers

You can inject springSecurityService into your controller:

def springSecurityService

and then in your action, call:

def roles = springSecurityService.getPrincipal().getAuthorities()

See the docs here.

like image 105
Mike Sickler Avatar answered Oct 23 '22 06:10

Mike Sickler