Apache Shiro 使用手冊(cè)(五) Shiro 配置說(shuō)明
Apache Shiro的配置主要分為四部分:
對(duì)象和屬性的定義與配置
URL的過(guò)濾器配置
靜態(tài)用戶配置
靜態(tài)角色配置
其中,由于用戶、角色一般由后臺(tái)進(jìn)行操作的動(dòng)態(tài)數(shù)據(jù),因此Shiro配置一般僅包含前兩項(xiàng)的配置。
Apache Shiro的大多數(shù)組件是基于POJO的,因此我們可以使用POJO兼容的任何配置機(jī)制進(jìn)行配置,例如:Java代碼、Sping XML、YAML、JSON、ini文件等等。下面,以Spring XML的配置方式為例,并且對(duì)其中的一些配置參數(shù)進(jìn)行一些簡(jiǎn)單說(shuō)明。
Shiro對(duì)象的配置:
主要是對(duì)Shiro各個(gè)組件的實(shí)現(xiàn)進(jìn)行定義配置,主要組件在前文已做過(guò)簡(jiǎn)單介紹,這里不再一一說(shuō)明。
<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過(guò)濾器的配置
Shiro主要是通過(guò)URL過(guò)濾來(lái)進(jìn)行安全管理,這里的配置便是指定具體授權(quán)規(guī)則定義。
<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過(guò)濾器配置說(shuō)明:
Shiro可以通過(guò)配置文件實(shí)現(xiàn)基于URL的授權(quán)驗(yàn)證。FilterChain定義格式:
URL_Ant_Path_Expression = Path_Specific_Filter_Chain
每個(gè)URL配置,表示匹配該URL的應(yīng)用程序請(qǐng)求將由對(duì)應(yīng)的過(guò)濾器進(jìn)行驗(yàn)證。
例如:
[urls]
/index.html = anon
/user/create = anon
/user/** = authc
/admin/** = authc, roles[administrator]
/rest/** = authc, rest
/remoting/rpc/** = authc, perms["remote:invoke"]
URL表達(dá)式說(shuō)明
1、URL目錄是基于HttpServletRequest.getContextPath()此目錄設(shè)置
2、URL可使用通配符,**代表任意子目錄
3、Shiro驗(yàn)證URL時(shí),URL匹配成功便不再繼續(xù)匹配查找。所以要注意配置文件中的URL順序,尤其在使用通配符時(shí)。
Filter Chain定義說(shuō)明
1、一個(gè)URL可以配置多個(gè)Filter,使用逗號(hào)分隔
2、當(dāng)設(shè)置多個(gè)過(guò)濾器時(shí),全部驗(yàn)證通過(guò),才視為通過(guò)
3、部分過(guò)濾器可指定參數(shù),如perms,roles
Shiro內(nèi)置的FilterChain
Filter Name | Class |
anon | org.apache.shiro.web.filter.authc.AnonymousFilter |
authc | org.apache.shiro.web.filter.authc.FormAuthenticationFilter |
authcBasic | org.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter |
perms | org.apache.shiro.web.filter.authz.PermissionsAuthorizationFilter |
port | org.apache.shiro.web.filter.authz.PortFilter |
rest | org.apache.shiro.web.filter.authz.HttpMethodPermissionFilter |
roles | org.apache.shiro.web.filter.authz.RolesAuthorizationFilter |
ssl | org.apache.shiro.web.filter.authz.SslFilter |
user | org.apache.shiro.web.filter.authc.UserFilter |
相關(guān)文章
基于Linux下Nagios的安裝與配置說(shuō)明介紹[圖]
本篇文章小編為大家介紹,基于Linux下Nagios的安裝與配置說(shuō)明介紹[圖]。需要的朋友參考下2013-04-04Ubuntu 安裝Eclipse 菜單無(wú)法使用解決辦法
這篇文章主要介紹了Ubuntu 安裝Eclipse 菜單無(wú)法使用解決辦法的相關(guān)資料,需要的朋友可以參考下2017-03-03Linux centos7 下安裝 phpMyAdmin的教程
這篇文章主要介紹了Linux centos7 安裝 phpMyAdmin的教程,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-01-01Apache NameVirtualHost *:80 has no VirtualHosts問(wèn)題解決辦法
這篇文章主要介紹了Apache NameVirtualHost *:80 has no VirtualHosts問(wèn)題解決辦法,一個(gè)很簡(jiǎn)單的配置性錯(cuò)誤,需要的朋友可以參考下2014-08-08bt寶塔面板php7.3、php7.4不支持ZipArchive解決方法
這篇文章主要介紹了bt寶塔面板php7.3、php7.4不支持ZipArchive解決方法,需要的朋友可以參考下2020-06-06Apache?Kafka?分區(qū)重分配的實(shí)現(xiàn)原理解析
這篇文章主要介紹了Apache?Kafka?分區(qū)重分配的實(shí)現(xiàn)原理,本文結(jié)合?2.0.0?版本的?Kafka?源碼,詳細(xì)介紹了?Kafka?分區(qū)副本重分配的流程和邏輯,需要的朋友可以參考下2022-07-07