<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    人在江湖

      BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
      82 Posts :: 10 Stories :: 169 Comments :: 0 Trackbacks

    接上一篇 總結Spring Security之 關于Authentication

    * 關于授權

    AcessDecisionManager是管授權的。具體授權(authorization)的工作是交給一系列Voter來做的。每個Voter都實現AccessDecisionVoter接口的vote方法,返回
    int ACCESS_GRANTED = 1;(投贊成票)

    int ACCESS_ABSTAIN = 0;(投棄權票)

    int ACCESS_DENIED = -1; (投反對票)

    AccessDecisioinManager有三種實現:

    AffirmativeBased -當至少有一個投票者投允許訪問票時允許訪問

    ConsensusBased - 當所有投票者都投允許訪問票時允許訪問

    UnanimousBased - 當沒有投票者投拒絕訪問票時允許訪問

    Spring Security提供了一個實用的voter:

    RoleVoter participates in a vote when the secured resource has a configuration attribute whose name starts with ROLE_.

    *  關于保護web

    Spring Security提供一套filter chain保護web應用

    注意FilterToBeanProxy

    <filter>
      <filter-name>Spring Security Filter Chain Proxy</filter-name>
      <filter-class>org.acegisecurity.util.FilterToBeanProxy</filter-class>
      <init-param>
        <param-name>targetClass</param-name>
        <param-value>org.acegisecurity.util.FilterChainProxy</param-value>
      </init-param>
    </filter>

    跑下題,說FilterToBeanProxy的作用:filter配置在web.xml里,它是不可能有IOC的概念的,服務沒辦法自動注射到filter中,這時候有一個辦法就是使用WebApplicationContextUtils(如WebApplicationContextUtils.getWebApplicationContext(servletContext).getBean("securityManager"))。按照SpringSide的說法,WebApplicationContextUtils適合廚房,衛生間,草坪,屋頂等非常規場所,汗…這么做的一個缺點是,Spring的API還是入侵到你的code里了(向來對Rod Johnson的入侵論不感冒,感覺這純是順手拽過來揍EJB的板磚,不值得深究,pojo粉絲估計要拍我板磚了,俺穿個軟猬甲先。入侵論深入人心之后,大家反而愿意對Spring的偶爾入侵指指點點。另外不喜歡Spring的兩個方面是,從DDD的角度看Spring不natural;contract first我覺得是扯淡的,或許在之后的博客亂噴一下我的看法)解決入侵的方法就是FilterToBeanProxy, 它把真正的工作代理給target class,而spring拿到target class的類名后,就歸它管了。 proxy是個簡單的模式,但用在這里感覺還是挺巧妙的。

    回到正題,filter chain里至少包括四個filter:

    Integration Filter - 拿之前的authentication, 通常是從session里拿。

    AuthenticationProcessing Filter - 處理authentication request, 比如登錄的時候

    Exception Translation Filter - 典型的處理是,把AuthenticationException轉登錄頁,把AccessNeniedException轉403錯誤頁

    Filter Security Interceptor - 它是真正做權限處理的,把AuthenticaionManager 和AccessDecisionManager串起來.所以FilterSecurityInterceptor是重點。

    先看一個配置示例:

       1: <beans:bean id="resourceSecurityInterceptor" class="org.springframework.security.intercept.web.FilterSecurityInterceptor">
       2:     <beans:property name="authenticationManager" ref="authenticationManager"/>
       3:     <beans:property name="accessDecisionManager" ref="accessDecisionManager"/>
       4:     <beans:property name="objectDefinitionSource" ref="secureResourceFilterInvocationDefinitionSource" />
       5:     <beans:property name="observeOncePerRequest" value="false" />
       6:     <custom-filter after="LAST" />
       7: </beans:bean>

    這里objectDefinitionSource屬性來定義受保護的資源,保護web的時候,“資源”代表url. 保護method的時候,資源就代表method, 關于保護method之后再討論?!居腥∩岬匾?spring security學習總結】首先讓我們來認識一下系統為我們提供的 ObjectDefinitionSource接口,objectDefinitionSource屬性正是指向此接口的實現類。其中有個重要方法,ConfigAttributeDefinition getAttributes(Object object)方法用戶獲取保護資源對應的權限信息,該方法返回一個ConfigAttributeDefinition對象,該對象中實際就只有一個List列表,我們可以通過使用 ConfigAttributeDefinition類的構造函數來創建這個List列表,這樣,安全攔截器就通過調用 getAttributes(Object object)方法來獲取ConfigAttributeDefinition對象,并將該對象和當前用戶擁有的Authentication對象傳遞給 accessDecisionManager,accessDecisionManager再將其傳遞給voter們,這些投票者從ConfigAttributeDefinition對象中獲取這個存放了訪問保護資源需要的權限信息的列表,然后遍歷這個列表并與Authentication對象中GrantedAuthority[]數據中的用戶權限信息進行匹配,如果匹配成功,投票者就會投贊成票,否則就投反對票,最后accessDecisionManager來統計這些投票決定用戶是否能訪問該資源?!居腥∩岬匾?spring security學習總結結束】如果你用的是role voter的話,那么返回的ConfigAttributeDefinition其實就是一系列Role_XXX

     

    說到這里小結一下之前說的認證,授權和"保護web”,涉及三個概念,user, role, resource. 認證的過程是鑒定你的身份,并且順便把role也關聯上。user和role是多對多的。 授權是處理role和resource之間的關系的,也是多對多。典型的resource是servlet URL路徑。從實戰的角度講,authentication需要我們自己做的有兩個事情:一是通過實現UserService.loadUserByUsername(String userName)完成認證的本職工作;二是通過實現Users.getAuthorities()把user和role關聯起來。

     

    * 保護方法:

    Spring提供了兩種方式保護方法,一種是AOP,另一種是annotation.

    AOP:

    如果你已經看懂了上面關于objectDefinitionSource的介紹和小結部分,那么直接看配置應該很容易可以看懂:

       1:  
       2: <bean id="autoProxyCreator" 
       3: class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
       4:  
       5: <property name="interceptorNames">
       6:  
       7: <list>
       8:  
       9: <value>securityInterceptor</value>
      10:  
      11: </list>
      12:  </property>
      13:  <property 
      14: name="beanNames">
      15:  
      16: <list>
      17:  
      18: <value>courseService</value>
      19:  
      20: <value>billingService</value>
      21:  
      22: </list>
      23:  </property>
      24: </bean>
      25:  
      26: <bean id="securityInterceptor" 
      27:  
      28: class="org.acegisecurity.intercept.method.MethodSecurityInterceptor">
      29:  
      30: <property name="authenticationManager">
      31:  <ref 
      32: bean="authenticationManager"/>
      33:  </property>
      34:  
      35: <property name="accessDecisionManager">
      36:  <ref 
      37: bean="accessDecisionManager"/>
      38:  </property>
      39:  
      40: <property name="objectDefinitionSource">
      41:  
      42: <value>
      43:  
      44: com.springinaction.springtraining.service.CourseService.createCourse=ROLE_ADMIN
      45:  
      46: com.springinaction.springtraining.service.CourseService.enroll*=ROLE_ADMIN,ROLE_REGISTRAR
      47:  
      48: </value>
      49:  </property>
      50: </bean>

    注意上面的倒數幾行定義了方法和對應的role

    annotation:

    最終目標是通過這種方式定義權限

       1: /**
       2:  *  @@org.acegisecurity.SecurityConfig("ROLE_ADMIN")
       3:  *  @@org.acegisecurity.SecurityConfig("ROLE_REGISTRAR")
       4:  */
       5: public void enrollStudentInCourse(Course course, Student student) throws CourseException;

    這個看起來很酷,但是有個drawback是,如果你做的是產品,并且允許用戶靈活配置role和method(capability)的功能,那么annotation就不適用了,因為annotation是寫死在code里的,compile time已經把role和method之間的map寫死了。

    這個沒啥理論邏輯可談,直接貼spring in action的配置:

       1:  
       2: <bean 
       3: id="attributes"class="org.springframework.metadata.commons.CommonsAttributes"/>
       4:  
       5: <bean id="objectDefinitionSource" 
       6: class="org.acegisecurity.intercept.method.MethodDefinitionAttributes">
       7:  
       8: <property name="attributes"><ref 
       9: bean="attributes"/></property>
      10: </bean>
      11:  
      12: <bean id="securityInterceptor" 
      13:  
      14: class="org.acegisecurity.intercept.method.MethodSecurityInterceptor">
      15:
      16:  
      17: <property name="objectDefinitionSource">
      18:  <ref 
      19: bean="objectDefinitionSource"/>
      20:  
      21: </property>
      22: </bean>
    posted on 2011-03-14 08:41 人在江湖 閱讀(4420) 評論(1)  編輯  收藏 所屬分類: spring

    Feedback

    # re: 總結Spring Security之 關于授權,保護web和保護方法 2015-03-09 10:49 jirly
    nothing to say   回復  更多評論
      

    主站蜘蛛池模板: 亚洲乱妇熟女爽到高潮的片| 国产精品亚洲精品爽爽| 成全视频免费高清 | 亚洲一区二区三区乱码A| 久久青草免费91线频观看不卡| 亚洲宅男精品一区在线观看| 亚洲精品NV久久久久久久久久| 久久久久久国产精品免费免费男同 | 色偷偷亚洲男人天堂| 亚洲AV无码1区2区久久| 日本一道综合久久aⅴ免费| a在线观看免费网址大全| 亚洲日本成本人观看| 亚洲国产精品久久久久网站| 国产极品粉嫩泬免费观看| 99久在线国内在线播放免费观看| 性色av极品无码专区亚洲| 亚洲国产一区在线| 亚洲&#228;v永久无码精品天堂久久 | 国产精品亚洲精品久久精品| 亚洲国产精品一区| 亚洲精品视频久久久| 一本无码人妻在中文字幕免费| a级片免费在线播放| 鲁死你资源站亚洲av| 亚洲色欲www综合网| 亚洲色欲久久久综合网东京热| 国内自产拍自a免费毛片| 久久ww精品w免费人成| 一级毛片a女人刺激视频免费| 99久久国产亚洲综合精品| 亚洲成人动漫在线| 国产亚洲精品资在线| 免费h成人黄漫画嘿咻破解版| 99久久这里只精品国产免费| 免费人成在线观看网站| 一级毛片大全免费播放下载| 亚洲AV日韩AV一区二区三曲| 精品久久久久久亚洲精品| 久久久久亚洲AV无码麻豆| 亚洲精品乱码久久久久久中文字幕 |