简体   繁体   中英

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?

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"/>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM