什麼是shiro?
Shiro是apache旗下一個開源框架,它將軟件系統的安全認證相關的功能抽取出來,實現用戶身份認證,權限授權、加密、會話管理等功能,組成了一個通用的安全認證框架。
既然shiro將安全認證相關的功能抽取出來組成一個框架,使用shiro就可以非常快速的完成認證、授權等功能的開發,降低系統成本。 shiro使用廣泛,shiro可以運行在web應
用,非web應用,集羣分佈式應用中越來越多的用戶開始使用shiro。java領域中spring security(原名Acegi)也是一個開源的權限管理框架,但是spring security依賴spring運
行,而shiro就相對獨立,最主要是因爲shiro使用簡單、靈活,所以現在越來越多的用戶選擇shiro。
shiro框架
1.subject
Subject即主體,外部應用與subject進行交互,subject記錄了當前操作用戶,將用戶的概念理解爲當前操作的主體,可能是一個通過瀏覽器請求的用戶,也可能是一個運行的程序。Subject在shiro中是一個接口,接口中定義了很多認證相關的方法,外部程序通過subject進行認證授,而subject是通過SecurityManager安全管理器進行認證授權
2.SecurityManager
SecurityManager安全管理器器,對全部的subject進行安全管理,它是shiro的核心,負責對所有的subject進行安全管理。通過SecurityManager可以完成subject的認證、授權等,實質上SecurityManager是通過Authenticator進行認證,通過Authorizer進行授權,通過SessionManager進行會話管理等。SecurityManager是一個接口,繼承了Authenticator, Authorizer, SessionManager這三個接口。
3.Authenticator
Authenticator即認證器,對用戶身份進行認證,Authenticator是一個接口,shiro提供ModularRealmAuthenticator現類,通過ModularRealmAuthenticator基本上可以滿足大多數需求,也可以自定義認證器。
4.Authorizer
Authorizer授權器,用戶通過認證器認證通過,在訪問功能時需要通過授權器判斷用戶是否有此功能的操作權限。
5.realm
Realm即領域,相當於datasource數據源,securityManager進行安全認證需要通過Realm獲取用戶權限數據,比如:如果用戶身份數據在數據庫那麼realm就需要從數據庫獲取用戶身份信息。
注意:不要把realm理解成只是從數據源取數據,在realm中還有認證授權校驗的相關的代碼。
6.sessionManager
sessionManager即會話管理,shiro框架定義了一套會話管理,它不依賴web容器的session,所以shiro可以使用在非web應用上,也可以將分佈式應用的會話集中在一點管理,此特性可使它實現單點登錄。
7.sessionDAO
SessionDAO會話dao,是對session會話操作的一套接口,比如要將session存儲到數據庫,可以通過jdbc將會話存儲到數據庫。
8.CacheManager
CacheManager即緩存管理,將用戶權限數據存儲在緩存,這樣可以提高性能。
9.Cryptography
Cryptography即密碼管理,shiro提供了一套加密/解密的組件,方便開發。比如提供常用的散列、加/解密等功能。
shiro所需要的jar
與其它java開源框架類似,將shiro的jar包加入項目就可以使用shiro提供的功能了。shiro-core是核心包必須選用,還提供了與web整合的shiro-web、與spring整合的shiro-spring、與任務調度quartz整合的shiro-quartz等,下邊是shiro各jar包的maven座標。
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-web</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-ehcache</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-quartz</artifactId>
<version>1.2.3</version>
</dependency>
shiro認證流程
shiro授權流程
shiro過濾器
Shiro驗證URL時,URL匹配成功便不再繼續匹配查找(所以要注意配置文件中的URL順序,尤其在使用通配符時)
故filterChainDefinitions的配置順序爲自上而下,以最上面的爲準
當運行一個Web應用程序時,Shiro將會創建一些有用的默認Filter實例,並自動地在[main]項中將它們置爲可用
自動地可用的默認的Filter實例是被DefaultFilter枚舉類定義的,枚舉的名稱字段就是可供配置的名稱
anon---------------org.apache.shiro.web.filter.authc.AnonymousFilter
authc--------------org.apache.shiro.web.filter.authc.FormAuthenticationFilter
authcBasic---------org.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter
logout-------------org.apache.shiro.web.filter.authc.LogoutFilter
noSessionCreation--org.apache.shiro.web.filter.session.NoSessionCreationFilter
perms--------------org.apache.shiro.web.filter.authz.PermissionAuthorizationFilter
port---------------org.apache.shiro.web.filter.authz.PortFilter
rest---------------org.apache.shiro.web.filter.authz.HttpMethodPermissionFilter
roles--------------org.apache.shiro.web.filter.authz.RolesAuthorizationFilter
ssl----------------org.apache.shiro.web.filter.authz.SslFilter
user---------------org.apache.shiro.web.filter.authz.UserFilter
通常可將這些過濾器分爲兩組anon,authc,authcBasic,user是第一組認證過濾器
perms,port,rest,roles,ssl是第二組授權過濾器
注意user和authc不同:當應用開啓了rememberMe時,用戶下次訪問時可以是一個user,但絕不會是authc,因爲authc是需要重新認證的
user表示用戶不一定已通過認證,只要曾被Shiro記住過登錄狀態的用戶就可以正常發起請求,比如rememberMe
說白了,以前的一個用戶登錄時開啓了rememberMe,然後他關閉瀏覽器,下次再訪問時他就是一個user,而不會authc
舉幾個例子/admin=authc,roles[admin] 表示用戶必需已通過認證,並擁有admin角色纔可以正常發起'/admin'請求
/edit=authc,perms[admin:edit] 表示用戶必需已通過認證,並擁有admin:edit權限纔可以正常發起'/edit'請求
/home=user 表示用戶不一定需要已經通過認證,只需要曾經被Shiro記住過登錄狀態就可以正常發起'/home'請求
各默認過濾器常用如下(注意URL Pattern裏用到的是兩顆星,這樣才能實現任意層次的全匹配)
/admins/**=anon 無參,表示可匿名使用,可以理解爲匿名用戶或遊客
/admins/user/**=authc 無參,表示需認證才能使用
/admins/user/**=authcBasic 無參,表示httpBasic認證
/admins/user/**=user 無參,表示必須存在用戶,當登入操作時不做檢查
/admins/user/**=ssl 無參,表示安全的URL請求,協議爲https
/admins/user/**=perms[user:add:*]
參數可寫多個,多參時必須加上引號,且參數之間用逗號分割,如/admins/user/**=perms["user:add:*,user:modify:*"]
當有多個參數時必須每個參數都通過纔算通過,相當於isPermitedAll()方法
/admins/user/**=port[8081]
當請求的URL端口不是8081時,跳轉到schemal://serverName:8081?queryString
其中schmal是協議http或https等,serverName是你訪問的Host,8081是Port端口,queryString是你訪問的URL裏的?後面的參數
/admins/user/**=rest[user]
根據請求的方法,相當於/admins/user/**=perms[user:method],其中method爲post,get,delete等
/admins/user/**=roles[admin]
參數可寫多個,多個時必須加上引號,且參數之間用逗號分割,如/admins/user/**=roles["admin,guest"]
當有多個參數時必須每個參數都通過纔算通過,相當於hasAllRoles()方法
轉載地址:http://www.cppblog.com/guojingjia2006/archive/2014/05/14/206956.html