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

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