shiro是apache開源的安全框架,可方便用來處理用戶信息認證、用戶受權控制、信息加密等功能。web
<filter> <filter-name>shiroFilter</filter-name> <filter-class> org.springframework.web.filter.DelegatingFilterProxy </filter-class> <init-param> <param-name>targetFilterLifecycle</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>shiroFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
工做原理:web.xml中配置DelegatingFilterProxy的Servlet對象,其代理的目標Serlvet實際上是org.apache.shiro.spring.web.ShiroFilterFactoryBean,爲了創建兩個DelegatingFilterProxy和ShiroFilterFactoryBean關係,主要是經過Filter名稱進行綁定的,當客戶端發送請求時,DelegatingFilterProxy進行攔截,而後根據獲取Filter名稱,最後調用IOC容器的getBean的方法獲取和Filter名稱匹配的Bean,因ShiroFilterFactoryBean是FactoryBean因實際上處理客戶端請求的是shiro在IOC容器Filter的實現類spring
<bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager"> <!--Ehcache緩存配置文件爲空則使用默認配置--> <property name="cacheManagerConfigFile" value="classpath:ehcache-shiro.xml"/> </bean>
<bean id="shiroSessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager"> <property name="globalSessionTimeout" value="600000"/> <!-- session 有效時間爲半小時 (毫秒單位)--> <property name="sessionListeners"> <list> <bean class="com.zhiwei.shiro.listener.sessionLisenter"/> </list> </property> </bean>
<bean id="myRealm" class="com.zhiwei.shiro.realm.MyRealm" init-method="setCredentialMatcher"/>
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> <property name="cacheManager" ref="cacheManager"/> <property name="sessionManager" ref="shiroSessionManager"/> <property name="realm" ref="myRealm"/> <!-- AuthenticationListener在認證器裏面維護 --> <property name="authenticator.authenticationListeners"> <set> <bean class="com.zhiwei.shiro.listener.DefineAuthenticationListener"/> </set> </property> </bean>
注意過濾器的工做順序:shiro採起第1次匹配優先,第一次匹配後後面的過濾器鏈不會匹配,順序不當可能出現"302 not found"錯誤apache
經常使用過濾器通俗解釋:(過濾路徑支持ant風格)緩存
千萬注意:名稱必須與web.xml配置的Filter名稱一致,不然IOC找不到對應的Bean出現異常安全
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> <property name="securityManager" ref="securityManager"/> <property name="loginUrl" value="/toLoginPage"/> <property name="successUrl" value="/toSuccessfulPage"/> <property name="unauthorizedUrl" value="/toUnauthorizedPage"/> <property name="filterChainDefinitions"> <value> /shiroHandler/shiro-logout = logout /shiroHandler/shiro-login = anon /toUserPage = authc,roles[user] /toAdminPage = authc,roles[admin] /** = authc </value> </property> </bean>
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>
補充: 配置文件AuthenticationListener/SessionListener配置session
AuthenticationListener:認證監聽器:會負責監聽整個shiro認證過程的狀況,對於一些項目想統計用戶登錄的請求,可使用該接口,該接口在AuthenticatingSecurityManager的Authenticator中的authenticationListeners維護,DefaultWebSecurityManager因繼承了AuthenticatingSecurityManager:使用級聯屬性配置authenticator.authenticationListeners便可:mvc
AuthenticationListener接口:app
public interface AuthenticationListener { void onSuccess(AuthenticationToken token, AuthenticationInfo info); void onLogout(PrincipalCollection principals); }
spring配置AuthenticationListener:authenticator.authenticationListeners框架
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> <property name="authenticator.authenticationListeners"> <set> <bean class="com.zhiwei.shiro.listener.DefineAuthenticationListener"/> </set> </property> </bean>
SessionListener:會話監聽器:顧名思義就是監聽整個會話的操做過程在SessionManager中的sessionListeners維護:加密
SessionListener 接口:
public interface SessionListener { void onStart(Session session); void onStop(Session session); void onExpiration(Session session); }
spring配置SessionListener :sessionListeners
<bean id="shiroSessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager"> <property name="sessionListeners"> <list> <bean class="com.zhiwei.shiro.listener.sessionLisenter"/> </list> </property> </bean>