Apache Shiro的配置主要分為四部分:
對象和屬性的定義與配置
URL的過濾器配置
靜態用戶配置
靜態角色配置
其中,由于用戶、角色一般由后臺進行操作的動態數據,因此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 'realms' 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的應用程序請求將由對應的過濾器進行驗證。
|
新聞熱點
疑難解答