spring 标签
2015-07-15 18:26
627 查看
Security框架可以精确控制页面的一个按钮、链接,它在页面上权限的控制实际上是通过它提供的标签来做到的
Security共有三类标签authorize authentication accesscontrollist ,第三个标签不在这里研究
前提:项目需要引用spring-security-taglibs-3.05,jstl1.2的jar包,页面加入:<%@ taglib
prefix=”sec” uri=”http://www.springframework.org/security/tags” %>
本文配置
一、authorize
对应的类: org.springframework.security.taglibs.authz.AuthorizeTag
attribute: access url method ifNotGranted ifAllGranted ifAnyGranted
使用方式:见SimpleDemo的index.jsp
<p>
<sec:authorize ifAllGranted="ROLE_ADMIN">#这里可以用逗号分隔,加入多个角色
你拥有管理员权限,你可以查看 该页面<a href="http://blog.163.com/sir_876/blog/admin.jsp">
管理员进入</a> </sec:authorize> </p> <p> <sec:authorize url='/profile.jsp'>你登陆成功了可以看到 <a href="http://blog.163.com/sir_876/blog/profile.jsp"> 这个页面</a></sec:authorize>
</p>
页面标签的使用与权限配置相对应
<intercept-url pattern="/admin.jsp" access="hasRole('ROLE_ADMIN')"
/> <intercept-url pattern="/profile.jsp" access="isAuthenticated()" /> <intercept-url pattern="/**" access="permitAll" />
对比可以看到只有ROLE_ADMIN角色的用户才能访问admin.jsp,通过认证的用户都可以访问profile.jsp
从标签源码可以知道,authorize标签判断顺序是: access->url->ifNotGranted->ifAllGranted->ifAnyGranted
但他们的关系是“与”: 即只要其中任何一个属性不满足则该标签中间的内容将不会显示给用户,举个例子:
<sec:authorize ifAllGranted=”ROLE_ADMIN,ROLE_MEMBER” ifNotGranted=”ROLE_SUPER”>满足才会显示给用户
</sec:authorize>
标签中间的内容只有在当前用户拥有ADMIN,MEMBER角色,但不拥有SUPER权限时才会显示
access属性是基于角色判断,url属性是基于访问路径判断,与security.xml配置对应
对于ifAllGranted ,ifNotGranted,ifAnyGranted属性的理解可以与集合api类比
Collection grantedAuths :当前用户拥有的权限
Collection requiredAuths : 当前要求的权限,即ifAllGranted ,ifNotGranted,ifAnyGranted
属性的值
满足ifAllGranted: 只需要grantedAuths.containsAll(requiredAuths);返回true即可
满足ifAnyGranted: 只需要grantedAuths.retainAll(requiredAuths);有内容即可(两集合有交集)
满足ifNotGranted:与Any相反,如果没有交集即可
二、authentication
对应的类: org.springframework.security.taglibs.authz.AuthenticationTag
attribute: property(required) var htmlEscape scope
使用方式:
<sec:authentication property=’name’ />
<sec:authentication property=’principal.username’ />
<sec:authentication property=’principal.enabled’ />
<sec:authentication property=’principal.accountNonLocked’ />
主要用来显示authentication属性,
var scope: 将property的值以var设置到scope域中
htmlEscape: 将特殊字符转义 > –> “>”
Security共有三类标签authorize authentication accesscontrollist ,第三个标签不在这里研究
前提:项目需要引用spring-security-taglibs-3.05,jstl1.2的jar包,页面加入:<%@ taglib
prefix=”sec” uri=”http://www.springframework.org/security/tags” %>
本文配置
一、authorize
对应的类: org.springframework.security.taglibs.authz.AuthorizeTag
attribute: access url method ifNotGranted ifAllGranted ifAnyGranted
使用方式:见SimpleDemo的index.jsp
<p>
<sec:authorize ifAllGranted="ROLE_ADMIN">#这里可以用逗号分隔,加入多个角色
你拥有管理员权限,你可以查看 该页面<a href="http://blog.163.com/sir_876/blog/admin.jsp">
管理员进入</a> </sec:authorize> </p> <p> <sec:authorize url='/profile.jsp'>你登陆成功了可以看到 <a href="http://blog.163.com/sir_876/blog/profile.jsp"> 这个页面</a></sec:authorize>
</p>
页面标签的使用与权限配置相对应
<intercept-url pattern="/admin.jsp" access="hasRole('ROLE_ADMIN')"
/> <intercept-url pattern="/profile.jsp" access="isAuthenticated()" /> <intercept-url pattern="/**" access="permitAll" />
对比可以看到只有ROLE_ADMIN角色的用户才能访问admin.jsp,通过认证的用户都可以访问profile.jsp
从标签源码可以知道,authorize标签判断顺序是: access->url->ifNotGranted->ifAllGranted->ifAnyGranted
但他们的关系是“与”: 即只要其中任何一个属性不满足则该标签中间的内容将不会显示给用户,举个例子:
<sec:authorize ifAllGranted=”ROLE_ADMIN,ROLE_MEMBER” ifNotGranted=”ROLE_SUPER”>满足才会显示给用户
</sec:authorize>
标签中间的内容只有在当前用户拥有ADMIN,MEMBER角色,但不拥有SUPER权限时才会显示
access属性是基于角色判断,url属性是基于访问路径判断,与security.xml配置对应
对于ifAllGranted ,ifNotGranted,ifAnyGranted属性的理解可以与集合api类比
Collection grantedAuths :当前用户拥有的权限
Collection requiredAuths : 当前要求的权限,即ifAllGranted ,ifNotGranted,ifAnyGranted
属性的值
满足ifAllGranted: 只需要grantedAuths.containsAll(requiredAuths);返回true即可
满足ifAnyGranted: 只需要grantedAuths.retainAll(requiredAuths);有内容即可(两集合有交集)
满足ifNotGranted:与Any相反,如果没有交集即可
二、authentication
对应的类: org.springframework.security.taglibs.authz.AuthenticationTag
attribute: property(required) var htmlEscape scope
使用方式:
<sec:authentication property=’name’ />
<sec:authentication property=’principal.username’ />
<sec:authentication property=’principal.enabled’ />
<sec:authentication property=’principal.accountNonLocked’ />
主要用来显示authentication属性,
var scope: 将property的值以var设置到scope域中
htmlEscape: 将特殊字符转义 > –> “>”
相关文章推荐
- MySQL 安全事宜
- 表格标签table深入了解
- WEB标准网页布局中尽量不要使用的HTML标签
- web标准知识――用途相似的标签
- h1标签的使用技巧
- HTML代码中标签的全部属性 中文注释说明
- jquery 实现两Select 标签项互调示例代码
- jQuery获取标签文本内容和html内容的方法
- JavaScript获取并更改input标签name属性的方法
- css样式标签和js语法属性区别
- 关于用Jquery的height()、width()计算动态插入的IMG标签的宽高的问题
- jQuery如何获取同一个类标签的所有值(默认无法获取)
- 解决JSTL foEach标签 刷新报错的方法
- js改变embed标签src值的方法
- ThinkPHP模板判断输出Empty标签用法详解
- php实现html标签闭合检测与修复方法
- ThinkPHP模板范围判断输出In标签与Range标签用法详解
- ThinkPHP分组下自定义标签库实例
- 彻底删除thinkphp3.1案例blog标签的方法
- thinkphp特殊标签用法概述