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

目錄express

1.1     指定攔截的urlapp

1.2     指定訪問權限jsp

1.3     指定訪問協議post

1.4     指定請求方法ui

 

1.1    指定攔截的url

       經過pattern指定當前intercept-url定義應看成用於哪些url。url

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

 

1.2     指定訪問權限

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

<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所寫)

相關文章
相關標籤/搜索