shiro的RequiresPermissions注解使用

权限控制是shiro最核心的东西
Shiro权限声明通常是使用以冒号分隔的表达式。一个权限表达式可以清晰的指定资源类型,允许的操作,可访问的数据。同时,Shiro权限表达式支持简单的通配符,可以更加灵活的进行权限设置。
下面以实例来说明权限表达式。
可查询用户数据
User:view
可查询或编辑用户数据
User:view,edit
可对用户数据进行所有操作
User:* 或 user
可编辑id为123的用户数据
User:edit:123

@ RequiresAuthentication

可以用户类/属性/方法,用于表明当前用户需是经过认证的用户。
使用这个注解之前,需要先在spring-mvc.xml加入一段代码(一定要写在最先加载的xml中,写在后面加载的xml中也不起作用)

<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>
  • 1
  • 2
  • 3
  • 4
  • 5

lifecycleBeanPostProcessor和securityManager是在shiro配置文件中定义好的:

<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"></bean>

<!-- Shiro安全管理器 -->
    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
        <property name="realm" ref="jdbcRealm"></property>
        <property name="cacheManager" ref="cacheManager"></property>
    </bean>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

在前端权限管理处写上权限的字符串
在前端权限管理处写上权限的字符串
contraller里写上注解,括号里是前端对应的权限字符串
contraller里方法上写上注解,括号里是前端对应的权限字符串
这样就可以控制已认证用户权限了

转载自:https://blog.csdn.net/MOTU_/article/details/74941419

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章