spring security 在jsp中的標籤庫

spring-security 在jsp中的標籤庫
1.在jsp中聲明
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>

2.標籤
目前共有三個標籤
   
<sec:authorize></sec:authorize>      
<sec:authentication property=""></sec:authentication>
<sec:accesscontrollist hasPermission="" domainObject=""></sec:accesscontrollist>

      
2.1、authorize標籤
這個標籤用來決定它的內容是否會被執行.
<sec:authorize access="hasRole('supervisor')">
    This content will only be visible to users who have
    the "supervisor" authority in their list of GrantedAuthoritys.
</sec:authorize>



顯示一個特定的鏈接,如果用戶允許點擊它.
<sec:authorize url="/admin">
    This content will only be visible to users who are authorized to send requests to the "/admin" URL.
</sec:authorize>



2.2、authentication標籤
這個標籤允許訪問當前的Authentication 對象, 保存在安全上下文中。
比如,如果Authentication 的principal 屬性是Spring Security 的UserDetails 對象的一個實例,
就要使用
<sec:authentication property="principal.username" /> 

來渲染當前用戶的名稱。

當然,它不必使用JSP 標籤來實現這些功能,一些人更願意在視圖中保持邏輯越少越好。你可以在你的MVC 控制器中訪問Authentication 對象( 通過調用
SecurityContextHolder.getContext().getAuthentication()) 然後直接在模型中添加數據,來渲染視圖。

2.3、accesscontrollist標籤
這個標籤紙在使用Spring Security ACL 模塊時纔可以使用。它檢測一個用逗號分隔的特
定領域對象的需要權限列表。如果當前用戶擁有這些權限的任何一個,標籤內容就會被執行。
否則,就會被略過。
<sec:accesscontrollist hasPermission="1,2" domainObject="${someObject}">
    This will be shown if the user has either of the permissions
    represented by the values "1" or "2" on the given object.
</sec:accesscontrollist>

參考:Spring_Security-3.0.1_中文官方文檔(翻譯版).pdf

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章