Spring Acegi Security实例解析一

         Acegi安全系统,是一个用于Spring Framework的安全框架,能够和目前流行的Web容器无缝集成。它使用了Spring的方式提供了安全和认证安全服务,包括使用Bean Context,拦截器和面向接口的编程方式。因此,Acegi安全系统能够轻松地适用于复杂的安全需求。

        Acegi安全系统包含以下关键的功能组件:

        1. FilterToBeanProxy。Acegi通过实现了Filter接口的FilterToBeanProxy提供一种特殊的使用Servlet Filter的方式,它委托Spring中的Bean -- FilterChainProxy来完成过滤功能,这好处是简化了web.xml的配置,并且充分利用了Spring IOC的优势。FilterChainProxy包含了处理认证过程的filter列表,每个filter都有各自的功能。

        2. Authentication对象,包含了Principal,Credential和Principal的授权信息。同时还可以包含关于发起认证请求的客户的其他信息。

        3. ContextHolder对象,使用ThreadLocal储存Authentication对象的地方。

        4. AuthenticationManager,用于认证ContextHolder中的Authentication对象。

        5. daoAuthenticationProvider。进行简单的基于数据源的身份验证。DaoAuthenticationProvider获取数据源中的账号密码并进行匹配,若成功则在通过用户身份的同时返回一个包含授权信息的Authentication对象,否则身份验证失败,抛出一个AuthenticatiionException。

        6. AccessDecissionManager,用于授权一个特定的操作。

下面是使用Acegi登陆的实例分析:

1. 将以下Jar包导入工程中:acegi-security-0.8.3.jar、commons-logging-1.0.4.jar、javax.servlet.jar、oro-2.0.8.jar和spring-1.2-RC2.jar。并要加入工程的classpath中。

2. web.xml的配置,如下

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	<display-name>myacegi</display-name>
	
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			/WEB-INF/applicationContext-acegi-security.xml
		</param-value>
	</context-param>
	
	<filter>
  		<filter-name>MyAcegiFilter</filter-name>
  		<filter-class>net.sf.acegisecurity.util.FilterToBeanProxy</filter-class>
		<init-param>
    		<param-name>targetBean</param-name>
    		<param-value>filterChainProxy</param-value> 
  		</init-param>
	</filter>
	
	<filter-mapping>
      <filter-name>MyAcegiFilter</filter-name>
      <url-pattern>/*</url-pattern>
    </filter-mapping>
    
    <listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>
</web-app>

3. applicationContext-acegi-security.xml的配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
	
	<bean id="filterChainProxy" class="net.sf.acegisecurity.util.FilterChainProxy">
      <property name="filterInvocationDefinitionSource">
         <value>
		    CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON
		    PATTERN_TYPE_APACHE_ANT
            /**=httpSessionContextIntegrationFilter,authenticationProcessingFilter
         </value>
      </property>
    </bean>
    
   <bean id="httpSessionContextIntegrationFilter" class="net.sf.acegisecurity.context.HttpSessionContextIntegrationFilter">
      <property name="context"><value>net.sf.acegisecurity.context.security.SecureContextImpl</value></property>
   </bean>
   
	<bean id="authenticationDao" class="net.sf.acegisecurity.providers.dao.memory.InMemoryDaoImpl">
	  <property name="userMap">
	    <value>
	      lanp=lanpiao,ROLE_PRESIDENT
	      palmerd=4moreyears,ROLE_PRESIDENT
	      bauerj=ineedsleep,ROLE_FIELD_OPS,ROLE_DIRECTOR
	      myersn=traitor,disabled,ROLE_FIELD_OPS
	    </value>
	  </property>
	</bean>

	<bean id="daoAuthenticationProvider" class="net.sf.acegisecurity.providers.dao.DaoAuthenticationProvider">
  		<property name="authenticationDao">
    		<ref bean="authenticationDao"/>
  		</property>
	</bean>


	<bean id="authenticationManager" class="net.sf.acegisecurity.providers.ProviderManager">
  		<property name="providers">
	    <list>
	      <ref bean="daoAuthenticationProvider"/>
	    </list>
  		</property>
	</bean>
	
	<bean id="authenticationEntryPoint" class="net.sf.acegisecurity.ui.webapp.AuthenticationProcessingFilterEntryPoint">
  		<property name="loginFormUrl">
    		<value>/login.jsp</value>
  		</property>
  		<property name="forceHttps"><value>true</value></property>
	</bean>
	
	<bean id="authenticationProcessingFilter" class="net.sf.acegisecurity.ui.webapp.AuthenticationProcessingFilter">
	  <property name="filterProcessesUrl">
	    <value>/j_acegi_security_check</value>
	  </property>
	  <property name="authenticationFailureUrl">
	    <value>/login.jsp?failed=true</value>
	  </property>
	  <property name="defaultTargetUrl">
	    <value>/jsp/index.jsp</value>
	  </property>
	  <property name="authenticationManager">
	    <ref bean="authenticationManager"/>
	  </property>
	</bean>

</beans>

4. 登陆表单如下:

<form method="POST" action="j_acegi_security_check">
  		Name: <input width="100" type="text" name="j_username"><br>
  		Password: <input width="100" type="password" name="j_password"><br>
  		<input type="submit">
	</form>



 


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值