0

I am developing an ordering web application and I am new to learning Grails. I used the Spring Security plugin. I haved configured and override the login and register, what I want is to get the currently logged in user to apply an order. Is there a way to do this without using the static belongsTo? Because using this always shows me a dropdown box where I can choose between the users and admins. Am I doing the right approach in getting the logged in user to order?

Burt Beckwith
  • 75,342
  • 5
  • 143
  • 156

1 Answers1

0

The simplest way to retrieve the currently authenticated principal is via a static call to the SecurityContextHolder:

Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (!(authentication instanceof AnonymousAuthenticationToken)) {
    String currentUserName = authentication.getName();
    return currentUserName;
}

OR

Inject SpringSecurityService bean, like this into controller

def springSecurityService 

and then call

def currentPrincipal = springSecurityService.principal

print "The details of current logged in user is" + currentPrincipal

For more details follow this link http://www.baeldung.com/get-user-in-spring-security

How to get the current logged in user object from spring security?

Community
  • 1
  • 1
Dipak Thoke
  • 1,963
  • 11
  • 18
  • 4
    The easiest way with the plugin is to dependency-inject the `SpringSecurityService` bean, e.g. `def springSecurityService` and call `def currentPrincipal = springSecurityService.principal` – Burt Beckwith Nov 07 '16 at 08:53