2008-09-08
缩略显示[置顶] 如何在Spring中集成Acegi 2.x安全框架
关键字: acegi, spring security
Acegi是基于Spring的一个开源的安全认证框架,现在的最新版本是Spring Security 2.0。新版本增加了许多新的特性:新特性包括简化的配置,并增加了新的功能,包括OpenID、NTLM、JSR 250注解、AspectJ切入点(pointcut)支持、域ACL增强、RESTful URI授权、组、分级角色、用户管理API、数据库支持的“remember me”,portlet认证、其他语言、Web Flow 2.0支持、Spring IDE可视化及自动完成、通过Spring Web Services 1.5提供的增强WSS支持等等。其中,最明显的就是配置文件的简化。
1.x版本需要配置一个过滤器链以及各个过滤器对应的bean,各个过滤器一般有先后顺序,顺序颠倒了会出现一些意想不到的异常,无疑增加了配置的复杂性。在2.x版本中,框架将过滤器链和各个过滤器的先后顺序作了固化,以降低配置的复杂性、减少配置出错的机率。
下面将详细介绍Spring Security 2.0的配置。
一、在web.xml中的配置
Xml代码
<filter>
<filter-name>securityFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<init-param>
<param-name>targetBeanName</param-name>
<param-value>springSecurityFilterChain</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>securityFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 当过滤器的名称不是springSecurityFilterChain时,需要配置targetBeanName参数,参数值为springSecurityFilterChain -->
<filter>
<filter-name>securityFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<init-param>
<param-name>targetBeanName</param-name>
<param-value>springSecurityFilterChain</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>securityFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 当过滤器的名称不是springSecurityFilterChain时,需要配置targetBeanName参数,参数值为springSecurityFilterChain -->
二、在security.xml中的配置
1、namespace的引用
Xml代码
<b:beans xmlns="http://www.springframework.org/schema/security"
xmlns:b="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.1.xsd">
<b:beans xmlns="http://www.springframework.org/schema/security"
xmlns:b="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.1.xsd"> 2、http节点的配置
Xml代码
<http auto-config="true" lowercase-comparisons="false" path-type="ant" access-denied-page="/accessDenied.jsp" access-decision-manager-ref="accessDecisionManager">
<intercept-url pattern="/index.jsp*" access="ROLE_ANONYMOUS"/>
<intercept-url pattern="/logout.jsp*" access="ROLE_ANONYMOUS"/>
<intercept-url pattern="/accessDenied.jsp*" access="ROLE_ANONYMOUS"/>
<intercept-url pattern="/**/*.jsp*" access="ADMIN,SYS_MANAGER"/>
<intercept-url pattern="/**/*.htm*" access="ADMIN,SYS_MANAGER"/>
<intercept-url pattern="/**/*.html*" access="ADMIN,SYS_MANAGER"/>
<intercept-url pattern="/**/*.action*" access="ADMIN,SYS_MANAGER"/>
<intercept-url pattern="/**/*.ftl*" access="ADMIN,SYS_MANAGER"/>
<form-login login-page="/index.jsp" always-use-default-target="true" login-processing-url="/j_security_check" authentication-failure-url="/index.jsp?login_error=1" default-target-url="/main.action"/>
<concurrent-session-control max-sessions="1" exception-if-maximum-exceeded="false"/>
<logout logout-url="/j_security_logout" logout-success-url="/index.jsp" invalidate-session="true"/>
</http>
<http auto-config="true" lowercase-comparisons="false" path-type="ant" access-denied-page="/accessDenied.jsp" access-decision-manager-ref="accessDecisionManager">
<intercept-url pattern="/index.jsp*" access="ROLE_ANONYMOUS"/>
<intercept-url pattern="/logout.jsp*" access="ROLE_ANONYMOUS"/>
<intercept-url pattern="/accessDenied.jsp*" access="ROLE_ANONYMOUS"/>
<intercept-url pattern="/**/*.jsp*" access="ADMIN,SYS_MANAGER"/>
<intercept-url pattern="/**/*.htm*" access="ADMIN,SYS_MANAGER"/>
<intercept-url pattern="/**/*.html*" access="ADMIN,SYS_MANAGER"/>
<intercept-url pattern="/**/*.action*" access="ADMIN,SYS_MANAGER"/>
<intercept-url pattern="/**/*.ftl*" access="ADMIN,SYS_MANAGER"/>
<form-login login-page="/index.jsp" always-use-default-target="true" login-processing-url="/j_security_check" authentication-failure-url="/index.jsp?login_error=1" default-target-url="/main.action"/>
<concurrent-session-control max-sessions="1" exception-if-maximum-exceeded="false"/>
<logout logout-url="/j_security_logout" logout-success-url="/index.jsp" invalidate-session="true"/>
</http> 说明:
lowercase-comparisons:表示URL比较前先转为小写。
path-type:表示使用Apache Ant的匹配模式。
access-denied-page:访问拒绝时转向的页面。
access-decision-manager-ref:指定了自定义的访问策略管理器。当系统角色名的前缀不是默认的ROLE_时,需要自定义访问策略管理器。
login-page:指定登录页面。
login-processing-url:指定了客户在登录页面中按下 Sign In 按钮时要访问的 URL。与登录页面form的action一致。其默认值为:/j_spring_security_check。
authentication-failure-url:指定了身份验证失败时跳转到的页面。
default-target-url:指定了成功进行身份验证和授权后默认呈现给用户的页面。
always-use-default-target:指定了是否在身份验证通过后总是跳转到default-target-url属性指定的URL。
logout-url:指定了用于响应退出系统请求的URL。其默认值为:/j_spring_security_logout。
logout-success-url:退出系统后转向的URL。
invalidate-session:指定在退出系统时是否要销毁Session。
max-sessions:允许用户帐号登录的次数。范例限制用户只能登录一次。
exception-if-maximum-exceeded: 默认为false,此值表示:用户第二次登录时,前一次的登录信息都被清空。
当exception-if-maximum-exceeded="true"时系统会拒绝第二次登录。
3、自定义访问决策管理器的配置
Java代码
<b:bean id="accessDecisionManager" class="org.springframework.security.vote.AffirmativeBased">
<b:property name="allowIfAllAbstainDecisions" value="false"/><!-- 设定是否允许 “没人反对就通过” 的投票策略 -->
<b:property name="decisionVoters"><!-- 投票者 -->
<b:list>
<b:bean class="org.springframework.security.vote.RoleVoter">
<b:property name="rolePrefix" value=""/><!-- 投票者支持的权限前缀,默认是“ROLE_” -->
</b:bean>
</b:list>
</b:property>
</b:bean>
<b:bean id="accessDecisionManager" class="org.springframework.security.vote.AffirmativeBased">
<b:property name="allowIfAllAbstainDecisions" value="false"/><!-- 设定是否允许 “没人反对就通过” 的投票策略 -->
<b:property name="decisionVoters"><!-- 投票者 -->
<b:list>
<b:bean class="org.springframework.security.vote.RoleVoter">
<b:property name="rolePrefix" value=""/><!-- 投票者支持的权限前缀,默认是“ROLE_” -->
</b:bean>
</b:list>
</b:property>
</b:bean> 说明:
Acegi提供三种投票通过策略的实现:
1、AffirmativeBased(至少一个投票者同意方可通过)
2、ConsensusBased(多数投票者同意方可通过)
3、UnanimousBased(所有投票者同意方可通过)
4、DAO身份验证提供者的配置
Java代码
<authentication-provider user-service-ref="userDao"/>
<authentication-provider user-service-ref="userDao"/>
三、登录页面
Html代码
<form method="post" id="frmLogin" name="frmLogin" action="<s:url value="/j_security_check"/>">
用户:<input type="text" name="j_username" id="j_username"/>
密码:<input type="password" name="j_password" id="j_password"/>
<input type="submit" value="Sign In"/>
</form>
<form method="post" id="frmLogin" name="frmLogin" action="<s:url value="/j_security_check"/>">
用户:<input type="text" name="j_username" id="j_username"/>
密码:<input type="password" name="j_password" id="j_password"/>
<input type="submit" value="Sign In"/>
</form> 说明:
1、form的action值必须与security.xml配置文件中form-login节点的login-processing-url属性值一致。
2、用于输入用户名的input控件name必须为j_username。
3、用于输入密码的input控件name必须为j_password。
分享到:
相关推荐
详细的spring security2.x配置
我在配置spring security2.0是的工作笔记 很有启发性
======================================================================== <br>第1篇 概述 第1章 Spring概述 第2章 快速入门 第2篇 Spring核心技术 第3章 IoC容器概述 第4章 在IoC容器中...
======================================================================== <br>第1篇 概述 第1章 Spring概述 第2章 快速入门 第2篇 Spring核心技术 第3章 IoC容器概述 第4章 在IoC容器中...
详细介绍了Spring Security(Acegi)的各种功能,罗时飞出品
======================================================================== <br>第1篇 概述 第1章 Spring概述 第2章 快速入门 第2篇 Spring核心技术 第3章 IoC容器概述 第4章 在IoC容器中...
A.2. 持久登陆(Remember-Me)表 A.3. ACL表 A.3.1. Hypersonic SQL A.3.1.1. PostgreSQL B. 安全命名空间 B.1. Web应用安全 - <http> 元素 B.1.1. <http> 属性 B.1.1.1. servlet-api-provision B....
用acegi 实现权限控制的例子 精通spring2.x企业级应用17章 想 来看看的朋友最好有这本书
struts + spring + hibernate + velocity + ajax + jotm + acegi
Spring Security中可以使用Acegi-1.x时代的普通配置方式,也可以使用从2.0时代才出现的命名空间配置方式,实际上这两者实现的功能是完全一致的,只是新的命名空间配置方式可以把原来需要几百行的配置压缩成短短的几...
A.2. 持久登陆(Remember-Me)表 A.3. ACL 表 A.3.1. Hypersonic SQL A.3.1.1. PostgreSQL B. 安全命名空间 B.1. Web 应用安全- <http> 元素 B.1.1. <http> 属性 B.1.1.1. servlet-api-provision B....
1. Spring Security 11个步骤为应用程序添加安全防护 ...自2003年出现的Spring扩展插件Acegi Security发展而来。 目前最新 版本为3.x,已成为Spring的一部分。 为J2EE企业应用程序提供可靠的安全性服务。
Spring Security 中可以使用Acegi-1.x 时代的普通配置方式,也可以使用 从2.0 时代才出现的命名空间配置方式,实际上这两者实现的功能是完 全一致的,只是新的命名空间配置方式可以把原来需要几百行的配置压 缩成短...
Acegi是Spring Framework 下最成熟的安全系统,它提供了强大灵活的企业级安全服务,如: 1 : 完善的认证和授权机制, 2 : Http资源访问控制, 3 : Method调用访问控制, 4 : Access Control List (ACL) 基于对象...
• 与 Spring Security (原 Acegi Security )整合 只需将某个 flow 声明为“ secured ”,即可利用 Spring Security 来确定当前用户是否有权限运 行 flow 、激发事件等等。 • 更简洁的配置 官方的数据说同一个 ...
Spring Security 的前身是 Acegi Security ,是 Spring 项目组中用来提供安全认证服务的框架。 Spring Security 为基于J2EE企业应用软件提供了全面安全服务。特别是使用领先的J2EE解决方案-Spring框架开发的企业...
Ext2.1,Struts1.3,Hibernate3.2,Spring2.5,Acegi 1.06,MySQL 2.功能概述: Blog,RSS订阅,网址导航 3.特色 基于标准的SSH结构,基础Ext的展现界面,基于数据库的acegi安全认证.可随意更改的portal布局,可随意添加的...
Acegi是SpringFramework下最成熟的安全系统,它提供了强大灵活的企业级安全服务,如:完善的认证和授权机制,Http资源访问控制,Method调用访问控制,AccessControlList(ACL)基于对象实例的访问控制,...
对于Grails 3.x,不再需要此插件,而应该声明对核心Grails数据库的依赖: compile 'org.grails:grails-datastore-rest-client' 基本用法 有关API样式的文档,请参阅 主要入口是类。 构造和使用REST“动词”之一。...