在使用Shiro框架進行項目整合時,使用注解在使用Shiro框架進行項目整合時,使用注解在使用Shiro框架進行項目整合時,使用注解@RequiresPermissions為方法提供是需要的權限,但是根本沒有進行驗證,后面發現在自己的Realm中只執行了doGetAuthenticationInfo(登錄驗證)方法而沒有執行doGetAuthorizationInfo(權限驗證)的方法。
查看相關資料后發現是因為在Springmvc的配置文件中(我的名字是spring-servlet.xml)沒有加入
<aop:config proxy-target-class="true"/>
<!-- 開啟shiro注解 -->
<bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
<property name="securityManager" ref="securityManager" />
</bean>
加入后解決問題.
也可以使用:
<!-- 支持Shiro對Controller的方法級AOP安全控制 begin-->
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor">
<property name="proxyTargetClass" value="true" />
</bean>
<bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
<property name="securityManager" ref="securityManager" />
</bean>
<!-- 保證實現了Shiro內部lifecycle函數的bean執行 -->
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>
但是一定要記住是把以下配置放在SpringMvc的配置文件中。
<aop:config proxy-target-class="true"/>
或
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor">
<property name="proxyTargetClass" value="true" />
</bean>