Apache Shiro 使用手冊(五)Shiro 配置說明

Apache Shiro的配置主要分爲四部分: 
html

  1. 對象和屬性的定義與配置web

  2. URL的過濾器配置spring

  3. 靜態用戶配置apache

  4. 靜態角色配置數組

其中,因爲用戶、角色通常由後臺進行操做的動態數據,所以Shiro配置通常僅包含前兩項的配置。安全

Apache Shiro的大多數組件是基於POJO的,所以咱們可使用POJO兼容的任何配置機制進行配置,例如:Java代碼、Sping XML、YAML、JSON、ini文件等等。下面,以Spring XML的配置方式爲例,而且對其中的一些配置參數進行一些簡單說明。 session

Shiro對象的配置: 
主要是對Shiro各個組件的實現進行定義配置,主要組件在前文已作過簡單介紹,這裏再也不一一說明。 app

<bean id="securityManager" class="org.apache.shiro.mgt.DefaultSecurityManager">
        <property name="cacheManager" ref="cacheManager"/>
        <property name="sessionMode" value="native"/>
        <!-- Single realm app.  If you have multiple realms, use the 'realms' property instead. -->
        <property name="realm" ref="myRealm"/>
        <property name="sessionManager" ref="sessionManager"/> 
</bean>

Shiro過濾器的配置 
Shiro主要是經過URL過濾來進行安全管理,這裏的配置即是指定具體受權規則定義。jsp

<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
    <property name="securityManager" ref="securityManager"/>
    <property name="loginUrl" value="/login.jsp"/>
    <property name="successUrl" value="/home.jsp"/>
    <property name="unauthorizedUrl" value="/unauthorized.jsp"/> -->
    <property name="filterChainDefinitions">
        <value>
            # some example chain definitions:
            /admin/** = authc, roles[admin]
            /docs/** = authc, perms[document:read]
            /** = authc
            # more URL-to-FilterChain definitions here
        </value>
    </property>
</bean>

URL過濾器配置說明: 
Shiro能夠經過配置文件實現基於URL的受權驗證。FilterChain定義格式: 
URL_Ant_Path_Expression = Path_Specific_Filter_Chain 
每一個URL配置,表示匹配該URL的應用程序請求將由對應的過濾器進行驗證。 
例如: url

[urls] 
/index.html = anon 
/user/create = anon 
/user/** = authc 
/admin/** = authc, roles[administrator] 
/rest/** = authc, rest 
/remoting/rpc/** = authc, perms["remote:invoke"] 

URL表達式說明 
一、URL目錄是基於HttpServletRequest.getContextPath()此目錄設置 
二、URL可以使用通配符,**表明任意子目錄 
三、Shiro驗證URL時,URL匹配成功便再也不繼續匹配查找。因此要注意配置文件中的URL順序,尤爲在使用通配符時。 

Filter Chain定義說明 
一、一個URL能夠配置多個Filter,使用逗號分隔 
二、當設置多個過濾器時,所有驗證經過,才視爲經過 
三、部分過濾器可指定參數,如perms,roles 

Shiro內置的FilterChain 

相關文章
相關標籤/搜索