在學習和使用shiro中,需要整合shiro框架,然后可以在spring中中使用有三種方法,我用的是注解開發這種方式,但是,我加入注解后發現,沒什么作用,然后想着肯定是沒有注解成功,然后查找資料,發現是沒有開啟spring攔截器,那么怎么開啟呢,如下所示
在spring-mvc.xml中加入以下代碼就可以了(一定要寫在最先加載的xml中,寫在后面加載的xml中也不起作用)
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor" /> <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor"> <property name="securityManager" ref="securityManager" /> </bean>
lifecycleBeanPostProcessor和securityManager是在shiro配置文件中定義好的
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"></bean> <!-- 配額securityManager --> <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> <property name="realm" ref="customRealm" /> <!-- 注入緩存管理器 --> <property name="cacheManager" ref="cacheManager"/> </bean>
一會總結一下springmvc+shiro的整合配置。