2013-12-16 54 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>