我可以在Grails 2中使用Spring安全性保护单个选项卡吗?

时间:2009-06-14 06:05:00

标签: security grails

如果我使用Grails GUI选项之一创建标签(我应该使用哪一个),是否可以根据当前用户打开和关闭标签?例如,只有具有admin角色的用户才能看到“管理用户”选项卡。甚至匿名用户也应该看到主要内容选项卡。

理想情况下,我想使用Spring Security ACL。

3 个答案:

答案 0 :(得分:0)

是的,使用Acegi(Spring Security Plugin)可能非常简单,请参阅this section of the docs。您需要定义一些角色,然后描述这些角色如何应用于不同的URL。示例(来自上面的链接):

/管理/的 = ROLE_USER /书/测试/ = IS_AUTHENTICATED_FULLY /书/ ** = ROLE_SUPERVISOR

在Grails In Action一书中还有关于如何做到这一点的解释。

答案 1 :(得分:0)

Spring Security为此提供了一个很好的标记库。您可以使用类似于此的内容来分隔呈现标签:

<sec:authorize access="hasRole('supervisor')">

This content will only be visible to users who have
the "supervisor" authority in their list of <tt>GrantedAuthority</tt>s.

</sec:authorize>

您可以正常启用jsp标记:

<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>

详细了解here if you like

答案 2 :(得分:0)

要添加,有时您可以在视图(GSP)中添加太多逻辑和编码。您可以使用其他选项(如导航插件和Spring-Security插件)将更多代码推送到控制器。好处是视图只是清除条件标签。

grails install-plugin navigation

然后在控制器中使用@Secured注释。例如,我创建了两个带有两个相应控制器的选项卡。

@Secured(['ROLE_ADMIN'])
class SlidesController {

static navigation = [
    group:'tabs', order:10, title:'Users', action:'index'
]
def index = {
      .....
}

@Secured(['ROLE_ADMIN'])
class ProgramsController {

static navigation = [
    group:'tabs', order:10, title:'Programs & Presentation', action:'index'
]
def index = {
      .....
}

在视图中:

<head>
     ... other head elems.
  <nav:resources/>
</head>

<body>
<nav:render/>
    ... Your other stuff
</body>

自动显示标签(对于使此视图成为布局GSP也很有用)。

enter image description here