spring acegi2.X

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。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值