How to get the id of the current user registered in grails?

I want to display information about a user registered as his data, etc., specified during registration ... how to do this? How am I new to help grails plz! iam using Spring Security Plugin

+5
source share
3 answers

Well, you can use the springSecurityService function to get user information in the controller:

    class MyController {
      def springSecurityService

      def myAction() {
        def principal = springSecurityService.principal
        String username = principal.username
        ...
      }
     }

Or in gsp

    <sec:loggedInUserInfo field="username" />

This is a pretty general question.

+14
source

Define (in the controller or service where you need the user ID):

SpringSecurityService springSecurityService

and try:

springSecurityService.currentUser.id //loads user from db, and returns its id

or

springSecurityService.principal.id //if you need just id
+9
source

add this @ to the top of your service or controller class

def securityService

this returns the cuureent user id.

securityService.currentUser.id 
+1
source

All Articles