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

前端 未结 3 917
失恋的感觉
失恋的感觉 2021-01-18 18:24

I want to display the info about the user logged in like his details etc given during sign up ...how to do it ?? As iam new to grails plz help! iam using Spring security plu

相关标签:
3条回答
  • 2021-01-18 19:03

    Well you can use the springSecurityService to get some user information in a controller:

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

    Or in a gsp

        <sec:loggedInUserInfo field="username" />
    

    It is a pretty general question.

    0 讨论(0)
  • 2021-01-18 19:06

    Define (in a controller, or service where you need 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
    
    0 讨论(0)
  • 2021-01-18 19:07

    add this @the top of your service or controller class

    def securityService
    

    this returns the cuureent user id.

    securityService.currentUser.id 
    
    0 讨论(0)
提交回复
热议问题