1
				
				
						、整合已有的用户表操作
				
				
						
								
								
								
						
				
		
		
				
						
								      
						
				
				a.
				修改Hibernate的映射文件
		
		
				
						     b.
				实现com.toodou.bbs.security.service.acegi.JdbcDaoImpl,来实现自定义的用户认证和授权
		
		
				
						         
				
				对于BBS系统默认的登录用户是USER角色
		
		
				
						     c.
				增加数据库链接的配置,权限用户相关的使用专门的数据库链接
		
		
				
						         
				
				特别要注意一些显示的和潜在的acegiDataSource和acegiSessionFactory的配置
		
		
				
						         
				
				注意DAO中acegiSessionFactory的配置
		
		
				
						2
				
				
						、权限的区分
				
				
						
								
								
						
				
		
		
				
						
								      
						
				
				有些操作发帖、回复等需要登录
		
		
				
						3
				
				
						、方法拦截
				
				
						
								
								
						
				
		
		
				
						
								      
						
				
				<!-- 
				用于安全框架拦截方法,用在事务拦截之前的方式 -->
		
		
				
						        <property name="preInterceptors">
		
		
				
						            <list>
		
		
				
						                <ref bean="methodSecurityInterceptor"/>
		
		
				
						            </list>
		
		
				
						        </property>
		
		
				
						     
				
				同样也可以采用直接使用拦截器的方式:
		
		
				
						     
				
				这对方法的拦截,对于没有权限的时候,会抛出AccessDeniedException的异常,所以在Action中对于有权限要求的方法就要捕获该异常,然后统一处理(比如:跳转到提示页面)
		
		
				
						4
				
				
						、登录后的跳转
				
				
						
								
								
						
				
		
		
				
						
								      
						
				
				通过配置AuthenticationProcessingFilter的属性,其中包括:
		
		
				
						     authenticationFailureUrl    
				定义登陆失败时转向的页面
		
		
				defaultTargetUrl   
				定义登陆成功时转向的页面
		
		
				filterProcessesUrl
				定义登陆请求的页面
		
		
				rememberMeServices
				用于在验证成功后添加cookie信息
		
		
				
						5
				
				
						、登录后自定义权限的分配
				
				
						
								
								
						
				
		
		
				
						
								      
						
				
				需求是默认的没有分配权限的用户的权限是USER.
		
		
				
						     
				
				这方面Acegi已经预留好了接口,只需要继承JdbcDaoImpl类,然后覆盖方法:addCustomAuthorities就可以了。例如:
		
		
				
						     protected void addCustomAuthorities(String username, List authorities) {
		
		
				
						    
						
						
						     if(authorities.size()==0){
		
		
				
						    
						     
						     logger.info("Authority is User");
		
		
				
						    
						     
						     authorities.add(new GrantedAuthorityImpl("USER"));
		
		
				
						    
						
						
						     }
		
		
				}
		
		
				
						6
				
				
						、用户退出
				
				
						
								
								
						
				
		
		
				
						
								      
						
				
				是指用户点击退出按钮,无非做两个动作:session实效和 cookie实效
		
		
				
						     if (session!=null)
		
		
				
						         session.invalidate();
		
		
				
						         Cookie terminate = new 
Cookie(TokenBasedRememberMeServices.ACEGI_SECURITY_HASHED_REMEMBER_ME_COOKIE_KEY, null);
		
		
				
						         terminate.setMaxAge(0);
		
		
				
						         response.addCookie(terminate);
		
		
				
						7
				
				
						、用户信息
				
				
						
								
								
						
				
		
		
				
						     
				
				默认的Acegi登录成功后会在Session中放下面两个内容:
		
		
				
						     
				
				ACEGI_SECURITY_CONTEXT--------org.acegisecurity.context.SecurityContextImpl@fdd91091: Authentication: org.acegisecurity.providers.UsernamePasswordAuthenticationToken@fdd91091: Username: org.acegisecurity.userdetails.User@fdcb3700: Username: test; Password: [PROTECTED]; Enabled: true; AccountNonExpired: true; credentialsNonExpired: true; AccountNonLocked: true; Granted Authorities: USER; Password: [PROTECTED]; Authenticated: true; Details: org.acegisecurity.ui.WebAuthenticationDetails@fffc7f0c: RemoteIpAddress: 127.0.0.1; SessionId: BBBC69F664334F9E4CD0797BC1209DDB; Granted Authorities: USER
				
						
						
				
		
		
				
						 
				
		
		
				ACEGI_SECURITY_LAST_USERNAME--------test
		
		
				<authz:authentication operation="username"/></td>
		
		
				
						 
				
		
		
				
						8
				
				
						、
				
				
						Acegi
				
				
						标签的使用
				
				
						
								
								
						
				
		
		
				<authz:authorize ifAllGranted="ROLE_SUPERVISOR">
		
		
				
						  <td>
		
		
				
						    <A HREF="del.htm?id=123">del</A>
		
		
				
						  </td>
		
		
				</authz:authorize>
		
		
				
						9
				
				
						、对于
				
				
						URL
				
				
						过滤
				
				
						
								
								
						
				
		
		
				
						     
				
				发生AccessDeniedException异常并且是anonymous user的时候跳转到登录页面的设置
		
		
				
						     
				
				配置AuthenticationProcessingFilterEntryPoint的属性loginFormUrl为:/security/login.jsp
		
		
				
						 
				
		
		
				
						     
				
				发生AccessDeniedException异常并且是已登录用户的时候跳转到提示页面的设置
		
		
				     
		
		配置AccessDeniedHandlerImpl的errorPage的属性为:/accessDenied.jsp