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 中:
但是,在使用Spring MVC 的情况下,会遇到:
Exception: org.hibernate.HibernateException: No Session found for current thread
解决方法:
将以上代码移至Spring MVC 的servlet 配置文件,如:applicationContext-servlet.xml
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>
相关文章推荐
- Apache Shiro 整合Spring 进行权限验证 以及在Freemarker中使用shiro标签
- Apache shiro配置与使用(Spring整合)
- Apache Shiro 整合Spring 进行权限验证 以及在Freemarker中使用shiro标签
- 使用Apache Shiro和Spring搭建安全应用
- Spring的Aop授权不起作用,例如Shiro的注解(@RequiresRoles,@RequiresPermissions)
- shiro使用经验总结:【同时实现url和按钮的拦截,只能用配置文件。不需要用注解!!!已多次测试=@RequiresPermissions不能拦截url直接访问。只能拦截标签(鸡肋,不要用!!)
- Apache shiro+springmvc+springdata+jpa+swagger(零配置文件使用)
- Apache Shiro 整合Spring 进行权限验证 以及在Freemarker中使用shiro标签
- apache shiro 在spring 的使用
- Apache shiro的简单介绍与使用教程(与spring整合使用)
- Spring Boot 项目构建 之 使用 Spring Boot 构建应用(Building an Application with Spring Boot)
- Apache Shiro 使用手册(四)Realm 实现
- 简单两步快速实现shiro的配置和使用,包含登录验证、角色验证、权限验证以及shiro登录注销流程(基于spring的方式,使用maven构建)
- Apache Shiro 使用手册(三)Shiro 授权
- Apache Shiro 使用手册
- 完美企业后台管理项目架构,apache shiro,bootstrap,spring,springMVC,mybatis,github,gradle
- Shiro关于JSESSIONID失效报错问题,org.apache.shiro.session.UnknownSessionException: There is no session with i
- Spring Boot系列(十五) 安全框架Apache Shiro(三)RememberMe
- shiro学习随笔【四】session过期报 org.apache.shiro.session.UnknownSessionException: There is no session with id
- Apache Shiro 使用手册(五)Shiro 配置说明