Acegi简介2 2008-09-17 14:31

字号:    

 

[简介]

对于一个典型的Web应用,完善的认证和授权机制是必不可少的,在SpringFramework中,Juergen Hoeller提供的范例JPetStore给了一些这方面的介绍,但还远远不够,Acegi是一个专门为SpringFramework提供安全机制的项目,全称为Acegi Security System for Spring,当前版本为0.5.1,就其目前提供的功能,应该可以满足绝大多数应用的需求。

本文的主要目的是希望能够说明如何在基于Spring构架的Web应用中使用Acegi,而不是详细介绍其中的每个接口、每个类。注意,即使对已经存在的Spring应用,通过下面介绍的步骤,也可以马上享受到Acegi提供的认证和授权。

[基础工作]

在你的Web应用的lib中添加Acegi下载包中的acegi-security.jar

[web.xml]

实现认证和授权的最常用的方法是通过filter,Acegi亦是如此,通常Acegi需要在web.xml添加以下5个filter:

<filter>

   <filter-name>Acegi Channel Processing Filter</filter-name>

   <filter-class>net.sf.acegisecurity.util.FilterToBeanProxy</filter-class>

   <init-param>

     <param-name>targetClass</param-name>

     <param-value>net.sf.acegisecurity.securechannel.ChannelProcessingFilter</param-value>

   </init-param>

</filter>

<filter>

   <filter-name>Acegi Authentication Processing Filter</filter-name>

   <filter-class>net.sf.acegisecurity.util.FilterToBeanProxy</filter-class>

   <init-param>

     <param-name>targetClass</param-name>

     <param-value>net.sf.acegisecurity.ui.webapp.AuthenticationProcessingFilter</param-value>

   </init-param>

</filter>

<filter>

   <filter-name>Acegi HTTP BASIC Authorization Filter</filter-name>

   <filter-class>net.sf.acegisecurity.util.FilterToBeanProxy</filter-class>

   <init-param>

     <param-name>targetClass</param-name>

     <param-value>net.sf.acegisecurity.ui.basicauth.BasicProcessingFilter</param-value>

   </init-param>

</filter>

<filter>

   <filter-name>Acegi Security System for Spring Auto Integration Filter</filter-name>

   <filter-class>net.sf.acegisecurity.ui.AutoIntegrationFilter</filter-class>

</filter>

<filter>

   <filter-name>Acegi HTTP Request Security Filter</filter-name>

   <filter-class>net.sf.acegisecurity.util.FilterToBeanProxy</filter-class>

   <init-param>

     <param-name>targetClass</param-name>

     <param-value>net.sf.acegisecurity.intercept.web.SecurityEnforcementFilter</param-value>

   </init-param>

</filter>

最先引起迷惑的是net.sf.acegisecurity.util.FilterToBeanProxy,Acegi自己的文档上解释是: “What   FilterToBeanProxy does is delegate the Filter's methods through to a bean which is obtained from the

Spring application context. This enables the bean to benefit from the Spring application context lifecycle support and configuration flexibility.”,如希望深究的话,去看看源代码应该不难理解。

再下来就是添加filter-mapping了:

<filter-mapping>

   <filter-name>Acegi Channel Processing Filter</filter-name>

   <url-pattern>/*</url-pattern>

</filter-mapping>

<filter-mapping>

   <filter-name>Acegi Authentication Processing Filter</filter-name>

   <url-pattern>/*</url-pattern>

</filter-mapping>

<filter-mapping>

   <filter-name>Acegi HTTP BASIC Authorization Filter</filter-name>

   <url-pattern>/*</url-pattern>

</filter-mapping>

<filter-mapping>

   <filter-name>Acegi Security System for Spring Auto Integration Filter</filter-name>

   <url-pattern>/*</url-pattern>

</filter-mapping>

<filter-mapping>

   <filter-name>Acegi HTTP Request Security Filter</filter-name>

   <url-pattern>/*</url-pattern>

</filter-mapping>

这里,需要注意以下两点:

1) 这几个filter的顺序是不能更改的,顺序不对将无法正常工作;

2) 如果你的应用不需要安全传输,如https,则将"Acegi Channel Processing Filter"相关内容注释掉即可;

3) 如果你的应用不需要Spring提供的远程访问机制,如Hessian and Burlap,将"Acegi HTTP BASIC Authorization

Filter"相关内容注释掉即可。

[applicationContext.xml]

接下来就是要添加applicationContext.xml中的内容了,从刚才FilterToBeanFactory的解释可以看出,真正的filter都

在Spring的applicationContext中管理:

1) 首先,你的数据库中必须具有保存用户名和密码的table,Acegi要求table的schema必须如下:

CREATE TABLE users (

     username VARCHAR(50) NOT NULL PRIMARY KEY,

     password VARCHAR(50) NOT NULL,

     enabled BIT NOT NULL

);

CREATE TABLE authorities (

     username VARCHAR(50) NOT NULL,

     authority VARCHAR(50) NOT NULL

);

CREATE UNIQUE INDEX ix_auth_username ON authorities ( username, authority );

ALTER TABLE authorities ADD CONSTRAINT fk_authorities_users foreign key (username) REFERENCES users

(username);

2) 添加访问你的数据库的datasource和Acegi的jdbcDao,如下:

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">

   <property name="driverClassName"><value>${jdbc.driverClassName}</value></property>

   <property name="url"><value>${jdbc.url}</value></property>

   <property name="username"><value>${jdbc.username}</value></property>

   <property name="password"><value>${jdbc.password}</value></property>

</bean>

<bean id="jdbcDaoImpl" class="net.sf.acegisecurity.providers.dao.jdbc.JdbcDaoImpl">

   <property name="dataSource"><ref bean="dataSource"/></property>

</bean>

3) 添加DaoAuthenticationProvider:

<bean id="daoAuthenticationProvider" class="net.sf.acegisecurity.providers.dao.DaoAuthenticationProvider">

   <property name="authenticationDao"><ref bean="authenticationDao"/></property>

   <property name="userCache"><ref bean="userCache"/></property>

</bean>

<bean id="userCache" class="net.sf.acegisecurity.providers.dao.cache.EhCacheBasedUserCache">

   <property name="minutesToIdle"><value>5</value></property>

</bean>

如果你需要对密码加密,则在daoAuthenticationProvider中加入:<property name="passwordEncoder"><ref

bean="passwordEncoder"/></property>,Acegi提供了几种加密方法,详细情况可看包

net.sf.acegisecurity.providers.encoding

4) 添加authenticationManager:

<bean id="authenticationManager" class="net.sf.acegisecurity.providers.ProviderManager">

   <property name="providers">

     <list>

       <ref bean="daoAuthenticationProvider"/>

     </list>

    </property>

</bean>

5) 添加accessDecisionManager:

<bean id="accessDecisionManager" class="net.sf.acegisecurity.vote.AffirmativeBased">

   <property name="allowIfAllAbstainDecisions">

     <value>false</value>

   </property>

   <property name="decisionVoters">

     <list><ref bean="roleVoter"/></list>

   </property>

</bean>

<bean id="roleVoter" class="net.sf.acegisecurity.vote.RoleVoter"/>

6) 添加authenticationProcessingFilterEntryPoint:

<bean id="authenticationProcessingFilterEntryPoint"

class="net.sf.acegisecurity.ui.webapp.AuthenticationProcessingFilterEntryPoint">

   <property name="loginFormUrl"><value>/acegilogin.jsp</value></property>

   <property name="forceHttps"><value>false</value></property>

</bean>

其中acegilogin.jsp是登陆页面,一个最简单的登录页面如下:

<%@ taglib prefix='c' uri='http://java.sun.com/jstl/core' %>

<%@ page import="net.sf.acegisecurity.ui.AbstractProcessingFilter" %>

<%@ page import="net.sf.acegisecurity.AuthenticationException" %>

<html>

   <head>

     <title>Login</title>

   </head>

   <body>

     <h1>Login</h1>

     <form action="<c:url value='j_acegi_security_check'/>" method="POST">

       <table>

         <tr><td>User:</td><td><input type='text' name='j_username'></td></tr>

         <tr><td>Password:</td><td><input type='password' name='j_password'></td></tr>

         <tr><td colspan='2'><input name="submit" type="submit"></td></tr>

         <tr><td colspan='2'><input name="reset" type="reset"></td></tr>

       </table>

     </form>

   </body>

</html>

7) 添加filterInvocationInterceptor:

<bean id="filterInvocationInterceptor"

class="net.sf.acegisecurity.intercept.web.FilterSecurityInterceptor">

   <property name="authenticationManager">

     <ref bean="authenticationManager"/>

   </property>

   <property name="accessDecisionManager">

     <ref bean="accessDecisionManager"/>

   </property>

   <property name="objectDefinitionSource">

     <value>

       CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON

       \A/sec/administrator.*\Z=ROLE_SUPERVISOR

       \A/sec/user.*\Z=ROLE_TELLER

     </value>

   </property>

</bean>

这里请注意,要objectDefinitionSource中定义哪些页面需要权限访问,需要根据自己的应用需求进行修改,我上面给出

的定义的意思是这样的:

a. CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON意思是在比较请求路径时全部转换为小写

b. \A/sec/administrator.*\Z=ROLE_SUPERVISOR意思是只有权限为ROLE_SUPERVISOR才能访问/sec/administrator*的页面

c. \A/sec/user.*\Z=ROLE_TELLER意思是只有权限为ROLE_TELLER的用户才能访问/sec/user*的页面

8) 添加securityEnforcementFilter:

<bean id="securityEnforcementFilter" class="net.sf.acegisecurity.intercept.web.SecurityEnforcementFilter">

   <property name="filterSecurityInterceptor">

     <ref bean="filterInvocationInterceptor"/>

   </property>

   <property name="authenticationEntryPoint">

     <ref bean="authenticationProcessingFilterEntryPoint"/>

   </property>

</bean>

9) 添加authenticationProcessingFilter:

<bean id="authenticationProcessingFilter"

class="net.sf.acegisecurity.ui.webapp.AuthenticationProcessingFilter">

   <property name="authenticationManager">

     <ref bean="authenticationManager"/>

   </property>

   <property name="authenticationFailureUrl">

     <value>/loginerror.jsp</value>

   </property>

   <property name="defaultTargetUrl">

     <value>/</value>

   </property>

   <property name="filterProcessesUrl">

     <value>/j_acegi_security_check</value>

   </property>

</bean>

其中authenticationFailureUrl是认证失败的页面。

10) 如果需要一些页面通过安全通道的话,添加下面的配置:

<bean id="channelProcessingFilter" class="net.sf.acegisecurity.securechannel.ChannelProcessingFilter">

   <property name="channelDecisionManager">

     <ref bean="channelDecisionManager"/>

   </property>

   <property name="filterInvocationDefinitionSource">

     <value>

       CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON

       \A/sec/administrator.*\Z=REQUIRES_SECURE_CHANNEL

       \A/acegilogin.jsp.*\Z=REQUIRES_SECURE_CHANNEL

       \A/j_acegi_security_check.*\Z=REQUIRES_SECURE_CHANNEL

       \A.*\Z=REQUIRES_INSECURE_CHANNEL

     </value>

   </property>

</bean>

<bean id="channelDecisionManager" class="net.sf.acegisecurity.securechannel.ChannelDecisionManagerImpl">

   <property name="channelProcessors">

     <list>

       <ref bean="secureChannelProcessor"/>

       <ref bean="insecureChannelProcessor"/>

     </list>

   </property>

</bean>

<bean id="secureChannelProcessor" class="net.sf.acegisecurity.securechannel.SecureChannelProcessor"/>

<bean id="insecureChannelProcessor" class="net.sf.acegisecurity.securechannel.InsecureChannelProcessor"/>

[缺少了什么?]

Acegi目前提供了两种"secure object",分别对页面和方法进行安全认证管理,我这里介绍的只是利用

FilterSecurityInterceptor对访问页面的权限控制,除此之外,Acegi还提供了另外一个Interceptor――

MethodSecurityInterceptor,它结合runAsManager可实现对对象中的方法的权限控制,使用方法可参看Acegi自带的文档

和contact范例。

[最后要说的]

本来以为只是说明如何使用Acegi而已,应该非常简单,但真正写起来才发现想要条理清楚的理顺所有需要的bean还是很

困难的,但愿我没有遗漏太多东西,如果我的文章有什么遗漏或错误的话,还请参看Acegi自带的quick-start范例,但请

注意,这个范例是不能直接拿来用的。

分析和学习Spring中的jpetstore用户管理

   存在用户的系统,必然需要用户的登录和认证,今天就通过分析Spring中自带的jpetstore的例子来学习一下如何实现在Spring构架的系统中用户登录。

1、首先从注册用户开始,先看看jpetstore-servlet.xml中关于注册用户的bean定义,从定义命名中就可以看出下面这段就是注册用户的:

  

<bean name="/shop/newAccount.do" class="org.springframework.samples.jpetstore.web.spring.AccountFormController">

     <property name="petStore"><ref bean="petStore"/></property>

     <property name="validator"><ref bean="accountValidator"/></property>

     <property name="successView"><value>index</value></property>

   </bean>

1). formView呢?从AccountFormController的构造函数中得到,原来为EditAccountForm;  

2). EditoAccountForm.jsp中显得非常乱,其实没有多少难理解的地方,最主要的是这个form既是添加新用户的,又是编辑用户信息的,所以显得有点乱糟糟的。

2、添加好了新用户,接下来看看如何登录,在jpetstore-servlet中发现这两个相关bean定义,如下:

  

<bean name="/shop/signon.do" class="org.springframework.samples.jpetstore.web.spring.SignonController">

     <property name="petStore"><ref bean="petStore"/></property>

   </bean>

   <bean name="/shop/signonForm.do" class="org.springframework.web.servlet.mvc.ParameterizableViewController">

     <property name="viewName"><value>SignonForm</value></property>

   </bean>

1). 第二个bean是在运行时用户输入用户名和密码的form,叫做SignonForm,对于这个 ParameterizableViewController,用文档里的话说这是最简单的Controller,其作用就是在运行中指向 Controller而不是直接指向jsp文件,仅此而已。

2). SignonForm.jsp,里面就是一个简单的form,其action就是第一个bean,即/shop/signon.do,最需要注意的是 signonForwardAction,其主要作用是forward到需要输入用户名和密码的那个页面上去,这个变量哪里来的呢?看看下面:

  

<bean id="secureHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">

     <property name="interceptors">

       <list>

         <ref bean="signonInterceptor"/>

       </list>

     </property>

     <property name="urlMap">

       <map>

         <entry key="/shop/editAccount.do"><ref local="secure_editAccount"/></entry>

         <entry key="/shop/listOrders.do"><ref local="secure_listOrders"/></entry>

         <entry key="/shop/newOrder.do"><ref local="secure_newOrder"/></entry>

         <entry key="/shop/viewOrder.do"><ref local="secure_viewOrder"/></entry>

       </map>

     </property>

   </bean>

   原来,上面的signonInterceptor实现了preHandle,因此在请求上面的map页面时,首先要经过这个Interceptor,看看 SignonInterceptor的源码,原来在其中为signon.jsp赋予一个signonForwardAction对象,呵呵,总算明白了。

3). 接下来去学习一下SignonController,其主体部分中可以看出,首先取出用户输入的username和password,然后到数据库中验证有没有这个用户,如果没有这个用户,返回各错误页面;如果成功,首先生成一个UserSession对象,在request的session加入这个 userSession,注意这部分代码中给出了PagedListHolder分页的简单使用方法,关于分页显示,以后再学习吧。

3、登录成功后,就可以根据不同的用户设施不同的行为了,取得用户信息,无非就是从session取出userSession即可。

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
网易公司版权所有 ©1997-2009