Apache Shiro 使用手冊(五) Shiro 配置說明
Apache Shiro的配置主要分為四部分:
對象和屬性的定義與配置
URL的過濾器配置
靜態(tài)用戶配置
靜態(tài)角色配置
其中,由于用戶、角色一般由后臺進(jìn)行操作的動態(tài)數(shù)據(jù),因此Shiro配置一般僅包含前兩項(xiàng)的配置。
Apache Shiro的大多數(shù)組件是基于POJO的,因此我們可以使用POJO兼容的任何配置機(jī)制進(jìn)行配置,例如:Java代碼、Sping XML、YAML、JSON、ini文件等等。下面,以Spring XML的配置方式為例,并且對其中的一些配置參數(shù)進(jìn)行一些簡單說明。
Shiro對象的配置:
主要是對Shiro各個組件的實(shí)現(xiàn)進(jìn)行定義配置,主要組件在前文已做過簡單介紹,這里不再一一說明。
<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過濾來進(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過濾器配置說明:
Shiro可以通過配置文件實(shí)現(xiàn)基于URL的授權(quán)驗(yàn)證。FilterChain定義格式:
URL_Ant_Path_Expression = Path_Specific_Filter_Chain
每個URL配置,表示匹配該URL的應(yīng)用程序請求將由對應(yīng)的過濾器進(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á)式說明
1、URL目錄是基于HttpServletRequest.getContextPath()此目錄設(shè)置
2、URL可使用通配符,**代表任意子目錄
3、Shiro驗(yàn)證URL時,URL匹配成功便不再繼續(xù)匹配查找。所以要注意配置文件中的URL順序,尤其在使用通配符時。
Filter Chain定義說明
1、一個URL可以配置多個Filter,使用逗號分隔
2、當(dāng)設(shè)置多個過濾器時,全部驗(yàn)證通過,才視為通過
3、部分過濾器可指定參數(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 centos7 下安裝 phpMyAdmin的教程
這篇文章主要介紹了Linux centos7 安裝 phpMyAdmin的教程,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2020-01-01Apache NameVirtualHost *:80 has no VirtualHosts問題解決辦法
這篇文章主要介紹了Apache NameVirtualHost *:80 has no VirtualHosts問題解決辦法,一個很簡單的配置性錯誤,需要的朋友可以參考下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