Apache Shiro 使用手冊(五) Shiro 配置說明
Apache Shiro的配置主要分為四部分:
對象和屬性的定義與配置
URL的過濾器配置
靜態(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的配置方式為例,并且對其中的一些配置參數(shù)進(jìn)行一些簡單說明。
Shiro對象的配置:
主要是對Shiro各個(gè)組件的實(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
每個(gè)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時(shí),URL匹配成功便不再繼續(xù)匹配查找。所以要注意配置文件中的URL順序,尤其在使用通配符時(shí)。
Filter Chain定義說明
1、一個(gè)URL可以配置多個(gè)Filter,使用逗號(hào)分隔
2、當(dāng)設(shè)置多個(gè)過濾器時(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系統(tǒng)下安裝visdom包出現(xiàn)報(bào)錯(cuò)問題及解決
這篇文章主要介紹了linux系統(tǒng)下安裝visdom包出現(xiàn)報(bào)錯(cuò)問題及解決,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06
Linux服務(wù)器系統(tǒng)時(shí)鐘和硬件時(shí)鐘
這篇文章主要介紹了Linux服務(wù)器系統(tǒng)時(shí)鐘和硬件時(shí)鐘,需要的朋友可以參考下2023-10-10

