首頁  >  文章  >  後端開發  >  Apache Shiro 使用手冊(五)Shiro 設定說明

Apache Shiro 使用手冊(五)Shiro 設定說明

黄舟
黄舟原創
2017-01-18 09:34:151668瀏覽

Apache Shiro 使用手冊(五)Shiro 設定說明

部落格分類: 

開發

安全框架Shiro 

Apache Shiro的設定主要分為四個部分: 配置
靜態使用者配置
靜態角色配置

其中,由於使用者、角色一般由後台進行操作的動態數據,因此Shiro配置一般僅包含前兩項的配置。 

Apache Shiro的大多數元件是基於POJO的,因此我們可以使用POJO相容的任何設定機制進行配置,例如:Java程式碼、Sping XML、YAML、JSON、ini檔案等等。下面,以Spring XML的配置方式為例,並且對其中的一些配置參數進行一些簡單說明。 

Shiro物件的配置: 

主要是針對Shiro各個元件的實作定義配置,主要元件在前文已做過簡單介紹,這裡不再一一說明。 

<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 &#39;realms&#39; property instead. --> 

<property name="realm" ref="myRealm"/> 

<property name="sessionManager" ref="sessionManager"/> 

</bean>

Shiro過濾器的設定 

Shiro主要是透過URL過濾來進行安全管理,這裡的設定便是指定特定授權規則定義。 

<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的應用程式要求將由對應的篩選器進行驗證。

例如: 

[urls] 

/index.html = anon 

/user/create = anon 

/user/** = authc 

/admin/** = authc, roles[administrator] 

/rest/** = authc, rest 

/remoting/rpc/** = authc, perms["remote:invoke"]

URL表達式說明 

1、URL目錄是基於HttpServletRequest.getContextPath()此目錄設定 


2、URL可使用時配符,並驗證代表任意子目錄,URL匹配成功便不再繼續匹配查找。所以要注意設定檔中的URL順序,尤其在使用通配符時。

Filter Chain定義說明 

1、一個URL可以設定多個Filter,使用逗號分隔 

2、當設定多個過濾器時,全部驗證通過,才視為透過 

3、部分過濾器可指定器時,全部驗證通過,才被視為透過 

3、部分過濾器可指定器參數,如perms,roles 

Shiro內建的FilterChain 

Filter Name    Class    

anon    org. .web.filter.authc.FormAuthenticationFilter    

authcBasic    org.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter    

perms    org.apache.shiro.web.filter.authz.PermissionAache. ro.web.filter.authz.PortFilter    

rest org.apache.shiro.web.filter.authz.HttpMethodPermissionFilter    

roles    org.apache.shiro.web.filter.authz.RolesAuthorizationFilter 完成. 

user    org. apache.shiro.web.filter.authc.UserFilter    

以上就是Apache Shiro 使用手冊(五)Shiro 設定說明的內容,更多相關內容請關注PHP中文網(www.php.cn)!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn