認證就是驗證用戶(hù)身份的過(guò)程。在認證過(guò)程中,用戶(hù)需要提交實(shí)體信息(Principals)和憑據信息(Credentials)以檢驗用戶(hù)是否合法。最常見(jiàn)的“實(shí)體/憑證”組合便是“用戶(hù)名/密碼”組合。
一、Shiro認證過(guò)程
1、收集實(shí)體/憑據信息
-
- UsernamePasswordToken token = new UsernamePasswordToken(username, password);
-
- token.setRememberMe(true);
UsernamePasswordToken支持最常見(jiàn)的用戶(hù)名/密碼的認證機制。同時(shí),由于它實(shí)現了RememberMeAuthenticationToken接口,我們可以通過(guò)令牌設置“記住我”的功能。
但是,“已記住”和“已認證”是有區別的:
已記住的用戶(hù)僅僅是非匿名用戶(hù),你可以通過(guò)subject.getPrincipals()獲取用戶(hù)信息。但是它并非是完全認證通過(guò)的用戶(hù),當你訪(fǎng)問(wèn)需要認證用戶(hù)的功能時(shí),你仍然需要重新提交認證信息。
這一區別可以參考亞馬遜網(wǎng)站,網(wǎng)站會(huì )默認記住登錄的用戶(hù),再次訪(fǎng)問(wèn)網(wǎng)站時(shí),對于非敏感的頁(yè)面功能,頁(yè)面上會(huì )顯示記住的用戶(hù)信息,但是當你訪(fǎng)問(wèn)網(wǎng)站賬戶(hù)信息時(shí)仍然需要再次進(jìn)行登錄認證。
2、提交實(shí)體/憑據信息
- Subject currentUser = SecurityUtils.getSubject();
- currentUser.login(token);
收集了實(shí)體/憑據信息之后,我們可以通過(guò)SecurityUtils工具類(lèi),獲取當前的用戶(hù),然后通過(guò)調用login方法提交認證。
3、認證處理
- try {
- currentUser.login(token);
- } catch ( UnknownAccountException uae ) { ...
- } catch ( IncorrectCredentialsException ice ) { ...
- } catch ( LockedAccountException lae ) { ...
- } catch ( ExcessiveAttemptsException eae ) { ...
- } ... catch your own ...
- } catch ( AuthenticationException ae ) {
-
- }
如果login方法執行完畢且沒(méi)有拋出任何異常信息,那么便認為用戶(hù)認證通過(guò)。之后在應用程序任意地方調用SecurityUtils.getSubject() 都可以獲取到當前認證通過(guò)的用戶(hù)實(shí)例,使用subject.isAuthenticated()判斷用戶(hù)是否已驗證都將返回true.
相反,如果login方法執行過(guò)程中拋出異常,那么將認為認證失敗。Shiro有著(zhù)豐富的層次鮮明的異常類(lèi)來(lái)描述認證失敗的原因,如代碼示例。
二、登出操作 登出操作可以通過(guò)調用subject.logout()來(lái)刪除你的登錄信息,如:
當執行完登出操作后,Session信息將被清空,subject將被視作為匿名用戶(hù)。
三、認證內部處理機制 以上,是Shiro認證在應用程序中的處理過(guò)程,下面將詳細解說(shuō)Shiro認證的內部處理機制。
如上圖,我們通過(guò)Shiro架構圖的認證部分,來(lái)說(shuō)明Shiro認證內部的處理順序:
1、應用程序構建了一個(gè)終端用戶(hù)認證信息的AuthenticationToken 實(shí)例后,調用Subject.login方法。
2、Sbuject的實(shí)例通常是DelegatingSubject類(lèi)(或子類(lèi))的實(shí)例對象,在認證開(kāi)始時(shí),會(huì )委托應用程序設置的securityManager實(shí)例調用securityManager.login(token)方法。
3、SecurityManager接受到token(令牌)信息后會(huì )委托內置的Authenticator的實(shí)例(通常都是ModularRealmAuthenticator類(lèi)的實(shí)例)調用authenticator.authenticate(token). ModularRealmAuthenticator在認證過(guò)程中會(huì )對設置的一個(gè)或多個(gè)Realm實(shí)例進(jìn)行適配,它實(shí)際上為Shiro提供了一個(gè)可拔插的認證機制。
4、如果在應用程序中配置了多個(gè)Realm,ModularRealmAuthenticator會(huì )根據配置的AuthenticationStrategy(認證策略)來(lái)進(jìn)行多Realm的認證過(guò)程。在Realm被調用后,AuthenticationStrategy將對每一個(gè)Realm的結果作出響應。
注:如果應用程序中僅配置了一個(gè)Realm,Realm將被直接調用而無(wú)需再配置認證策略。
5、判斷每一個(gè)Realm是否支持提交的token,如果支持,Realm將調用getAuthenticationInfo(token); getAuthenticationInfo 方法就是實(shí)際認證處理,我們通過(guò)覆蓋Realm的doGetAuthenticationInfo方法來(lái)編寫(xiě)我們自定義的認證處理。
四、使用多個(gè)Realm的處理機制: 1、Authenticator 默認實(shí)現是ModularRealmAuthenticator,它既支持單一Realm也支持多個(gè)Realm。如果僅配置了一個(gè)Realm,ModularRealmAuthenticator 會(huì )直接調用該Realm處理認證信息,如果配置了多個(gè)Realm,它會(huì )根據認證策略來(lái)適配Realm,找到合適的Realm執行認證信息。
自定義Authenticator的配置:
- [main]
- ...
- authenticator = com.foo.bar.CustomAuthenticator
- securityManager.authenticator = $authenticator
2、AuthenticationStrategy(認證策略) 當應用程序配置了多個(gè)Realm時(shí),ModularRealmAuthenticator將根據認證策略來(lái)判斷認證成功或是失敗。
例如,如果只有一個(gè)Realm驗證成功,而其他Realm驗證失敗,那么這次認證是否成功呢?如果大多數的Realm驗證成功了,認證是否就認為成功呢?或者,一個(gè)Realm驗證成功后,是否還需要判斷其他Realm的結果?認證策略就是根據應用程序的需要對這些問(wèn)題作出決斷。
認證策略是一個(gè)無(wú)狀態(tài)的組件,在認證過(guò)程中會(huì )經(jīng)過(guò)4次的調用:
- 在所有Realm被調用之前
- 在調用Realm的getAuthenticationInfo 方法之前
- 在調用Realm的getAuthenticationInfo 方法之后
- 在所有Realm被調用之后
認證策略的另外一項工作就是聚合所有Realm的結果信息封裝至一個(gè)AuthenticationInfo實(shí)例中,并將此信息返回,以此作為Subject的身份信息。
Shiro有3中認證策略的具體實(shí)現:
| AtLeastOneSuccessfulStrategy |
只要有一個(gè)(或更多)的Realm驗證成功,那么認證將被視為成功 |
| FirstSuccessfulStrategy |
第一個(gè)Realm驗證成功,整體認證將被視為成功,且后續Realm將被忽略 |
| AllSuccessfulStrategy |
所有Realm成功,認證才視為成功 |
ModularRealmAuthenticator 內置的認證策略默認實(shí)現是AtLeastOneSuccessfulStrategy 方式,因為這種方式也是被廣泛使用的一種認證策略。當然,你也可以通過(guò)配置文件定義你需要的策略,如:
- [main]
- ...
- authcStrategy = org.apache.shiro.authc.pam.FirstSuccessfulStrategy
- securityManager.authenticator.authenticationStrategy = $authcStrategy
- ...
3、Realm的順序 由剛才提到的認證策略,可以看到Realm在ModularRealmAuthenticator 里面的順序對認證是有影響的。
ModularRealmAuthenticator 會(huì )讀取配置在SecurityManager里的Realm。當執行認證是,它會(huì )遍歷Realm集合,對所有支持提交的token的Realm調用getAuthenticationInfo 。
因此,如果Realm的順序對你使用的認證策略結果有影響,那么你應該在配置文件中明確定義Realm的順序,如:
- blahRealm = com.company.blah.Realm
- ...
- fooRealm = com.company.foo.Realm
- ...
- barRealm = com.company.another.Realm
-
- securityManager.realms = $fooRealm, $barRealm, $blahRealm
(三)Shiro 授權
授權即訪(fǎng)問(wèn)控制,它將判斷用戶(hù)在應用程序中對資源是否擁有相應的訪(fǎng)問(wèn)權限。
如,判斷一個(gè)用戶(hù)有查看頁(yè)面的權限,編輯數據的權限,擁有某一按鈕的權限,以及是否擁有打印的權限等等。
一、授權的三要素 授權有著(zhù)三個(gè)核心元素:權限、角色和用戶(hù)。
權限 權限是Apache Shiro安全機制最核心的元素。它在應用程序中明確聲明了被允許的行為和表現。一個(gè)格式良好好的權限聲明可以清晰表達出用戶(hù)對該資源擁有的權限。
大多數的資源會(huì )支持典型的CRUD操作(create,read,update,delete),但是任何操作建立在特定的資源上才是有意義的。因此,權限聲明的根本思想就是建立在資源以及操作上。
而我們通過(guò)權限聲明僅僅能了解這個(gè)權限可以在應用程序中做些什么,而不能確定誰(shuí)擁有此權限。
于是,我們就需要在應用程序中對用戶(hù)和權限建立關(guān)聯(lián)。
通常的做法就是將權限分配給某個(gè)角色,然后將這個(gè)角色關(guān)聯(lián)一個(gè)或多個(gè)用戶(hù)。
權限聲明及粒度 Shiro權限聲明通常是使用以冒號分隔的表達式。就像前文所講,一個(gè)權限表達式可以清晰的指定資源類(lèi)型,允許的操作,可訪(fǎng)問(wèn)的數據。同時(shí),Shiro權限表達式支持簡(jiǎn)單的通配符,可以更加靈活的進(jìn)行權限設置。
下面以實(shí)例來(lái)說(shuō)明權限表達式。
可查詢(xún)用戶(hù)數據
User:view
可查詢(xún)或編輯用戶(hù)數據
User:view,edit
可對用戶(hù)數據進(jìn)行所有操作
User:* 或 user
可編輯id為123的用戶(hù)數據
User:edit:123
角色 Shiro支持兩種角色模式:
1、傳統角色:一個(gè)角色代表著(zhù)一系列的操作,當需要對某一操作進(jìn)行授權驗證時(shí),只需判斷是否是該角色即可。這種角色權限相對簡(jiǎn)單、模糊,不利于擴展。
2、權限角色:一個(gè)角色擁有一個(gè)權限的集合。授權驗證時(shí),需要判斷當前角色是否擁有該權限。這種角色權限可以對該角色進(jìn)行詳細的權限描述,適合更復雜的權限設計。
下面將詳細描述對兩種角色模式的授權實(shí)現。
二、授權實(shí)現 Shiro支持三種方式實(shí)現授權過(guò)程:
- 編碼實(shí)現
- 注解實(shí)現
- JSP Taglig實(shí)現
1、基于編碼的授權實(shí)現 1.1基于傳統角色授權實(shí)現 當需要驗證用戶(hù)是否擁有某個(gè)角色時(shí),可以調用Subject 實(shí)例的hasRole*方法驗證。
- Subject currentUser = SecurityUtils.getSubject();
- if (currentUser.hasRole("administrator")) {
} else {
} 相關(guān)驗證方法如下:
| Subject方法 |
描述 |
| hasRole(String roleName) |
當用戶(hù)擁有指定角色時(shí),返回true |
| hasRoles(List<String> roleNames) |
按照列表順序返回相應的一個(gè)boolean值數組 |
| hasAllRoles(Collection<String> roleNames) |
如果用戶(hù)擁有所有指定角色時(shí),返回true |
斷言支持 Shiro還支持以斷言的方式進(jìn)行授權驗證。斷言成功,不返回任何值,程序繼續執行;斷言失敗時(shí),將拋出異常信息。使用斷言,可以使我們的代碼更加簡(jiǎn)潔。
- Subject currentUser = SecurityUtils.getSubject();
-
-
- currentUser.checkRole("bankTeller");
- openBankAccount();
斷言的相關(guān)方法:
| Subject方法 |
描述 |
| checkRole(String roleName) |
斷言用戶(hù)是否擁有指定角色 |
| checkRoles(Collection<String> roleNames) |
斷言用戶(hù)是否擁有所有指定角色 |
| checkRoles(String... roleNames) |
對上一方法的方法重載 |
1.2 基于權限角色授權實(shí)現 相比傳統角色模式,基于權限的角色模式耦合性要更低些,它不會(huì )因角色的改變而對源代碼進(jìn)行修改,因此,基于權限的角色模式是更好的訪(fǎng)問(wèn)控制方式。
它的代碼實(shí)現有以下幾種實(shí)現方式:
1、基于權限對象的實(shí)現 創(chuàng )建org.apache.shiro.authz.Permission的實(shí)例,將該實(shí)例對象作為參數傳遞給Subject.isPermitted()進(jìn)行驗證。
- Permission printPermission = new PrinterPermission("laserjet4400n", "print");
- Subject currentUser = SecurityUtils.getSubject();
- if (currentUser.isPermitted(printPermission)) {
-
- } else {
-
- }
- Permission printPermission = new PrinterPermission("laserjet4400n", "print");
- Subject currentUser = SecurityUtils.getSubject();
- if (currentUser.isPermitted(printPermission)) {
-
- } else {
-
- }
相關(guān)方法如下:
| Subject方法 |
描述 |
| isPermitted(Permission p) |
Subject擁有制定權限時(shí),返回treu |
| isPermitted(List<Permission> perms) |
返回對應權限的boolean數組 |
| isPermittedAll(Collection<Permission> perms) |
Subject擁有所有制定權限時(shí),返回true |
2、 基于字符串的實(shí)現 相比笨重的基于對象的實(shí)現方式,基于字符串的實(shí)現便顯得更加簡(jiǎn)潔。
- Subject currentUser = SecurityUtils.getSubject();
- if (currentUser.isPermitted("printer:print:laserjet4400n")) {
-
- } else {
-
- }
使用冒號分隔的權限表達式是org.apache.shiro.authz.permission.WildcardPermission 默認支持的實(shí)現方式。
這里分別代表了 資源類(lèi)型:操作:資源ID
類(lèi)似基于對象的實(shí)現相關(guān)方法,基于字符串的實(shí)現相關(guān)方法:
isPermitted(String perm)、isPermitted(String... perms)、isPermittedAll(String... perms)
基于權限對象的斷言實(shí)現
- Subject currentUser = SecurityUtils.getSubject();
-
-
- Permission p = new AccountPermission("open");
- currentUser.checkPermission(p);
- openBankAccount();
基于字符串的斷言實(shí)現
- Subject currentUser = SecurityUtils.getSubject();
-
-
- currentUser.checkPermission("account:open");
- openBankAccount();
斷言實(shí)現的相關(guān)方法
| Subject方法 |
說(shuō)明 |
| checkPermission(Permission p) |
斷言用戶(hù)是否擁有制定權限 |
| checkPermission(String perm) |
斷言用戶(hù)是否擁有制定權限 |
| checkPermissions(Collection<Permission> perms) |
斷言用戶(hù)是否擁有所有指定權限 |
| checkPermissions(String... perms) |
斷言用戶(hù)是否擁有所有指定權限 |
2、基于注解的授權實(shí)現 Shiro注解支持AspectJ、Spring、Google-Guice等,可根據應用進(jìn)行不同的配置。
相關(guān)的注解:
@ RequiresAuthentication 可以用戶(hù)類(lèi)/屬性/方法,用于表明當前用戶(hù)需是經(jīng)過(guò)認證的用戶(hù)。
- @RequiresAuthentication
- public void updateAccount(Account userAccount) {
-
-
- ...
- }
@ RequiresGuest 表明該用戶(hù)需為”guest”用戶(hù)
@ RequiresPermissions 當前用戶(hù)需擁有制定權限
- @RequiresPermissions("account:create")
- public void createAccount(Account account) {
-
-
- ...
- }
@RequiresRoles 當前用戶(hù)需擁有制定角色
@ RequiresUser 當前用戶(hù)需為已認證用戶(hù)或已記住用戶(hù)
3、基于JSP TAG的授權實(shí)現 Shiro提供了一套JSP標簽庫來(lái)實(shí)現頁(yè)面級的授權控制。
在使用Shiro標簽庫前,首先需要在JSP引入shiro標簽:
- <%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
下面一一介紹Shiro的標簽:
guest標簽
驗證當前用戶(hù)是否為“訪(fǎng)客”,即未認證(包含未記?。┑挠脩?hù)
- <shiro:guest>
- Hi there! Please <a href="login.jsp">Login</a> or <a href="signup.jsp">Signup</a> today!
- </shiro:guest>
user標簽
認證通過(guò)或已記住的用戶(hù)
- <shiro:user>
- Welcome back John! Not John? Click <a href="login.jsp">here<a> to login.
- </shiro:user>
authenticated標簽
已認證通過(guò)的用戶(hù)。不包含已記住的用戶(hù),這是與user標簽的區別所在。
- <shiro:authenticated>
- <a href="updateAccount.jsp">Update your contact information</a>.
- </shiro:authenticated>
notAuthenticated標簽
未認證通過(guò)用戶(hù),與authenticated標簽相對應。與guest標簽的區別是,該標簽包含已記住用戶(hù)。
- <shiro:notAuthenticated>
- Please <a href="login.jsp">login</a> in order to update your credit card information.
- </shiro:notAuthenticated>
principal 標簽
輸出當前用戶(hù)信息,通常為登錄賬號信息
- Hello, <shiro:principal/>, how are you today?
hasRole標簽
驗證當前用戶(hù)是否屬于該角色
- <shiro:hasRole name="administrator">
- <a href="admin.jsp">Administer the system</a>
- </shiro:hasRole>
lacksRole標簽
與hasRole標簽邏輯相反,當用戶(hù)不屬于該角色時(shí)驗證通過(guò)
- <shiro:lacksRole name="administrator">
- Sorry, you are not allowed to administer the system.
- </shiro:lacksRole>
hasAnyRole標簽
驗證當前用戶(hù)是否屬于以下任意一個(gè)角色。
- <shiro:hasAnyRoles name="developer, project manager, administrator">
- You are either a developer, project manager, or administrator.
- </shiro:lacksRole>
hasPermission標簽
驗證當前用戶(hù)是否擁有制定權限
- <shiro:hasPermission name="user:create">
- <a href="createUser.jsp">Create a new User</a>
- </shiro:hasPermission>
lacksPermission標簽
與hasPermission標簽邏輯相反,當前用戶(hù)沒(méi)有制定權限時(shí),驗證通過(guò)
- <shiro:hasPermission name="user:create">
- <a href="createUser.jsp">Create a new User</a>
- </shiro:hasPermission>
三、Shiro授權的內部處理機制 1、在應用程序中調用授權驗證方法(Subject的isPermitted*或hasRole*等)
2、Sbuject的實(shí)例通常是DelegatingSubject類(lèi)(或子類(lèi))的實(shí)例對象,在認證開(kāi)始時(shí),會(huì )委托應用程序設置的securityManager實(shí)例調用相應的isPermitted*或hasRole*方法。
3、接下來(lái)SecurityManager會(huì )委托內置的Authorizer的實(shí)例(默認是ModularRealmAuthorizer 類(lèi)的實(shí)例,類(lèi)似認證實(shí)例,它同樣支持一個(gè)或多個(gè)Realm實(shí)例認證)調用相應的授權方法。
4、每一個(gè)Realm將檢查是否實(shí)現了相同的 Authorizer 接口。然后,將調用Reaml自己的相應的授權驗證方法。
當使用多個(gè)Realm時(shí),不同于認證策略處理方式,授權處理過(guò)程中:
1、當調用Realm出現異常時(shí),將立即拋出異常,結束授權驗證。
2、只要有一個(gè)Realm驗證成功,那么將認為授權成功,立即返回,結束認證。
(四)Realm 實(shí)現
在認證、授權內部實(shí)現機制中都有提到,最終處理都將交給Real進(jìn)行處理。因為在Shiro中,最終是通過(guò)Realm來(lái)獲取應用程序中的用戶(hù)、角色及權限信息的。通常情況下,在Realm中會(huì )直接從我們的數據源中獲取Shiro需要的驗證信息??梢哉f(shuō),Realm是專(zhuān)用于安全框架的DAO.
一、認證實(shí)現 正如前文所提到的,Shiro的認證過(guò)程最終會(huì )交由Realm執行,這時(shí)會(huì )調用Realm的getAuthenticationInfo(token)方法。
該方法主要執行以下操作:
1、檢查提交的進(jìn)行認證的令牌信息
2、根據令牌信息從數據源(通常為數據庫)中獲取用戶(hù)信息
3、對用戶(hù)信息進(jìn)行匹配驗證。
4、驗證通過(guò)將返回一個(gè)封裝了用戶(hù)信息的AuthenticationInfo實(shí)例。
5、驗證失敗則拋出AuthenticationException異常信息。
而在我們的應用程序中要做的就是自定義一個(gè)Realm類(lèi),繼承AuthorizingRealm抽象類(lèi),重載doGetAuthenticationInfo (),重寫(xiě)獲取用戶(hù)信息的方法。
- protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken) throws AuthenticationException {
- UsernamePasswordToken token = (UsernamePasswordToken) authcToken;
- User user = accountManager.findUserByUserName(token.getUsername());
- if (user != null) {
- return new SimpleAuthenticationInfo(user.getUserName(), user.getPassword(), getName());
- } else {
- return null;
- }
- }
二、授權實(shí)現 而授權實(shí)現則與認證實(shí)現非常相似,在我們自定義的Realm中,重載doGetAuthorizationInfo()方法,重寫(xiě)獲取用戶(hù)權限的方法即可。
- protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
- String userName = (String) principals.fromRealm(getName()).iterator().next();
- User user = accountManager.findUserByUserName(userName);
- if (user != null) {
- SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
- for (Group group : user.getGroupList()) {
- info.addStringPermissions(group.getPermissionList());
- }
- return info;
- } else {
- return null;
- }
- }
(五)Shiro 配置說(shuō)明
Apache Shiro的配置主要分為四部分:
- 對象和屬性的定義與配置
- URL的過(guò)濾器配置
- 靜態(tài)用戶(hù)配置
- 靜態(tài)角色配置
其中,由于用戶(hù)、角色一般由后臺進(jìn)行操作的動(dòng)態(tài)數據,因此Shiro配置一般僅包含前兩項的配置。
Apache Shiro的大多數組件是基于POJO的,因此我們可以使用POJO兼容的任何配置機制進(jìn)行配置,例如:Java代碼、Sping XML、YAML、JSON、ini文件等等。下面,以Spring XML的配置方式為例,并且對其中的一些配置參數進(jìn)行一些簡(jiǎn)單說(shuō)明。
Shiro對象的配置: 主要是對Shiro各個(gè)組件的實(shí)現進(jìn)行定義配置,主要組件在前文已做過(guò)簡(jiǎn)單介紹,這里不再一一說(shuō)明。
- <bean id="securityManager" class="org.apache.shiro.mgt.DefaultSecurityManager">
- <property name="cacheManager" ref="cacheManager"/>
<property name="sessionMode" value="native"/>
<property name="realm" ref="myRealm"/>
<property name="sessionManager" ref="sessionManager"/>
</bean> Shiro過(guò)濾器的配置 Shiro主要是通過(guò)URL過(guò)濾來(lái)進(jìn)行安全管理,這里的配置便是指定具體授權規則定義。
- <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
- <property name="securityManager" ref="securityManager"/>
- <property name="loginUrl" value="/login.jsp"/>
- <property name="successUrl" value="/home.jsp"/>
- <property name="unauthorizedUrl" value="/unauthorized.jsp"/> -->
- <property name="filterChainDefinitions">
- <value>
- # some example chain definitions:
- /admin/** = authc, roles[admin]
- /docs/** = authc, perms[document:read]
- /** = authc
- # more URL-to-FilterChain definitions here
- </value>
- </property>
- </bean>
URL過(guò)濾器配置說(shuō)明: Shiro可以通過(guò)配置文件實(shí)現基于URL的授權驗證。FilterChain定義格式:
URL_Ant_Path_Expression = Path_Specific_Filter_Chain
每個(gè)URL配置,表示匹配該URL的應用程序請求將由對應的過(guò)濾器進(jìn)行驗證。
例如:
[urls]
/index.html = anon
/user/create = anon
/user/** = authc
/admin/** = authc, roles[administrator]
/rest/** = authc, rest
/remoting/rpc/** = authc, perms["remote:invoke"]
URL表達式說(shuō)明 1、URL目錄是基于HttpServletRequest.getContextPath()此目錄設置
2、URL可使用通配符,**代表任意子目錄
3、Shiro驗證URL時(shí),URL匹配成功便不再繼續匹配查找。所以要注意配置文件中的URL順序,尤其在使用通配符時(shí)。
Filter Chain定義說(shuō)明 1、一個(gè)URL可以配置多個(gè)Filter,使用逗號分隔
2、當設置多個(gè)過(guò)濾器時(shí),全部驗證通過(guò),才視為通過(guò)
3、部分過(guò)濾器可指定參數,如perms,roles
Shiro內置的FilterChain
| Filter Name |
Class |
| anon |
org.apache.shiro.web.filter.authc.AnonymousFilter |
| authc |
org.apache.shiro.web.filter.authc.FormAuthenticationFilter |
| authcBasic |
org.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter |
| perms |
org.apache.shiro.web.filter.authz.PermissionsAuthorizationFilter |
| 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.authc.UserFilter |