如何在gsp視圖中使用springSecurity訪問當前登錄的用戶名,或者更具體地說,在文本字段中使用此值?在視圖中訪問當前登錄的用戶名和彈簧安全性
下面將該溶液不起作用:
值= 「$ {sec.loggedInUserInfo(字段: '用戶名')}」
如何在gsp視圖中使用springSecurity訪問當前登錄的用戶名,或者更具體地說,在文本字段中使用此值?在視圖中訪問當前登錄的用戶名和彈簧安全性
下面將該溶液不起作用:
值= 「$ {sec.loggedInUserInfo(字段: '用戶名')}」
下面線在我的測試都正常工作。
<input name="username" type="text" value="${sec.username()}" />
<input name="username" type="text" value="${sec.loggedInUserInfo(field: 'username')}" />
錯誤你可能來自其他地方。您可以通過在GSP中使用該輸入標籤進行測試嗎?
自定義Taglib選項獲取關於用戶登錄到視圖中的各種信息。
ExamplNameTagLib.groovy
class ExamplNameTagLib {
static namespace = 'foo'
def springSecurityService
def currentUserProps = { attrs ->
def user = springSecurityService.getCurrentUser()
if (user) {
if (attrs.username) {
out << user.username
} else if (attrs.firstName) {
out << user.firstName
} else if (attrs.lastName) {
out << user.lastName
} else if (attrs.email) {
out << user.email
}
}
}
}
exampleViewPage.gsp
//Return username from taglib of the logged in user
<g:field type="text"
name="username"
value="${foo.currentUserProps(username: true)}" />
//Return email from taglib of the logged in user
<g:field type="email"
name="email"
value="${foo.currentUserProps(email: true)}"/>
什麼錯誤?這將有助於包含堆棧跟蹤,因爲「不起作用」非常模糊。 – ikumen
我沒有錯誤,但它不顯示任何內容 – zgaw87
我擔心你將無法使用'sec' taglib,因爲它會寫入DOM。你可以在'controller'操作中使用'springSecurityService.authentication.name',並在'model'中設置可以在視圖中使用的值,除非我沒有認識到直接的過程。 – dmahapatro