<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 人在江湖 閱讀(4412) 評論(1)  編輯  收藏 所屬分類: spring

    Feedback

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

    主站蜘蛛池模板: 在人线av无码免费高潮喷水| 黄网站色在线视频免费观看| 国产高清不卡免费视频| 最近中文字幕mv免费高清在线| 日本成年免费网站| 免费永久在线观看黄网站| 亚洲午夜国产精品无码老牛影视| 亚洲国产精久久久久久久| 亚洲免费人成视频观看| 亚洲AV综合永久无码精品天堂| 久久久WWW免费人成精品| 最近2019年免费中文字幕高清| 午夜免费福利影院| 国产日产亚洲系列最新| 亚洲高清日韩精品第一区| 亚洲国产精品无码久久| 最近国语视频在线观看免费播放 | 中文字幕乱码亚洲精品一区| 污污免费在线观看| 免费无码中文字幕A级毛片| 色www永久免费视频| 亚洲AV无码一区二区乱孑伦AS| 国产成人亚洲精品| a级毛片免费观看网站| 91精品国产免费| 免费a级毛片大学生免费观看| 亚洲成av人影院| 亚洲精品无AMM毛片| 三年片在线观看免费西瓜视频| 中文字幕无码不卡免费视频| 亚洲黄黄黄网站在线观看| 亚洲天堂一区二区三区四区| 免费大片av手机看片高清| 51在线视频免费观看视频| 午夜国产大片免费观看| 亚洲视频在线观看免费| 黄床大片30分钟免费看| 最近免费mv在线电影| 亚洲国产精品狼友中文久久久| 亚洲麻豆精品果冻传媒| 国产精品无码永久免费888|