權限控制是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裏方法上寫上註解,括號裏是前端對應的權限字符串
這樣就可以控制已認證用戶權限了
轉載自:https://blog.csdn.net/MOTU_/article/details/74941419