acegi2.0的学习——Spring Security

    今天看到appfuse2.0.2里acegi的配置文件非常奇怪,到appfuse官网查看原来将appfuse2.0.2包含的acegi1.0升级到了acegi2.0 ,同1.0比起变化很大,名称也改为spring security2.0.下面是简单配置使用步骤

 

 

   1、第一件事是把下面的filter声明添加到 web.xml 文件中:

Xml代码   收藏代码
  1. <filter>   
  2. <filter-name>springSecurityFilterChain</filter-name>   
  3. <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>   
  4. </filter>   
  5.   
  6. <filter-mapping>   
  7. <filter-name>springSecurityFilterChain</filter-name>   
  8. <url-pattern>/*</url-pattern>   
  9. </filter-mapping>   

 

    2.配置信息都放到application-security.xml里(名称可改),要加上命名空间

    

Xml代码   收藏代码
  1. <beans:beans xmlns="http://www.springframework.org/schema/security"  
  2.   xmlns:beans="http://www.springframework.org/schema/beans"  
  3.   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd  
  5.               http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.2.xsd">  
  6.     ...  
  7. </beans:beans>  

 3、其次为路径,地址增加权限。

<intercept-url pattern="/**" access="ROLE_USER" />

 这表示,我们要保护应用程序中的所有URL,只有拥有 ROLE_USER角色的用户才能访问。

例如:

Xml代码   收藏代码
  1. <http auto-config="true" lowercase-comparisons="false">  
  2.         <!--intercept-url pattern="/images/*" filters="none"/>  
  3.         <intercept-url pattern="/styles/*" filters="none"/>  
  4.         <intercept-url pattern="/scripts/*" filters="none"/-->  
  5.         <intercept-url pattern="/admin/*" access="ROLE_ADMIN"/>  
  6.         <intercept-url pattern="/passwordHint.html*" access="ROLE_ANONYMOUS,ROLE_ADMIN,ROLE_USER"/>  
  7.         <intercept-url pattern="/signup.html*" access="ROLE_ANONYMOUS,ROLE_ADMIN,ROLE_USER"/>  
  8.         <intercept-url pattern="/a4j.res/*.html*" access="ROLE_ANONYMOUS,ROLE_ADMIN,ROLE_USER"/>  
  9.         <!-- APF-737, OK to remove line below if you're not using JSF -->  
  10.         <intercept-url pattern="/**/*.html*" access="ROLE_ADMIN,ROLE_USER"/>  
  11.         <form-login login-page="/login.jsp" authentication-failure-url="/login.jsp?error=true" login-processing-url="/j_security_check"/>  
  12.         <remember-me user-service-ref="userDao" key="e37f4b31-0c45-11dd-bd0b-0800200c9a66"/>  
  13.     </http>  

  

4、  指定数据库 和 密码加密

Xml代码   收藏代码
  1. <authentication-provider user-service-ref="userDao">  
  2.       <password-encoder ref="passwordEncoder"/>  
  3.   </authentication-provider>  

 5、方法授权

Xml代码   收藏代码
  1. <global-method-security>  
  2.         <protect-pointcut expression="execution(* *..service.UserManager.getUsers(..))" access="ROLE_ADMIN"/>  
  3.         <protect-pointcut expression="execution(* *..service.UserManager.removeUser(..))" access="ROLE_ADMIN"/>  
  4.     </global-method-security>  

 

6、Session控制
Acegi2.0 的session 控制真是简单,只能用简单得一米来形容J
首先在web.xml 中加个Listener:

 

Xml代码   收藏代码
  1. <listener>  
  2. <listener-class>  
  3. org.springframework.security.ui.session.HttpSessionEventPublisher  
  4. </listener-class>  
  5. </listener>  

 

 然后在application-security.xml 中配置规则:

 

Xml代码   收藏代码
  1. <http>  
  2. <concurrent-session-control max-sessions="1" />  
  3. </http>  
  4.   
  5.  或  
  6.   
  7. <http>  
  8. <concurrent-session-control max-sessions="1" exception-if-maximum-exceeded="true" />  
  9. </http>  

  
解释说明:两个配置都只允许用户登录一次,exception-if-maximum-exceeded 默认为false,此值表示:
用户第二次登录时,前一次的登录信息都被清空。当exception-if-maximum-exceeded="true"时系统会拒
绝第二次登录。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值