首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

Spring Security入门篇——标签的运用

2012-12-14 
Spring Security入门篇——标签的使用转自个人原创blog: http://www.javali.org/archive/getting-start-spri

Spring Security入门篇——标签的使用

转自个人原创blog: http://www.javali.org/archive/getting-start-spring-security-tags.html

Security框架可以精确控制页面的一个按钮、链接,它在页面上权限的控制实际上是通过它提供的标签来做到的。

Security共有三类标签authorize? authentication?? accesscontrollist? ,第三个标签不在这里研究

?

相关文章:

Spring Security入门篇——搭建简易权限框架:http://www.javali.org/archive/getting-start-spring-security.html

Spring Security入门篇——集成DB认证:http://www.javali.org/archive/getting-start-spring-security-integrate-db.html

Spring security进阶——扩展UserDetails:http://www.javali.org/archive/spring-security-advanced-extend-userdetails.html

Spring security进阶——资源汉化(i18n):http://www.javali.org/archive/spring-security-advanced-i18n.html

准备

项目需要引用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

我们怎么用authorize?

可以先去另外一篇文章(Spring Security入门篇——搭建简易权限框架) 下载SimpleDemo源码,其中index.jsp有它的使用范例

1)需求

我们在一个页面有两个链接,1:管理员进入? 2:个人信息页;

前者需要管理员身份才能查看,后者需要用户登录才能查看,我们分析下如何通过authorize标签实现的

2)配置

与spring强大的配置功能类似,要让spring security能正常工作需要配置正确(建议结合Demo工程),security.xml配置如下

<intercept-urlpattern="/admin.jsp"access="hasRole('ROLE_ADMIN')" />        <intercept-urlpattern="/profile.jsp"access="isAuthenticated()" />         <intercept-url        pattern="/**"access="permitAll" />
?

?

这么配置正好能符合我们开始的需求,这个大家对比看看,应该非常好理解 页面使用标签

----------------------------------------------以下内容引用自index.jsp------------------------------------

<sec:authorize ifAllGranted="ROLE_ADMIN">

你拥有管理员权限,就可以查看 该页面<a href="admin.jsp"> 管理员进入</a> </sec:authorize> </p> <p> <sec:authorize url='/profile.jsp'>你登陆成功了可以看到 <a href="profile.jsp"> 这个页面</a></sec:authorize>

----------------------------------------------引用结束----------------------

页面标签的使用与权限配置相对应,到此就已经实现了我们的需求了。

3)进阶

从标签源码可以知道,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: 将特殊字符转义 > --> "&gt"

?

?

热点排行