Spring Mvc 和Shiro整合常见问题
问题1: @RequiresPermissions 注解不起作用。当Shiro和spring 整合完成后,认证(登陆)模块没有问题,但是授权模块使用user..isAuthenticated()方法能进行授权验证,使用注解方法不能授权。这是因为Shiro配置文件应放在SpringMvc的配置文件中,这样才能被SpringMvc扫描。如果放到spring的配置文件中将会出现不能扫描注解的情况。
<bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
<property name="securityManager" ref="securityManager"/>
</bean>
<!-- AOP式方法级权限检查 -->
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor">
<property name="proxyTargetClass" value="true" />
</bean>
<!-- 保证实现了Shiro内部lifecycle函数的bean执行 -->
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />
问题2:<property name="unauthorizedUrl" value= /> 配置不起作用,授权不成功是返回异常而不是返回dao指定的页面。
这个需要使用springmvc的异常处理机制。将下面代码添加到Shiro的配置文件中
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key="org.apache.shiro.authz.UnauthorizedException">error/unauthorized</prop>
</props>
</property>
</bean>