@RequiresRoles@RequiresPermissions不起作用

韩哥123456 2014-09-03 10:50:36
楼主的配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

<!-- Root Context: defines shared resources visible to all other web components -->
<context:annotation-config />
<context:component-scan base-package="com.mvc" /> <!-- 自动扫描所有注解该路径 -->
<context:property-placeholder location="classpath:/hibernate.properties" />

<!-- 配置数据源 -->



<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!-- 基本属性 url、user、password -->
<property name="url" value="${dataSource.url}" />
<property name="username" value="${dataSource.username}" />
<property name="password" value="${dataSource.password}" />
<property name="driverClassName" value="${dataSource.driverClassName}" />
</bean>

<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${dataSource.dialect}</prop>
<prop key="hibernate.hbm2ddl.auto">${dataSource.hbm2ddl.auto}</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<property name="packagesToScan">
<list>
<value>com.mvc.model</value> <!-- 扫描实体类,也就是平时所说的model -->
</list>
</property>
</bean>

<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

<!-- 继承自AuthorizingRealm的自定义Realm,即指定Shiro验证用户登录的类为自定义的ShiroDbRealm.java -->
<bean id="myRealm" class="com.mvc.aspect.MyRealm"/>

<!-- Shiro默认会使用Servlet容器的Session,可通过sessionMode属性来指定使用Shiro原生Session -->
<!-- 即<property name="sessionMode" value="native"/>,详细说明见官方文档 -->
<!-- 这里主要是设置自定义的单Realm应用,若有多个Realm,可使用'realms'属性代替 -->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="realm" ref="myRealm"/>
</bean>

<!-- Shiro主过滤器本身功能十分强大,其强大之处就在于它支持任何基于URL路径表达式的、自定义的过滤器的执行 -->
<!-- Web应用中,Shiro可控制的Web请求必须经过Shiro主过滤器的拦截,Shiro对基于Spring的Web应用提供了完美的支持 -->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<!-- Shiro的核心安全接口,这个属性是必须的 -->
<property name="securityManager" ref="securityManager"/>
<!-- 要求登录时的链接(可根据项目的URL进行替换),非必须的属性,默认会自动寻找Web工程根目录下的"/login.jsp"页面 -->

<property name="loginUrl" value="/log.jsp"/> <!-- 登录没有权限跳入此链接 -->

<!-- 登录成功后要跳转的连接(本例中此属性用不到,因为登录成功后的处理逻辑在LoginController里硬编码为main.jsp了) -->
<!-- <property name="successUrl" value="/system/main"/> -->
<!-- 用户访问未对其授权的资源时,所显示的连接 -->
<!-- 若想更明显的测试此属性可以修改它的值,如unauthor.jsp,然后用[玄玉]登录后访问/admin/listUser.jsp就看见浏览器会显示unauthor.jsp -->
<property name="unauthorizedUrl" value="/index.jsp"/>
<!-- Shiro连接约束配置,即过滤链的定义 -->
<!-- 此处可配合我的这篇文章来理解各个过滤连的作用http://blog.csdn.net/jadyer/article/details/12172839 -->
<!-- 下面value值的第一个'/'代表的路径是相对于HttpServletRequest.getContextPath()的值来的 -->
<!-- anon:它对应的过滤器里面是空的,什么都没做,这里.do和.jsp后面的*表示参数,比方说login.jsp?main这种 -->
<!-- authc:该过滤器下的页面必须验证后才能访问,它是Shiro内置的一个拦截器org.apache.shiro.web.filter.authc.FormAuthenticationFilter -->
<property name="filterChainDefinitions">
<value>
<!-- /log.jsp=anon -->
/index.jsp=authc,perms[admin:manage]

<!-- /admin.do=authc -->
<!-- /reg.jsp=authc -->
<!-- /user.do=authc,perms[admin:manage] -->
<!-- /WEB-INF/jsp/log_success.jsp=authc,perms[admin:manage] -->
<!-- /log.jsp=authc,perms[admin:manage] -->
</value>
</property>
</bean>

<!-- 保证实现了Shiro内部lifecycle函数的bean执行 -->
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>

<!-- 开启Shiro的注解(如@RequiresRoles,@RequiresPermissions),需借助SpringAOP扫描使用Shiro注解的类,并在必要时进行安全逻辑验证 -->
<!-- 配置以下两个bean即可实现此功能 -->
<!-- Enable Shiro Annotations for Spring-configured beans. Only run after the lifecycleBeanProcessor has run -->
<!-- 由于本例中并未使用Shiro注解,故注释掉这两个bean(个人觉得将权限通过注解的方式硬编码在程序中,查看起来不是很方便,没必要使用) -->

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

<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>

<!-- 配置事务管理 -->

<!-- <bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

<tx:annotation-driven transaction-manager="txManager" />


<aop:config>
<aop:pointcut expression="execution(public * com.mvc.service.*.*(..))"
id="businessService" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="businessService" />

<aop:aspect id="aspect" ref="aspectBean">
<aop:pointcut expression="execution(public * com.mvc.service.*.*(..))" id="businessService1"/>
<aop:before pointcut-ref="businessService1" method="doBefore"/>
<aop:after-throwing pointcut-ref="businessService1" throwing="e" method="doAfterThrow" />
<aop:after pointcut-ref="businessService1" method="doAfter"/>
<aop:around pointcut-ref="businessService1" method="doAround"/>
</aop:aspect>

</aop:config>
<bean id="aspectBean" class="com.mvc.aspect.Aspect" />

<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="find*" read-only="true" propagation="NOT_SUPPORTED" />
get开头的方法不需要在事务中运行 。 有些情况是没有必要使用事务的,比如获取数据。开启事务本身对性能是有一定的影响的
<tx:method name="*" /> 其他方法在实务中运行

<tx:method name="get*" propagation="REQUIRED" read-only="true" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED"/>

</tx:attributes>
</tx:advice> -->
</beans>

实在没办法了,求大神的助攻。
...全文
3045 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
Rains_ 2018-05-29
  • 打赏
  • 举报
回复
放在springmvc.xml生效,application[-shiro].xml居然不生效。不知道为啥
韩哥123456 2016-12-14
  • 打赏
  • 举报
回复
看下你的controller的代码方法是private,还是public的,private的方式,spring不识别
小勤哥 2016-12-07
  • 打赏
  • 举报
回复
有人解决了吗,求助
街头小贩 2016-06-08
  • 打赏
  • 举报
回复
你的容器(tomcat)设置realm,这个东东不配合容器能好用吗
Java_er 2016-06-08
  • 打赏
  • 举报
回复
楼主解决了么?feedback一下啊
u016579597 2015-10-28
  • 打赏
  • 举报
回复
解决了吗?求教
gaofeng_monica 2015-08-13
  • 打赏
  • 举报
回复
这个问题怎么解决的啊,求教

24,923

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 Apache
社区管理员
  • Apache
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧