ShiroFilter的工作原理
ShiroFilter:DelegatingFilterProxy作用是自動到Spring 容器查找名字為shiroFilter(filter-name)的bean並把所有Filter 的操作委托給它。
問題講解:為什么applicationContext.xml中的id必須和web.xml文件中配置的DelegatingFilterProxy的 <filter-name> 一致?
applicationContext.xml中的id必須和web.xml文件中配置的DelegatingFilterProxy的 <filter-name> 一致
若不一致,則會拋出:NoSuchBeanDefinitionException. 因為 Shiro 會來 IOC 容器中查找和 <filter-name> 名字對應的 filter bean.
<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="/list.jsp"/> <property name="unauthorizedUrl" value="/unauthorized.jsp"/> <!-- 配置哪些頁面需要受保護 以及訪問這些頁面需要的權限 1). anon 可以被匿名訪問 2). authc 必須認證(即登錄)后才可以訪問的頁面 --> <property name="filterChainDefinitions"> <value> /login.jsp = anon # everything else requires authentication: /user.jsp = authc </value> </property> </bean>
web.xml:
<!-- Shiro Filter is defined in the spring application context: --> <!-- 1. 配置 Shiro 的 shiroFilter. 2. DelegatingFilterProxy 實際上是 Filter 的一個代理對象. 默認情況下, Spring 會到 IOC 容器中查找和 <filter-name> 對應的 filter bean. 也可以通過 targetBeanName 的初始化參數來配置 filter bean 的 id. --> <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>
若想修改shiroFilter名稱,即自定義,web.xml需要添加如下內容:
<!-- Shiro Filter is defined in the spring application context: --> <!-- 1. 配置 Shiro 的 shiroFilter. 2. DelegatingFilterProxy 實際上是 Filter 的一個代理對象. 默認情況下, Spring 會到 IOC 容器中查找和 <filter-name> 對應的 filter bean. 也可以通過 targetBeanName 的初始化參數來配置 filter bean 的 id. --> <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> <init-param> <param-name>targetBeanName</param-name> <param-value>abc</param-value> </init-param> </filter> <filter-mapping> <filter-name>shiroFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
applicationContext.xml:
<!-- 6.配置ShiroFilter 6.1 id必須和web.xml文件中配置的DelegatingFilterProxy的 <filter-name> 一致 若不一致,則會拋出:NoSuchBeanDefinitionException. 因為 Shiro 會來 IOC 容器中查找和 <filter-name> 名字對應的 filter bean. --> <bean id="abc" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> <property name="securityManager" ref="securityManager"/> <property name="loginUrl" value="/login.jsp"/> <property name="successUrl" value="/list.jsp"/> <property name="unauthorizedUrl" value="/unauthorized.jsp"/> <!-- 配置哪些頁面需要受保護 以及訪問這些頁面需要的權限 1). anon 可以被匿名訪問 2). authc 必須認證(即登錄)后才可以訪問的頁面 --> <property name="filterChainDefinitions"> <value> /login.jsp = anon # everything else requires authentication: /user.jsp = authc </value> </property> </bean>
這樣,就自定義了名稱(不推薦,第一種默認即可)