您的位置:首页 > 运维架构 > Apache

Apache Shiro 使用 RequiresPermissions with Spring...

2013-03-01 00:00 721 查看
Via:

http://zhidao.baidu.com/question/397868108.html

http://yingzhuo.iteye.com/blog/1709002

根据官方文档在启用Shiro 注解(例如,@RequiresRoles,@RequiresPermissions 等等)时,
需要Shiro 的Spring AOP 集成来扫描合适的注解类以及执行必要的安全逻辑。
只需添加这两个bean 定义到applicationContext-shiro.xml 中:

<bean class="org.springframwork.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor"/>

<bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
<property name="securityManager" ref="securityManager"/>
</bean>


但是,在使用Spring MVC 的情况下,会遇到:
Exception: org.hibernate.HibernateException: No Session found for current thread

解决方法:
将以上代码移至Spring MVC 的servlet 配置文件,如:applicationContext-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> 
<!-- Scans the classpath of this application for @Components to deploy as beans -->
<!-- 只应加装表现层Bean,否则可能引起问题 -->
<!-- 此处只应该加载表现层组件,如果此处还加载dao层或service层的bean会将之前容器加载的替换掉,而且此处不会进行AOP织入,所以会造成AOP失效问题(如事务不起作用) -->
<context:component-scan base-package="com.binaryoptions.controller">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

<!-- 其他spring-mvc框架配置 -->

<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:viewClass="org.springframework.web.servlet.view.JstlView"
p:prefix="/views/"
p:suffix=".jsp" />

<!-- more bean definitions go here -->
<!-- 为安全检查使用Shiro 的注解(例如,@RequiresRoles,@RequiresPermissions 等等)。 -->
<!--
以下两个bean的配置是为了在Controller层使用元注释控制权限
如果使用spring-mvc一定要不要放在webroot的配置文件中
-->
<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>

<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key="org.apache.shiro.authz.UnauthorizedException">/403</prop>
</props>
</property>
</bean>

</beans>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐