2013-01-22 40 views
1

假设我有一个配置文件页面,其中有一个'编辑您的配置文件'链接。所有用户都可以查看配置文件页面,但编辑链接按钮应仅对登录的用户查看其配置文件而非其他用户的配置文件可见。Spring Security - 显示特定于登录用户的内容

截至目前,我有这样的代码,

<sec:authorize access="isAuthenticated()"> 
<sec:authentication property="principal.username" var="principal"/> 
<c:if test="${profile_username eq principal}"> <!--profile_username is the username of the viewed profile --> 
<!-- edit your profile link --> 
</c:if> 
</sec:authorize>  

是否有这样一个干净的方式?可能是像

<sec:authorize access="isTheSamePerson()"/>一个班轮。

在此先感谢。 :)

+0

您不应该需要sec:authorize标记,如果用户未登录,主体对象将为空,并且c:if测试将失败。不完全是你想要的,但它会把它清理一点。 – dardo

+0

不错。但是,如果没有sec:auth标记,这将无法正常工作。它会抛出一个异常。 – shazinltc

+0

好的,这是跛脚 – dardo

回答

1

你想要考虑到实际的域对象。 Spring Security中有特殊的ACL功能用于这些目的。您可以设置它,并使用相应的accesscontrollist标签:

<sec:accesscontrollist hasPermission="2" domainObject="${profile}"> 
    <!-- Your edit link goes here --> 
    <!-- "2" means write permission --> 
    <!-- Be sure that you use Spring Security >= 3.1.2. This syntax may not works for smaller versions due to bugs --> 
</sec:accesscontrollist> 

如果你只有一个像这样的情况可能是矫枉过正。

件编号2.您可以定义自定义Web安全表达式:

<sec:authorize access="isOwner(#profile)"/>. 

这不是so simple too

我认为定制JSP标记(tag file)将是最简单的解决办法:

<customtags:authorizeeditaccount account="${profile}"/> 

这个标签会做同样的事情。它会看起来好多了。

+0

好的解决方案如果我有太多的情况。否则将是一个矫枉过正。 – shazinltc