天天看點

shrio教程初級(七)shiro注解與緩存一、前言二、shiro注解三、緩存

總結上一章節,我們搭建springmvc+mybatis+shrio初級整合。主要步驟web.xml+applicationContext-shiro配置filter,最後實作自己的realm。

我們用到的過濾器配置:

anon

org.apache.shiro.web.filter.authc.AnonymousFilter                                    匿名過濾器 :例子: /js/** anon 

authc

org.apache.shiro.web.filter.authc.FormAuthenticationFilter                          需要認證過濾器:例子: /admins/user/**=authc 

logout

org.apache.shiro.web.filter.authc.LogoutFilte                                            退出過濾器,例子:/logout=logout 

perms

org.apache.shiro.web.filter.authz.PermissionsAuthorizationFilter                權限過濾器,例子:/items/queryItems=perms[item:query]

先前看到的都是需要xml配置,現在我們來看看shiro支援的注解權限配置,與jsp标簽權限控制。取消url攔截器xml配置, 使用注解才是真正項目實用。。

的controller的方法頭上加注解requiresPermissions注解+權限表達式即可以

這裡的标簽好比我們的if标簽

标簽名稱

标簽條件(均是顯示标簽内容)

<shiro:authenticated>

登入之後

<shiro:notAuthenticated>

不在登入狀态時

<shiro:guest>

使用者在沒有RememberMe時

<shiro:user>

使用者在RememberMe時

<shiro:hasAnyRoles name="abc,123" >

在有abc或者123角色時

<shiro:hasRole name="abc">

擁有角色abc

<shiro:lacksRole name="abc">

沒有角色abc

<shiro:hasPermission name="abc">

擁有權限資源abc

<shiro:lacksPermission name="abc">

沒有abc權限資源

<shiro:principal>

 <shiro:principal property="username"/>     顯示使用者身份中的屬性值

測試:我們發現我們沒有了修改的選項

shrio教程初級(七)shiro注解與緩存一、前言二、shiro注解三、緩存

我們發現我們用注解其實在程式開發的時候已經解決權限問題,但是需要項目經理規定權限命名,個人經驗根據資料表名稱而定。。。。。。例如:sys_user:update

我們發現授權會頻繁查詢資料的問題,我們會使用緩存。不能每次查詢都查詢一次資料庫。

在applicationContext-shiro.xml中配置緩存管理器。

ehcache.xml

在我們自定義的realm裡面定義清除緩存,即是生效。一般在我們的service方法裡面調用,為了保證權限及時性。

新版shiro的緩存無論是否正常退出,都會清空緩存。一定要記住修改權限資料之後,要情況緩存。到後面我們用radis的時候可以用緩存架構來實作cacheDao另說