Spring Security(08)——intercept-url配置

intercept-url配置

目錄

1.1     指定攔截的url

1.2     指定訪問權限

1.3     指定訪問協議

1.4     指定請求方法

 

1.1    指定攔截的url

       通過pattern指定當前intercept-url定義應當作用於哪些url。

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

 

1.2     指定訪問權限

       可以通過access屬性來指定intercept-url對應URL訪問所應當具有的權限。access的值是一個字符串,其可以直接是一個權限的定義,也可以是一個表達式。常用的類型有簡單的角色名稱定義,多個名稱之間用逗號分隔,如:

<security:intercept-url pattern="/secure/**" access="ROLE_USER,ROLE_ADMIN"/>

       在上述配置中就表示secure路徑下的所有URL請求都應當具有ROLE_USER或ROLE_ADMIN權限。當access的值是以“ROLE_”開頭的則將會交由RoleVoter進行處理。

 

       此外,其還可以是一個表達式,上述配置如果使用表達式來表示的話則應該是如下這個樣子。

   <security:http use-expressions="true">

      <security:form-login />

      <security:logout />

      <security:intercept-url pattern="/secure/**"access="hasAnyRole('ROLE_USER','ROLE_ADMIN')"/>

   </security:http>

       或者是使用hasRole()表達式,然後中間以or連接,如:

   <security:intercept-url pattern="/secure/**" access="hasRole('ROLE_USER') or hasRole('ROLE_ADMIN')"/>

       需要注意的是使用表達式時需要指定http元素的use-expressions=”true”。更多關於使用表達式的內容將在後文介紹。當intercept-url的access屬性使用表達式時默認將使用WebExpressionVoter進行處理。

       此外,還可以指定三個比較特殊的屬性值,默認情況下將使用AuthenticatedVoter來處理它們。IS_AUTHENTICATED_ANONYMOUSLY表示用戶不需要登錄就可以訪問;IS_AUTHENTICATED_REMEMBERED表示用戶需要是通過Remember-Me功能進行自動登錄的才能訪問;IS_AUTHENTICATED_FULLY表示用戶的認證類型應該是除前兩者以外的,也就是用戶需要是通過登錄入口進行登錄認證的才能訪問。如我們通常會將登錄地址設置爲IS_AUTHENTICATED_ANONYMOUSLY。

   <security:http>

      <security:form-login login-page="/login.jsp"/>

      <!-- 登錄頁面可以匿名訪問 -->

      <security:intercept-url pattern="/login.jsp*"access="IS_AUTHENTICATED_ANONYMOUSLY"/>

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

   </security:http>

 

1.3     指定訪問協議

       如果你的應用同時支持Http和Https訪問,且要求某些URL只能通過Https訪問,這個需求可以通過指定intercept-url的requires-channel屬性來指定。requires-channel支持三個值:http、https和any。any表示http和https都可以訪問。

   <security:http auto-config="true">

      <security:form-login/>

      <!-- 只能通過https訪問 -->

      <security:intercept-url pattern="/admin/**" access="ROLE_ADMIN" requires-channel="https"/>

      <!-- 只能通過http訪問 -->

      <security:intercept-url pattern="/**" access="ROLE_USER" requires-channel="http"/>

   </security:http>

 

       需要注意的是當試圖使用http請求限制了只能通過https訪問的資源時會自動跳轉到對應的https通道重新請求。如果所使用的http或者https協議不是監聽在標準的端口上(http默認是80,https默認是443),則需要我們通過port-mapping元素定義好它們的對應關係。

   <security:http auto-config="true">

      <security:form-login/>

      <!-- 只能通過https訪問 -->

      <security:intercept-url pattern="/admin/**" access="ROLE_ADMIN" requires-channel="https"/>

      <!-- 只能通過http訪問 -->

      <security:intercept-url pattern="/**" access="ROLE_USER" requires-channel="http"/>

      <security:port-mappings>

         <security:port-mapping http="8899" https="9988"/>

      </security:port-mappings>

   </security:http>

 

1.4     指定請求方法

       通常我們都會要求某些URL只能通過POST請求,某些URL只能通過GET請求。這些限制Spring Security也已經爲我們實現了,通過指定intercept-url的method屬性可以限制當前intercept-url適用的請求方式,默認爲所有的方式都可以。

   <security:http auto-config="true">

      <security:form-login/>

      <!-- 只能通過POST訪問 -->

      <security:intercept-url pattern="/post/**" method="POST"/>

      <!-- 只能通過GET訪問 -->

      <security:intercept-url pattern="/**" access="ROLE_USER" method="GET"/>

   </security:http>

 

       method的可選值有GET、POST、DELETE、PUT、HEAD、OPTIONS和TRACE。

 

(注:本文是基於Spring Security3.1.6所寫)

(注:原創文章,轉載請註明出處。原文地址:http://haohaoxuexi.iteye.com/blog/2161056

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章