根据@David 发布的内容,我能够让它为我工作。这就是我所做的:
package myproject
import com.somepackage.ShiroUser
import org.apache.shiro.SecurityUtils
class UserTagLib {
static defaultEncodeAs = [taglib:'html']
//static encodeAsForTags = [tagName: [taglib:'html'], otherTagName: [taglib:'none']]
static namespace = "user"
def loggedInUser = { attrs, body ->
def user = _currentUser()
if (!user) return
def prop = user[attrs.property]
if (prop) out << prop.encodeAsHTML()
}
def _currentUser() {
def subject = SecurityUtils.subject
if (!subject.getPrincipal()) return // No-one logged-in
return User.findByUsername(subject.getPrincipal().toString())
}
}
(修改部分在 中_currentUser()
)
然后在视图中:
...
<div class="someClass">
<shiro:isLoggedIn>Hello, <user:loggedInUser property="fullName"/> </shiro:isLoggedIn>
</div>
...