2013-12-16 55 views
2

情況是我有一個佈局application.gsp,它定義除登錄頁面以外的所有頁面的佈局。佈局內部是一個標題,它應該顯示當前登錄用戶的名稱(Spring Security插件)。問題是,如果用戶第一次註冊或使用oAuth(與註冊相同),那麼當他登錄時,他將看到並清空點而不是用戶名,因爲在用戶名可用之前,佈局呈現。如果他再次註銷/登錄,他當然會看到他的用戶名。運行時Grails更新/重新排列布局

P.S.將標題移動到佈局外不是一種選擇,因爲它會導致大量的代碼重複。

有沒有辦法解決這個問題?

+0

您使用的是Spring Security Core Plugin嗎? –

+0

你曾經說過,將標題移動到佈局之外不是一種選擇,但是可以使用ag:include來顯示登錄用戶,如果登錄用戶存在,可能會觸發控制器以獲取登錄名,並且呈現它? – bschipp

+0

您不能將用戶從身份驗證操作轉發到主頁操作後,他們已經登錄?用戶名將在渲染視圖時可用。 – Armand

回答

1

我會回答我的問題,爲子孫後代試圖解決同樣的問題。
訣竅是使用修飾器設計模式和記錄不好的g:pageProperty gsp標記。

視圖/佈局/ application.gsp

.... 
<li class="dropdown menu"> 
         <a href="#" class="dropdown-toggle" 
          data-toggle="dropdown"><g:pageProperty name="page.username"/></a> 
         <ul class="dropdown-menu"> 
          <li>Preferences</li> 
          <li> 
           <a href="${resource(file: 'j_spring_security_logout')}" 
            class="navbar-btn btn-danger btn">Logout</a></li> 
         </ul> 
        </li> 
.... 

視圖/ index.gsp中:

.... 
<body> 
    <content tag="username"> 
     <g:username username="${username}"/> 
    </content> 
.... 

標籤庫/ UsernameTagLib.groovy:

class UsernameTagLib { 
def springSecurityService 

/** 
* Check if the username is already available, else inject newly created username into the layout 
* 
* @attr username REQUIRED that was received from the newly registered user 
*/ 
def username = { attrs -> 
    String currentUsername = springSecurityService.getCurrentUser()?.username 
    if (currentUsername) { 
     out << currentUsername 
    } else { 
     out << attrs.username 
    } 
    } 
} 

當用戶完成整個oAuth進程時,新創建的用戶名將傳遞給views/index.gsp

控制器/ OauthCallBackController.groovy

.... 
    def facebook() { 
     Token facebookAccessToken = (Token) session[oauthService.findSessionKeyForAccessToken('facebook')] 
     if (facebookAccessToken) { 
      String username = oauthCallBackService.facebook(facebookAccessToken) 
      render view: '/index', model: [username: username] 
     } else failure() 
    } 
.... 

基本上用戶名向上行進,直到其到達的佈局,這允許我使用的用戶名在佈局頭和傳播完成它在需要的所有頁佈局。

+0

您是否必須爲使用該佈局的每個gsp添加相同的標籤? –

+0

@EdJ不幸的是,它已經很久以前,我不記得了,從那時起就沒有使用過Grails – maryokhin

0

如果您使用的是Spring Security Core plugin,您可以在您的控制器中使用reauthenticate用戶。

springSecurityService.reauthenticate user.username 
+0

這就是我如何驗證用戶的方式,但它與用戶驗證前的佈局渲染問題無關 – maryokhin

0

您可以SecurityTagLib輔助彈簧安芯的檢查。

在你application.gsp你可以有:

... 
<sec:ifLoggedIn> 

<!-- Display user field with sec:loggedInUserInfo tag --> 
Welcome Back <sec:loggedInUserInfo field="fullName"/> 

<!-- Check if user have all specified privileges --> 
<sec:ifAllGranted roles="ROLE_ADMIN,ROLE_SUPERVISOR"> 
    You've ROLE_ADMIN AND ROLE_SUPERVISOR privileges. 
</sec:ifAllGranted> 

<!-- Check if user have at least one specified privileges --> 
<sec:ifAnyGranted roles="ROLE_ADMIN,ROLE_SUPERVISOR"> 
    You've ROLE_ADMIN OR ROLE_SUPERVISOR privileges. 
</sec:ifAnyGranted> 

</sec:ifLoggedIn> 

<sec:ifNotLoggedIn> 

It's public content, anonymous user. 

</sec:ifNotLoggedIn> 
... 

您還可以使用:<sec:ifNotGranted roles="ROLE_USER"></sec:ifNotGranted>