Bean
Bean

Reputation: 550

Grails Spring Security Username globally

I am learning the ins and outs of Spring Security...it has been slow going...

Right now I'm trying to access the currently logged in users Username from the index.gsp page. I have code that works from Controllers, but I can't get the code right for any other pages. I have a feeling I am missing something key in regards to the User object.

Here's what I use for my controller:

class BookController {

    def springSecurityService

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]


    def index() {
        redirect(action: "list", params: params)
    }

    def list(Integer max) {
        def userDetails = springSecurityService.principal
        params.max = Math.min(max ?: 10, 100)
        [bookInstanceList: Book.list(params), bookInstanceTotal: Book.count(), muds: userDetails]
    }
...
... etc
...

Then in the View (gsp) I can simply do something like:

Username: ${muds.getUsername() }

How would I do this in index.gsp?

Upvotes: 0

Views: 174

Answers (2)

Amit
Amit

Reputation: 606

Do you get any errors with ${muds.getUsername() }? Try using just ${muds} or better check the class of ${muds}

Upvotes: 0

Joshua Moore
Joshua Moore

Reputation: 24776

You are better off using the built in tag library that comes with the plugin. The documentation shows all the various tags, one of which is for getting properties of the current logged in user. As an example:

<sec:loggedInUserInfo field="username"/>

Upvotes: 4

Related Questions