2013-01-22 45 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