您的位置:首页 > 编程语言 > Java开发

spring-security 在jsp中的标签库

2016-06-21 08:44 381 查看
spring-security 在jsp中的标签库
1.在jsp中声明

[html] view
plain copy

<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>  

2.标签
目前共有三个标签
   

[html] view
plain copy

<sec:authorize></sec:authorize>        

<sec:authentication property=""></sec:authentication>  

<sec:accesscontrollist hasPermission="" domainObject=""></sec:accesscontrollist>  

      
2.1、authorize标签
这个标签用来决定它的内容是否会被执行.

[html] view
plain copy

<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>  

显示一个特定的链接,如果用户允许点击它.

[html] view
plain copy

<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 对象的一个实例,
就要使用

[html] view
plain copy

<sec:authentication property="principal.username" />   

来渲染当前用户的名称。

当然,它不必使用JSP 标签来实现这些功能,一些人更愿意在视图中保持逻辑越少越好。你可以在你的MVC 控制器中访问Authentication 对象( 通过调用
SecurityContextHolder.getContext().getAuthentication()) 然后直接在模型中添加数据,来渲染视图。

2.3、accesscontrollist标签
这个标签纸在使用Spring Security ACL 模块时才可以使用。它检测一个用逗号分隔的特
定领域对象的需要权限列表。如果当前用户拥有这些权限的任何一个,标签内容就会被执行。
否则,就会被略过。

[html] view
plain copy

<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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring