spring-security 在jsp中的標籤庫

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

2.標籤
目前共有三個標籤
   
[html] view plain copy
  1. <sec:authorize></sec:authorize>        
  2. <sec:authentication property=""></sec:authentication>  
  3. <sec:accesscontrollist hasPermission="" domainObject=""></sec:accesscontrollist>  

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



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



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

來渲染當前用戶的名稱。

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

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

參考:Spring_Security-3.0.1_中文官方文檔(翻譯版).pdf
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章