---
title: "Acegi源码研究(六): Acegi编码/设计碎得"
author: "rmn190"
date: "2009-02-18T22:28:49Z"
tags: ["Acegi", "Hibernate", "Security", "Web", "框架"]
categories: ["企业架构"]
slug: "acegi-acegi"
draft: false
lastmod: "2009-02-18T22:28:49Z"
id: 335146
url: "https://rmn190.iteye.com/blog/335146"
---

前些天通过一个Acegi的Web实例,我们感受了下受保护的好处,也通过一步步的跟踪,感觉到Acegi里"七剑"的存在.本来是想着继续再往下做扩展的,后来一想还是回过头来整理下研究Acegi过程中的碎得吧,毕竟这样的碎得写起轻松些,我也稍稍放松下.   

 

      信马由缰地溜达了下,又想起了当时一开始看Acegi源码时的如下问题:

 

      1, FilterToBeanProxy --&gt; FilterChainProxy, 一个VirtualFilterChain来模仿Web 容器中的FilterChain, 内容类   (自己写代码时也有意识地用protected, private了).  
              方法的实现与调用 --&gt;　代码块的重用.  
              FilterChainProxy类里filterInvocationDefinitionSource属性从String到相应类的自动转换.

2, Filter调用与方法栈的吻合,画一个序列图来表示.

3, SecurityContextHolder的多种实现, 为什么要有多种实现,每一种实现又有什么特点?  
    HttpSession中放没放SecurityContext? 在SecurityContextHolder里也有放. 这种有问题了: 如何让它们俩同步? 删了一个后又怎样?

4, 另处一些常用的配置:  
     logoutFilter, securityContextHolderAwareRequestFilter, anonymousProcessingFilter

    出于什么考虑提出这样的概念?

5, 这一段的作用:  
      if (request.getAttribute(FILTER\_APPLIED) != null) {  
            // ensure that filter is only applied once per request  
            chain.doFilter(request, response);

            return;  
     }

    要防止在 filterInvocationDefinitionSource中配置了多个httpSessionContextIntegrationFilter, httpSessionContextIntegrationFilter总是在第一个的. 多配置了也没用, 若不加有什么害处不?  
6, 为什么如下这种方式来取HttpSession?  
          try {  
            httpSession = request.getSession(forceEagerSessionCreation);  
        }  
        catch (IllegalStateException ignored) {  
        }  
         若getSesssion时传一个False会是什么样的?一个HttpSession一般什么时机创建? 现在越来越不考虑这些问题了.  
7, 为什么要把response再包装下? OnRedirectUpdateSessionResponseWrapper  
8, 为什么每次都要有 SecurityContextHolder.clearContext();这不挺浪费的吗? 防止Authentication里的用户名/密码/权限在用户操作时有改? 作为一个通用的安全框架应该支持这个功能的, 把权限控制存放到数据库中太常见了.  
9, 如下代码段的的注意点:  
          if (cloneFromHttpSession) {  
            Assert.isInstanceOf(Cloneable.class, contextFromSessionObject,  
                    "Context must implement Clonable and provide a Object.clone() method");  
            try {  
                Method m = contextFromSessionObject.getClass().getMethod("clone", new Class\[]{});  
                if (!m.isAccessible()) {  
                    m.setAccessible(true);  
                }  
                contextFromSessionObject = m.invoke(contextFromSessionObject, new Object\[]{});  
            }  
            catch (Exception ex) {  
                ReflectionUtils.handleReflectionException(ex);  
            }  
        }  
         9.1 为什么要 cloneFromHttpSession? 联想到Hibernate的PersistentContext, 它会自动来做dirty check的, 若不clone一个,直接把一个原来的return给client的话,会影响dirty check的速度.  
         9.2 自己写例子试试clone接口的实现与深度拷贝.  
10, 从方法 readSecurityContextFromSession看框架级代码的书写:  
    10.1 考虑到多种可能层层设防. 从而确保return一个 non-null,且为实现 SecurityContext接口的HttpSession里以" ACEGI\_SECURITY\_CONTEXT\_KEY "存放的对象, 不要被"狸猫换太子"了.  
    10.2, log的书写.