1 權限管理
1.1 什麼是權限管理
基本上涉及到用戶參與的系統都要進行權限管理,權限管理屬於系統安全的範疇,權限管理實現對用戶訪問系統的控制,按照安全規則或者安全策略控制用戶可以訪問而且只能訪問自己被授權的資源。
權限管理包括用戶身份認證和授權兩部分,簡稱認證授權。對於需要訪問控制的資源用戶首先經過身份認證,認證通過後用戶具有該資源的訪問權限方可訪問。
1.2 用戶身份認證
1.2.1 概念
身份認證,就是判斷一個用戶是否爲合法用戶的處理過程。最常用的簡單身份認證方式是系統通過覈對用戶輸入的用戶名和口令,看其是否與系統中存儲的該用戶的用戶名和口令一致,來判斷用戶身份是否正確。對於採用指紋等系統,則出示指紋;對於硬件Key等刷卡系統,則需要刷卡。
1.2.2 用戶名密碼身份認證流程
1.2.3 關鍵對象
上邊的流程圖中需要理解以下關鍵對象:
n Subject:主體
訪問系統的用戶,主體可以是用戶、程序等,進行認證的都稱爲主體;
n Principal:身份信息
是主體(subject)進行身份認證的標識,標識必須具有唯一性,如用戶名、手機號、郵箱地址等,一個主體可以有多個身份,但是必須有一個主身份(Primary Principal)。
n credential:憑證信息
是隻有主體自己知道的安全信息,如密碼、證書等。
1.3 授權
1.3.1 概念
授權,即訪問控制,控制誰能訪問哪些資源。主體進行身份認證後需要分配權限方可訪問系統的資源,對於某些資源沒有權限是無法訪問的。
1.3.2 授權流程
下圖中橙色爲授權流程。
1.3.3 關鍵對象
授權可簡單理解爲who對what(which)進行How操作:
n Who,即主體(Subject),主體需要訪問系統中的資源。
n What,即資源(Resource),如系統菜單、頁面、按鈕、類方法、系統商品信息等。資源包括資源類型和資源實例,比如商品信息爲資源類型,類型爲t01的商品爲資源實例,編號爲001的商品信息也屬於資源實例。
n How,權限/許可(Permission),規定了主體對資源的操作許可,權限離開資源沒有意義,如用戶查詢權限、用戶添加權限、某個類方法的調用權限、編號爲001用戶的修改權限等,通過權限可知主體對哪些資源都有哪些操作許可。
權限分爲粗顆粒和細顆粒,粗顆粒權限是指對資源類型的權限,細顆粒權限是對資源實例的權限。
主體、資源、權限關係如下圖:
1.3.4 權限模型
對上節中的主體、資源、權限通過數據模型表示。
主體(賬號、密碼)
權限(權限名稱、資源id)
角色(角色名稱)
角色和權限關係(角色id、權限id)
主體和角色關係(主體id、角色id)
如下圖:
通常企業開發中將資源和權限表合併爲一張權限表,如下:
資源(資源名稱、訪問地址)
權限(權限名稱、資源id)
合併爲:
權限(權限名稱、資源名稱、資源訪問地址)
上圖常被稱爲權限管理的通用模型,不過企業在開發中根據系統自身的特點還會對上圖進行修改,但是用戶、角色、權限、用戶角色關係、角色權限關係是需要去理解的。
1.3.5 權限分配
對主體分配權限,主體只允許在權限範圍內對資源進行操作,比如:對u01用戶分配商品修改權限,u01用戶只能對商品進行修改。
權限分配的數據通常需要持久化,根據上邊的數據模型創建表並將用戶的權限信息存儲在數據庫中。
1.3.6 權限控制
用戶擁有了權限即可操作權限範圍內的資源,系統不知道主體是否具有訪問權限需要對用戶的訪問進行控制。
1.3.6.1 基於角色的訪問控制
RBAC基於角色的訪問控制(Role-BasedAccess Control)是以角色爲中心進行訪問控制,比如:主體的角色爲總經理可以查詢企業運營報表,查詢員工工資信息等,訪問控制流程如下:
if(主體.hasRole("總經理角色id")){
查詢工資
}
缺點:以角色進行訪問控制粒度較粗,如果上圖中查詢工資所需要的角色變化爲總經理和部門經理,此時就需要修改判斷邏輯爲“判斷主體的角色是否是總經理或部門經理”,系統可擴展性差。
修改代碼如下:
if(主體.hasRole("總經理角色id") || 主體.hasRole("部門經理角色id")){
查詢工資
}
1.3.6.2 基於資源的訪問控制
RBAC基於資源的訪問控制(Resource-BasedAccess Control)是以資源爲中心進行訪問控制,比如:主體必須具有查詢工資權限纔可以查詢員工工資信息等,訪問控制流程如下:
上圖中的判斷邏輯代碼可以理解爲:
if(主體.hasPermission("查詢工資權限標識")){
查詢工資
}
優點:系統設計時定義好查詢工資的權限標識,即使查詢工資所需要的角色變化爲總經理和部門經理也只需要將“查詢工資信息權限”添加到“部門經理角色”的權限列表中,判斷邏輯不用修改,系統可擴展性強。
2 權限管理解決方案
2.1 粗顆粒度和細顆粒度
2.1.1 什麼是粗顆粒度和細顆粒度
對資源類型的管理稱爲粗顆粒度權限管理,即只控制到菜單、按鈕、方法,粗粒度的例子比如:用戶具有用戶管理的權限,具有導出訂單明細的權限。對資源實例的控制稱爲細顆粒度權限管理,即控制到數據級別的權限,比如:用戶只允許修改本部門的員工信息,用戶只允許導出自己創建的訂單明細。
2.1.2 如何實現粗顆粒度和細顆粒度
對於粗顆粒度的權限管理可以很容易做系統架構級別的功能,即系統功能操作使用統一的粗顆粒度的權限管理。
對於細顆粒度的權限管理不建議做成系統架構級別的功能,因爲對數據級別的控制是系統的業務需求,隨着業務需求的變更業務功能變化的可能性很大,建議對數據級別的權限控制在業務層個性化開發,比如:用戶只允許修改自己創建的商品信息可以在service接口添加校驗實現,service接口需要傳入當前操作人的標識,與商品信息創建人標識對比,不一致則不允許修改商品信息。
2.2 基於url攔截
基於url攔截是企業中常用的權限管理方法,實現思路是:將系統操作的每個url配置在權限表中,將權限對應到角色,將角色分配給用戶,用戶訪問系統功能通過Filter進行過慮,過慮器獲取到用戶訪問的url,只要訪問的url是用戶分配角色中的url則放行繼續訪問。
如下圖:
2.3 使用權限管理框架
對於權限管理基本上每個系統都有,使用權限管理框架完成權限管理功能的開發可以節省系統開發時間,並且權限管理框架提供了完善的認證和授權功能有利於系統擴展維護,但是學習權限管理框架是需要成本的,所以選擇一款簡單高效的權限管理框架顯得非常重要。
3 基於url攔截實現
3.1 環境準備
jdk:1.7.0_72
web容器:tomcat7
系統框架:springmvc3.2.0+mybatis3.2.7(詳細參考springmvc教案)
前臺UI:jquery easyUI1.2.2
3.2 數據庫
創建mysql5.1數據庫
創建用戶表、角色表、權限表、角色權限關係表、用戶角色關係表。
導入腳本,先導入shiro_sql_talbe.sql再導入shiro-sql_table_data.sql
3.3 activeUser用戶身份類
用戶登陸成功記錄activeUser信息並將activeUser存入session。
publicclass ActiveUser implements java.io.Serializable {
private String userid;//用戶id
private String usercode;// 用戶賬號
private String username;// 用戶名稱
private List<SysPermission> menus;// 菜單
private List<SysPermission> permissions;// 權限
3.4 anonymousURL.properties
anonymousURL.properties公開訪問地址,無需身份認證即可訪問。
3.5 commonURL.properties
commonURL.properties公共訪問地址,身份認證通過無需分配權限即可訪問。
3.6 用戶身份認證攔截器
使用springmvc攔截器對用戶身份認證進行攔截,如果用戶沒有登陸則跳轉到登陸頁面,本功能也可以使用filter實現。
publicclass LoginInterceptor implements HandlerInterceptor {
// 在進入controller方法之前執行
// 使用場景:比如身份認證校驗攔截,用戶權限攔截,如果攔截不放行,controller方法不再執行
@Override
publicboolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
// 校驗用戶訪問是否是公開資源地址(無需認證即可訪問)
List<String> open_urls = ResourcesUtil.gekeyList("anonymousURL");
// 用戶訪問的url
String url = request.getRequestURI();
for (String open_url : open_urls) {
if (url.indexOf(open_url) >= 0) {
// 如果訪問的是公開地址則放行
returntrue;
}
}
// 校驗用戶身份是否認證通過
HttpSession session = request.getSession();
ActiveUser activeUser = (ActiveUser) session.getAttribute("activeUser");
if (activeUser != null) {
// 用戶已經登陸認證,放行
returntrue;
}
// 跳轉到登陸頁面
request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request,
response);
returnfalse;
}
3.7 用戶授權攔截器
使用springmvc攔截器對用戶訪問url進行攔截,如果用戶訪問的url沒有分配權限則跳轉到無權操作提示頁面(refuse.jsp),本功能也可以使用filter實現。
publicclass PermissionInterceptor implements HandlerInterceptor {
// 在進入controller方法之前執行
// 使用場景:比如身份認證校驗攔截,用戶權限攔截,如果攔截不放行,controller方法不再執行
// 進入action方法前要執行
@Override
publicboolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
// TODO Auto-generatedmethod stub
// 用戶訪問地址:
String url = request.getRequestURI();
// 校驗用戶訪問是否是公開資源地址(無需認證即可訪問)
List<String> open_urls = ResourcesUtil.gekeyList("anonymousURL");
// 用戶訪問的url
for (String open_url : open_urls) {
if (url.indexOf(open_url) >= 0) {
// 如果訪問的是公開地址則放行
returntrue;
}
}
//從 session獲取用戶公共訪問地址(認證通過無需分配權限即可訪問)
List<String> common_urls = ResourcesUtil.gekeyList("commonURL");
// 用戶訪問的url
for (String common_url : common_urls) {
if (url.indexOf(common_url) >= 0) {
// 如果訪問的是公共地址則放行
returntrue;
}
}
// 從session獲取用戶權限信息
HttpSession session = request.getSession();
ActiveUser activeUser = (ActiveUser) session.getAttribute("activeUser");
// 取出session中權限url
// 獲取用戶操作權限
List<SysPermission> permission_list =activeUser.getPermissions();
// 校驗用戶訪問地址是否在用戶權限範圍內
for (SysPermission sysPermission : permission_list) {
String permission_url = sysPermission.getUrl();
if (url.contains(permission_url)) {
returntrue;
}
}
// 跳轉到頁面
request.getRequestDispatcher("/WEB-INF/jsp/refuse.jsp").forward(
request, response);
returnfalse;
}
3.8 用戶登陸
用戶輸入用戶賬號和密碼登陸,登陸成功將用戶的身份信息(用戶賬號、密碼、權限菜單、權限url等)記入activeUser類,並寫入session。
3.8.1 controller
//用戶登陸提交
@RequestMapping("/loginsubmit")
public String loginsubmit(HttpSession session,Stringusercode,String password,String randomcode) throws Exception{
//校驗驗證碼
//從session獲取正確的驗證碼
String validateCode = (String)session.getAttribute("validateCode");
if(!randomcode.equals(validateCode)){
//拋出異常:驗證碼錯誤
thrownew CustomException("驗證碼錯誤!");
}
//用戶身份認證
ActiveUser activeUser = sysService.authenticat(usercode,password);
//記錄session
session.setAttribute("activeUser", activeUser);
return"redirect:first.action";
}
3.8.2 service接口
/**
*
* <p>
*Title: authenticat
* </p>
* <p>
*Description:用戶認證
* </p>
*
* @param usercode
* 用戶賬號
* @param password
* 用戶密碼
* @return ActiveUser 用戶身份信息
* @throws Exception
*/
public ActiveUser authenticat(String usercode, String password)
throws Exception;
// 根據賬號查詢用戶
public SysUser findSysuserByUsercode(String usercode) throws Exception;
// 根據用戶id獲取權限
public List<SysPermission> findSysPermissionList(Stringuserid)
throws Exception;
// 根據用戶id獲取菜單
public List<SysPermission> findMenuList(Stringuserid) throws Exception;
4 shiro介紹
4.1 什麼是shiro
Shiro是apache旗下一個開源框架,它將軟件系統的安全認證相關的功能抽取出來,實現用戶身份認證,權限授權、加密、會話管理等功能,組成了一個通用的安全認證框架。
4.2 爲什麼要學shiro
既然shiro將安全認證相關的功能抽取出來組成一個框架,使用shiro就可以非常快速的完成認證、授權等功能的開發,降低系統成本。
shiro使用廣泛,shiro可以運行在web應用,非web應用,集羣分佈式應用中越來越多的用戶開始使用shiro。
java領域中springsecurity(原名Acegi)也是一個開源的權限管理框架,但是spring security依賴spring運行,而shiro就相對獨立,最主要是因爲shiro使用簡單、靈活,所以現在越來越多的用戶選擇shiro。
4.3 Shiro架構
4.3.1 Subject
Subject即主體,外部應用與subject進行交互,subject記錄了當前操作用戶,將用戶的概念理解爲當前操作的主體,可能是一個通過瀏覽器請求的用戶,也可能是一個運行的程序。 Subject在shiro中是一個接口,接口中定義了很多認證授相關的方法,外部程序通過subject進行認證授,而subject是通過SecurityManager安全管理器進行認證授權
4.3.2 SecurityManager
SecurityManager即安全管理器,對全部的subject進行安全管理,它是shiro的核心,負責對所有的subject進行安全管理。通過SecurityManager可以完成subject的認證、授權等,實質上SecurityManager是通過Authenticator進行認證,通過Authorizer進行授權,通過SessionManager進行會話管理等。
SecurityManager是一個接口,繼承了Authenticator,Authorizer, SessionManager這三個接口。
4.3.3 Authenticator
Authenticator即認證器,對用戶身份進行認證,Authenticator是一個接口,shiro提供ModularRealmAuthenticator實現類,通過ModularRealmAuthenticator基本上可以滿足大多數需求,也可以自定義認證器。
4.3.4 Authorizer
Authorizer即授權器,用戶通過認證器認證通過,在訪問功能時需要通過授權器判斷用戶是否有此功能的操作權限。
4.3.5 realm
Realm即領域,相當於datasource數據源,securityManager進行安全認證需要通過Realm獲取用戶權限數據,比如:如果用戶身份數據在數據庫那麼realm就需要從數據庫獲取用戶身份信息。
注意:不要把realm理解成只是從數據源取數據,在realm中還有認證授權校驗的相關的代碼。
4.3.6 sessionManager
sessionManager即會話管理,shiro框架定義了一套會話管理,它不依賴web容器的session,所以shiro可以使用在非web應用上,也可以將分佈式應用的會話集中在一點管理,此特性可使它實現單點登錄。
4.3.7 SessionDAO
SessionDAO即會話dao,是對session會話操作的一套接口,比如要將session存儲到數據庫,可以通過jdbc將會話存儲到數據庫。
4.3.8 CacheManager
CacheManager即緩存管理,將用戶權限數據存儲在緩存,這樣可以提高性能。
4.3.9 Cryptography
Cryptography即密碼管理,shiro提供了一套加密/解密的組件,方便開發。比如提供常用的散列、加/解密等功能。
4.4 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-all包括shiro所有的包:
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-all</artifactId>
<version>1.2.3</version>
</dependency>
參考lib目錄:
5 shiro認證
5.1 認證流程
5.2 入門程序(用戶登陸和退出)
5.2.1 創建java工程
jdk版本:1.7.0_72
eclipse:elipse-indigo
5.2.2 加入shiro-core的Jar包及依賴包
5.2.3 log4j.properties日誌配置文件
log4j.rootLogger=debug, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m %n
5.2.4 shiro.ini
通過Shiro.ini配置文件初始化SecurityManager環境。
配置 eclipse支持ini文件編輯:
在eclipse配置後,在classpath創建shiro.ini配置文件,爲了方便測試將用戶名和密碼配置的shiro.ini配置文件中:
[users]
zhang=123
lisi=123
5.2.5 認證代碼
// 用戶登陸、用戶退出
@Test
publicvoid testLoginLogout() {
// 構建SecurityManager工廠,IniSecurityManagerFactory可以從ini文件中初始化SecurityManager環境
Factory<SecurityManager> factory = new IniSecurityManagerFactory(
"classpath:shiro.ini");
// 通過工廠創建SecurityManager
SecurityManagersecurityManager = factory.getInstance();
// 將securityManager設置到運行環境中
SecurityUtils.setSecurityManager(securityManager);
// 創建一個Subject實例,該實例認證要使用上邊創建的securityManager進行
Subject subject = SecurityUtils.getSubject();
// 創建token令牌,記錄用戶認證的身份和憑證即賬號和密碼
UsernamePasswordToken token = new UsernamePasswordToken("zhang", "123");
try {
// 用戶登陸
subject.login(token);
} catch (AuthenticationException e) {
// TODO Auto-generatedcatch block
e.printStackTrace();
}
// 用戶認證狀態
Boolean isAuthenticated = subject.isAuthenticated();
System.out.println("用戶認證狀態:" +isAuthenticated);
// 用戶退出
subject.logout();
isAuthenticated = subject.isAuthenticated();
System.out.println("用戶認證狀態:" +isAuthenticated);
}
5.2.6 認證執行流程
1、 創建token令牌,token中有用戶提交的認證信息即賬號和密碼
2、 執行subject.login(token),最終由securityManager通過Authenticator進行認證
3、 Authenticator的實現ModularRealmAuthenticator調用realm從ini配置文件取用戶真實的賬號和密碼,這裏使用的是IniRealm(shiro自帶)
4、 IniRealm先根據token中的賬號去ini中找該賬號,如果找不到則給ModularRealmAuthenticator返回null,如果找到則匹配密碼,匹配密碼成功則認證通過。
5.2.7 常見的異常
n UnknownAccountException
賬號不存在異常如下:
org.apache.shiro.authc.UnknownAccountException: No account found for user。。。。
n IncorrectCredentialsException
當輸入密碼錯誤會拋此異常,如下:
org.apache.shiro.authc.IncorrectCredentialsException: Submitted credentials for token[org.apache.shiro.authc.UsernamePasswordToken - zhangsan, rememberMe=false] didnot match the expected credentials.
更多如下:
DisabledAccountException(帳號被禁用)
LockedAccountException(帳號被鎖定)
ExcessiveAttemptsException(登錄失敗次數過多)
ExpiredCredentialsException(憑證過期)等
5.3 自定義Realm
上邊的程序使用的是Shiro自帶的IniRealm,IniRealm從ini配置文件中讀取用戶的信息,大部分情況下需要從系統的數據庫中讀取用戶信息,所以需要自定義realm。
5.3.1 shiro提供的realm
最基礎的是Realm接口,CachingRealm負責緩存處理,AuthenticationRealm負責認證,AuthorizingRealm負責授權,通常自定義的realm繼承AuthorizingRealm。
5.3.2 自定義Realm
publicclass CustomRealm1 extends AuthorizingRealm {
@Override
public String getName() {
return"customRealm1";
}
//支持UsernamePasswordToken
@Override
publicboolean supports(AuthenticationToken token) {
return token instanceof UsernamePasswordToken;
}
//認證
@Override
protected AuthenticationInfodoGetAuthenticationInfo(
AuthenticationToken token) throws AuthenticationException{
//從token中獲取用戶身份信息
String username = (String) token.getPrincipal();
//拿username從數據庫中查詢
//....
//如果查詢不到則返回null
if(!username.equals("zhang")){//這裏模擬查詢不到
returnnull;
}
//獲取從數據庫查詢出來的用戶密碼
String password = "123";//這裏使用靜態數據模擬。。
//返回認證信息由父類AuthenticatingRealm進行認證
SimpleAuthenticationInfo simpleAuthenticationInfo = newSimpleAuthenticationInfo(
username, password, getName());
return simpleAuthenticationInfo;
}
//授權
@Override
protected AuthorizationInfo doGetAuthorizationInfo(
PrincipalCollection principals) {
// TODO Auto-generatedmethod stub
returnnull;
}
}
5.3.3 shiro-realm.ini
[main]
#自定義 realm
customRealm=cn.itcast.shiro.authentication.realm.CustomRealm1
#將realm設置到securityManager
securityManager.realms=$customRealm
思考:這裏爲什麼不用配置[users]了??
5.3.4 測試代碼
測試代碼同入門程序,將ini的地址修改爲shiro-realm.ini。
分別模擬賬號不存在、密碼錯誤、賬號和密碼正確進行測試。
5.4 散列算法
散列算法一般用於生成一段文本的摘要信息,散列算法不可逆,將內容可以生成摘要,無法將摘要轉成原始內容。散列算法常用於對密碼進行散列,常用的散列算法有MD5、SHA。
一般散列算法需要提供一個salt(鹽)與原始內容生成摘要信息,這樣做的目的是爲了安全性,比如:111111的md5值是:96e79218965eb72c92a549dd5a330112,拿着“96e79218965eb72c92a549dd5a330112”去md5破解網站很容易進行破解,如果要是對111111和salt(鹽,一個隨機數)進行散列,這樣雖然密碼都是111111加不同的鹽會生成不同的散列值。
5.4.1 例子
//md5加密,不加鹽
String password_md5 = new Md5Hash("111111").toString();
System.out.println("md5加密,不加鹽="+password_md5);
//md5加密,加鹽,一次散列
String password_md5_sale_1 = new Md5Hash("111111", "eteokues", 1).toString();
System.out.println("password_md5_sale_1="+password_md5_sale_1);
String password_md5_sale_2 = new Md5Hash("111111", "uiwueylm", 1).toString();
System.out.println("password_md5_sale_2="+password_md5_sale_2);
//兩次散列相當於md5(md5())
//使用SimpleHash
String simpleHash = new SimpleHash("MD5", "111111", "eteokues",1).toString();
System.out.println(simpleHash);
5.4.2 在realm中使用
實際應用是將鹽和散列後的值存在數據庫中,自動realm從數據庫取出鹽和加密後的值由shiro完成密碼校驗。
5.4.2.1 自定義realm
@Override
protected AuthenticationInfo doGetAuthenticationInfo(
AuthenticationToken token) throws AuthenticationException{
//用戶賬號
String username = (String) token.getPrincipal();
//根據用戶賬號從數據庫取出鹽和加密後的值
//..這裏使用靜態數據
//如果根據賬號沒有找到用戶信息則返回null,shiro拋出異常“賬號不存在”
//按照固定規則加密碼結果,此密碼要在數據庫存儲,原始密碼是111111,鹽是eteokues
String password = "cb571f7bd7a6f73ab004a70322b963d5";
//鹽,隨機數,此隨機數也在數據庫存儲
String salt = "eteokues";
//返回認證信息
SimpleAuthenticationInfosimpleAuthenticationInfo = new SimpleAuthenticationInfo(
username, password, ByteSource.Util.bytes(salt),getName());
return simpleAuthenticationInfo;
}
5.4.2.2 realm配置
配置shiro-cryptography.ini
[main]
#定義憑證匹配器
credentialsMatcher=org.apache.shiro.authc.credential.HashedCredentialsMatcher
#散列算法
credentialsMatcher.hashAlgorithmName=md5
#散列次數
credentialsMatcher.hashIterations=1
#將憑證匹配器設置到realm
customRealm=cn.itcast.shiro.authentication.realm.CustomRealm2
customRealm.credentialsMatcher=$credentialsMatcher
securityManager.realms=$customRealm
5.4.2.3測試代碼
測試代碼同上個章節,注意修改ini路徑。
6 shiro授權
6.1 授權流程
6.2 授權方式
Shiro 支持三種方式的授權:
n 編程式:通過寫if/else授權代碼塊完成:
Subject subject =SecurityUtils.getSubject();
if(subject.hasRole(“admin”)) {
//有權限
} else {
//無權限
}
n 註解式:通過在執行的Java方法上放置相應的註解完成:
@RequiresRoles("admin")
public void hello() {
//有權限
}
n JSP/GSP 標籤:在JSP/GSP 頁面通過相應的標籤完成:
<shiro:hasRolename="admin">
<!— 有權限—>
</shiro:hasRole>
本教程序授權測試使用第一種編程方式,實際與web系統集成使用後兩種方式。
6.3 授權測試
6.3.1 shiro-permission.ini
創建存放權限的配置文件shiro-permission.ini,如下:
#用戶zhang的密碼是123,此用戶具有role1和role2兩個角色
zhang=123,role1,role2
wang=123,role2
[roles]
#角色role1對資源user擁有create、update權限
role1=user:create,user:update
#角色role2對資源user擁有create、delete權限
role2=user:create,user:delete
#角色role3對資源user擁有create權限
role3=user:create
在ini文件中用戶、角色、權限的配置規則是:“用戶名=密碼,角色1,角色2...”“角色=權限1,權限2...”,首先根據用戶名找角色,再根據角色找權限,角色是權限集合。
6.3.2 權限字符串規則
權限字符串的規則是:“資源標識符:操作:資源實例標識符”,意思是對哪個資源的哪個實例具有什麼操作,“:”是資源/操作/實例的分割符,權限字符串也可以使用*通配符。
例子:
用戶創建權限:user:create,或user:create:*
用戶修改實例001的權限:user:update:001
用戶實例001的所有權限:user:*:001
6.3.3 測試代碼
測試代碼同認證代碼,注意ini地址改爲shiro-permission.ini,主要學習下邊授權的方法,注意:在用戶認證通過後執行下邊的授權代碼。
@Test
publicvoid testPermission() {
// 從ini文件中創建SecurityManager工廠
Factory<SecurityManager> factory = newIniSecurityManagerFactory(
"classpath:shiro-permission.ini");
// 創建SecurityManager
SecurityManager securityManager = factory.getInstance();
// 將securityManager設置到運行環境
SecurityUtils.setSecurityManager(securityManager);
// 創建主體對象
Subject subject = SecurityUtils.getSubject();
// 對主體對象進行認證
// 用戶登陸
// 設置用戶認證的身份(principals)和憑證(credentials)
UsernamePasswordToken token = new UsernamePasswordToken("zhang", "123");
try {
subject.login(token);
} catch (AuthenticationException e) {
// TODO Auto-generatedcatch block
e.printStackTrace();
}
// 用戶認證狀態
Boolean isAuthenticated = subject.isAuthenticated();
System.out.println("用戶認證狀態:" +isAuthenticated);
// 用戶授權檢測基於角色授權
// 是否有某一個角色
System.out.println("用戶是否擁有一個角色:" +subject.hasRole("role1"));
// 是否有多個角色
System.out.println("用戶是否擁有多個角色:" +subject.hasAllRoles(Arrays.asList("role1", "role2")));
// subject.checkRole("role1");
// subject.checkRoles(Arrays.asList("role1","role2"));
// 授權檢測,失敗則拋出異常
//subject.checkRole("role22");
// 基於資源授權
System.out.println("是否擁有某一個權限:" +subject.isPermitted("user:delete"));
System.out.println("是否擁有多個權限:" +subject.isPermittedAll("user:create:1", "user:delete"));
//檢查權限
subject.checkPermission("sys:user:delete");
subject.checkPermissions("user:create:1","user:delete");
}
6.3.4 基於角色的授權
// 用戶授權檢測基於角色授權
// 是否有某一個角色
System.out.println("用戶是否擁有一個角色:" +subject.hasRole("role1"));
// 是否有多個角色
System.out.println("用戶是否擁有多個角色:" +subject.hasAllRoles(Arrays.asList("role1", "role2")));
對應的check方法:
subject.checkRole("role1");
subject.checkRoles(Arrays.asList("role1", "role2"));
上邊check方法如果授權失敗則拋出異常:
org.apache.shiro.authz.UnauthorizedException:Subject does not have role [.....]
6.3.5 基於資源授權
// 基於資源授權
System.out.println("是否擁有某一個權限:" +subject.isPermitted("user:delete"));
System.out.println("是否擁有多個權限:" +subject.isPermittedAll("user:create:1", "user:delete"));
對應的check方法:
subject.checkPermission("sys:user:delete");
subject.checkPermissions("user:create:1","user:delete");
上邊check方法如果授權失敗則拋出異常:
org.apache.shiro.authz.UnauthorizedException:Subject does not have permission [....]
6.4 自定義realm
與上邊認證自定義realm一樣,大部分情況是要從數據庫獲取權限數據,這裏直接實現基於資源的授權。
6.4.1 realm代碼
在認證章節寫的自定義realm類中完善doGetAuthorizationInfo方法,此方法需要完成:根據用戶身份信息從數據庫查詢權限字符串,由shiro進行授權。
// 授權
@Override
protected AuthorizationInfo doGetAuthorizationInfo(
PrincipalCollection principals) {
// 獲取身份信息
String username = (String)principals.getPrimaryPrincipal();
// 根據身份信息從數據庫中查詢權限數據
//....這裏使用靜態數據模擬
List<String> permissions = newArrayList<String>();
permissions.add("user:create");
permissions.add("user:delete");
//將權限信息封閉爲AuthorizationInfo
SimpleAuthorizationInfo simpleAuthorizationInfo = newSimpleAuthorizationInfo();
for(String permission:permissions){
simpleAuthorizationInfo.addStringPermission(permission);
}
return simpleAuthorizationInfo;
}
6.4.2 shiro-realm.ini
ini配置文件還使用認證階段使用的,不用改變。
思考:shiro-permission.ini中的[roles]爲什麼不需要了??
6.4.3 測試代碼
同上邊的授權測試代碼,注意修改ini地址爲shiro-realm.ini。
6.4.4 授權執行流程
1、 執行subject.isPermitted("user:create")
2、 securityManager通過ModularRealmAuthorizer進行授權
3、 ModularRealmAuthorizer調用realm獲取權限信息
4、 ModularRealmAuthorizer再通過permissionResolver解析權限字符串,校驗是否匹配
7 shiro與項目集成開發
7.1 shiro與spring web項目整合
shiro與springweb項目整合在“基於url攔截實現的工程”基礎上整合,基於url攔截實現的工程的技術架構是springmvc+mybatis,整合注意兩點:
1、shiro與spring整合
2、加入shiro對web應用的支持
7.1.1 取消原springmvc認證和授權攔截器
去掉springmvc.xml中配置的LoginInterceptor和PermissionInterceptor攔截器。
7.1.2 加入shiro的 jar包
7.1.3 web.xml添加shiro Filter
<!-- shiro過慮器,DelegatingFilterProxy通過代理模式將spring容器中的bean和filter關聯起來 -->
<filter>
<filter-name>shiroFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<!-- 設置true由servlet容器控制filter的生命週期 -->
<init-param>
<param-name>targetFilterLifecycle</param-name>
<param-value>true</param-value>
</init-param>
<!-- 設置spring容器filter的bean id,如果不設置則找與filter-name一致的bean-->
<init-param>
<param-name>targetBeanName</param-name>
<param-value>shiroFilter</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>shiroFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
7.1.4 applicationContext-shiro.xml
<!-- Shiro 的Web過濾器 -->
<bean id="shiroFilter"class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager"ref="securityManager" />
<!-- loginUrl認證提交地址,如果沒有認證將會請求此地址進行認證,請求此地址將由formAuthenticationFilter進行表單認證 -->
<property name="loginUrl"value="/login.action" />
<property name="unauthorizedUrl" value="/refuse.jsp" />
<!-- 過慮器鏈定義,從上向下順序執行,一般將/**放在最下邊 -->
<property name="filterChainDefinitions">
<value>
<!-- 退出攔截,請求logout.action執行退出操作 -->
/logout.action = logout
<!-- 無權訪問頁面 -->
/refuse.jsp = anon
<!-- roles[XX]表示有XX角色纔可訪問 -->
/item/list.action = roles[item],authc
/js/** anon
/images/** anon
/styles/** anon
/validatecode.jsp anon
/item/* authc
<!-- user表示身份認證通過或通過記住我認證通過的可以訪問 -->
/** = authc
</value>
</property>
</bean>
<!-- 安全管理器 -->
<bean id="securityManager"class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="realm"ref="userRealm" />
</bean>
<!-- 自定義 realm -->
<bean id="userRealm"class="cn.itcast.ssm.realm.CustomRealm1">
</bean>
securityManager:這個屬性是必須的。
loginUrl:沒有登錄認證的用戶請求將跳轉到此地址進行認證,不是必須的屬性,不輸入地址的話會自動尋找項目web項目的根目錄下的”/login.jsp”頁面。
unauthorizedUrl:沒有權限默認跳轉的頁面。
7.1.5 自定義realm
此realm先不從數據庫查詢權限數據,當前需要先將shiro整合完成,在上邊章節定義的realm基礎上修改。
publicclass CustomRealm1 extends AuthorizingRealm {
@Override
public String getName() {
return"customRealm";
}
// 支持什麼類型的token
@Override
publicboolean supports(AuthenticationToken token) {
return token instanceof UsernamePasswordToken;
}
// 認證
@Override
protected AuthenticationInfo doGetAuthenticationInfo(
AuthenticationToken token) throws AuthenticationException{
// 從token中獲取用戶身份信息
String username = (String) token.getPrincipal();
// 拿username從數據庫中查詢
// ....
// 如果查詢不到則返回null
if (!username.equals("zhang")) {// 這裏模擬查詢不到
returnnull;
}
// 獲取從數據庫查詢出來的用戶密碼
String password = "123";// 這裏使用靜態數據模擬。。
// 根據用戶id從數據庫取出菜單
//...先用靜態數據
List<SysPermission> menus = newArrayList<SysPermission>();;
SysPermission sysPermission_1 = new SysPermission();
sysPermission_1.setName("商品管理");
sysPermission_1.setUrl("/item/queryItem.action");
SysPermission sysPermission_2 = new SysPermission();
sysPermission_2.setName("用戶管理");
sysPermission_2.setUrl("/user/query.action");
menus.add(sysPermission_1);
menus.add(sysPermission_2);
// 構建用戶身份信息
ActiveUser activeUser = new ActiveUser();
activeUser.setUserid(username);
activeUser.setUsername(username);
activeUser.setUsercode(username);
activeUser.setMenus(menus);
// 返回認證信息由父類AuthenticatingRealm進行認證
SimpleAuthenticationInfo simpleAuthenticationInfo = newSimpleAuthenticationInfo(
activeUser, password, getName());
return simpleAuthenticationInfo;
}
// 授權
@Override
protected AuthorizationInfo doGetAuthorizationInfo(
PrincipalCollection principals) {
// 獲取身份信息
ActiveUser activeUser = (ActiveUser)principals.getPrimaryPrincipal();
//用戶id
String userid = activeUser.getUserid();
// 根據用戶id從數據庫中查詢權限數據
// ....這裏使用靜態數據模擬
List<String> permissions = newArrayList<String>();
permissions.add("item:query");
permissions.add("item:update");
// 將權限信息封閉爲AuthorizationInfo
SimpleAuthorizationInfo simpleAuthorizationInfo = newSimpleAuthorizationInfo();
for (String permission : permissions) {
simpleAuthorizationInfo.addStringPermission(permission);
}
return simpleAuthorizationInfo;
}
}
7.1.6 登錄
// 用戶登陸提交
@RequestMapping("/login")
public Stringloginsubmit(Model model, HttpServletRequest request)
throws Exception {
// shiro在認證過程中出現錯誤後將異常類路徑通過request返回
String exceptionClassName = (String) request
.getAttribute("shiroLoginFailure");
if(exceptionClassName!=null){
if (UnknownAccountException.class.getName().equals(exceptionClassName)){
thrownew CustomException("賬號不存在");
} elseif (IncorrectCredentialsException.class.getName().equals(
exceptionClassName)) {
thrownew CustomException("用戶名/密碼錯誤");
} elseif("randomCodeError".equals(exceptionClassName)){
thrownew CustomException("驗證碼錯誤");
} else{
thrownew Exception();//最終在異常處理器生成未知錯誤
}
}
return"login";
}
7.1.7 首頁
由於session由shiro管理,需要修改首頁的controller方法,將session中的數據通過model傳到頁面。
//系統首頁
@RequestMapping("/first")
public String first(Model model)throws Exception{
//主體
Subject subject = SecurityUtils.getSubject();
//身份
ActiveUser activeUser = (ActiveUser) subject.getPrincipal();
model.addAttribute("activeUser", activeUser);
return"/first";
}
7.1.8 退出
由於使用shiro的sessionManager,不用開發退出功能,使用shiro的logout攔截器即可。
<!-- 退出攔截,請求logout.action執行退出操作 -->
/logout.action = logout
7.1.9 無權限refuse.jsp
當用戶無操作權限,shiro將跳轉到refuse.jsp頁面。
7.1.10 shiro過慮器總結
過濾器簡稱 |
對應的java類 |
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 |
logout |
org.apache.shiro.web.filter.authc.LogoutFilter |
anon:例子/admins/**=anon 沒有參數,表示可以匿名使用。
authc:例如/admins/user/**=authc表示需要認證(登錄)才能使用,FormAuthenticationFilter是表單認證,沒有參數
roles:例子/admins/user/**=roles[admin],參數可以寫多個,多個時必須加上引號,並且參數之間用逗號分割,當有多個參數時,例如admins/user/**=roles["admin,guest"],每個參數通過纔算通過,相當於hasAllRoles()方法。
perms:例子/admins/user/**=perms[user:add:*],參數可以寫多個,多個時必須加上引號,並且參數之間用逗號分割,例如/admins/user/**=perms["user:add:*,user:modify:*"],當有多個參數時必須每個參數都通過才通過,想當於isPermitedAll()方法。
rest:例子/admins/user/**=rest[user],根據請求的方法,相當於/admins/user/**=perms[user:method],其中method爲post,get,delete等。
port:例子/admins/user/**=port[8081],當請求的url的端口不是8081是跳轉到schemal://serverName:8081?queryString,其中schmal是協議http或https等,serverName是你訪問的host,8081是url配置裏port的端口,queryString
是你訪問的url裏的?後面的參數。
authcBasic:例如/admins/user/**=authcBasic沒有參數表示httpBasic認證
ssl:例子/admins/user/**=ssl沒有參數,表示安全的url請求,協議爲https
user:例如/admins/user/**=user沒有參數表示必須存在用戶,身份認證通過或通過記住我認證通過的可以訪問,當登入操作時不做檢查
注:
anon,authcBasic,auchc,user是認證過濾器,
perms,roles,ssl,rest,port是授權過濾器
7.2 認證
7.2.1 添加憑證匹配器
添加憑證匹配器實現md5加密校驗。
修改applicationContext-shiro.xml:
<!-- 憑證匹配器 -->
<bean id="credentialsMatcher"
class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
<property name="hashAlgorithmName" value="md5" />
<property name="hashIterations" value="1" />
</bean>
<!-- 自定義 realm -->
<bean id="userRealm" class="cn.itcast.ssm.realm.CustomRealm1">
<property name="credentialsMatcher" ref="credentialsMatcher" />
</bean>
7.2.2 修改realm認證方法
修改realm代碼從數據庫中查詢用戶身份信息,將sysService注入realm。
publicclass CustomRealm1 extends AuthorizingRealm {
@Autowired
private SysService sysService;
@Override
public String getName() {
return"customRealm";
}
// 支持什麼類型的token
@Override
publicboolean supports(AuthenticationToken token) {
return token instanceof UsernamePasswordToken;
}
@Override
protected AuthenticationInfo doGetAuthenticationInfo(
AuthenticationToken token) throws AuthenticationException{
// 從token中獲取用戶身份
String usercode = (String) token.getPrincipal();
SysUser sysUser = null;
try {
sysUser = sysService.findSysuserByUsercode(usercode);
} catch (Exception e) {
// TODO Auto-generatedcatch block
e.printStackTrace();
}
// 如果賬號不存在
if (sysUser == null) {
return null;
}
// 根據用戶id取出菜單
List<SysPermission> menus = null;
try {
menus = sysService.findMenuList(sysUser.getId());
} catch (Exception e) {
// TODO Auto-generatedcatch block
e.printStackTrace();
}
// 用戶密碼
String password = sysUser.getPassword();
//鹽
String salt = sysUser.getSalt();
// 構建用戶身體份信息
ActiveUser activeUser = new ActiveUser();
activeUser.setUserid(sysUser.getId());
activeUser.setUsername(sysUser.getUsername());
activeUser.setUsercode(sysUser.getUsercode());
activeUser.setMenus(menus);
SimpleAuthenticationInfo simpleAuthenticationInfo = newSimpleAuthenticationInfo(
activeUser, password, ByteSource.Util.bytes(salt),getName());
return simpleAuthenticationInfo;
}
.....
}
7.3 授權
7.3.1 修改realm授權方法
修改realm代碼從數據庫中查詢權限信息,將sysService注入realm。
publicclass CustomRealm1 extends AuthorizingRealm {
@Autowired
private SysService sysService;
@Override
public String getName() {
return"customRealm";
}
// 支持什麼類型的token
@Override
publicboolean supports(AuthenticationToken token) {
return token instanceof UsernamePasswordToken;
}
@Override
protected AuthorizationInfo doGetAuthorizationInfo(
PrincipalCollection principals) {
//身份信息
ActiveUser activeUser = (ActiveUser)principals.getPrimaryPrincipal();
//用戶id
String userid = activeUser.getUserid();
//獲取用戶權限
List<SysPermission> permissions = null;
try {
permissions = sysService.findSysPermissionList(userid);
} catch (Exception e) {
// TODO Auto-generatedcatch block
e.printStackTrace();
}
//構建shiro授權信息
SimpleAuthorizationInfo simpleAuthorizationInfo = newSimpleAuthorizationInfo();
for(SysPermission sysPermission:permissions){
simpleAuthorizationInfo.addStringPermission(sysPermission.getPercode());
}
return simpleAuthorizationInfo;
}
}
7.3.2 對controller開啓AOP
在springmvc.xml中配置shiro註解支持,可在controller方法中使用shiro註解配置權限:
<!-- 開啓aop,對類代理 -->
<aop:config proxy-target-class="true"></aop:config>
<!-- 開啓shiro註解支持 -->
<bean
class="
org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
<property name="securityManager"ref="securityManager" />
</bean>
7.3.3 權限註解控制
商品查詢controller方法添加權限(item:query):
// 查詢商品列表
@RequestMapping("/queryItem")
@RequiresPermissions("item:query")
public ModelAndView queryItem() throws Exception {
上邊代碼@RequiresPermissions("item:query")表示必須擁有“item:query”權限方可執行。
同理,商品修改controller方法添加權限(item:update):
@RequestMapping(value = "/editItem")
@RequiresPermissions("item:update")
public String editItem(@RequestParam(value = "id", required = true) Integer id,Model model) throws Exception
// 商品修改提交
@RequestMapping("/editItemSubmit")
@RequiresPermissions("item:update")
public String editItemSubmit(@ModelAttribute("item") Itemsitems,BindingResult result,
MultipartFile pictureFile,Model model,HttpServletRequestrequest)
throws Exception
7.3.4 jsp標籤控制
7.3.4.1 標籤介紹
Jsp頁面添加:
<%@tagliburi="http://shiro.apache.org/tags" prefix="shiro"%>
標籤名稱 |
標籤條件(均是顯示標籤內容) |
<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:principalproperty="username"/> 顯示用戶身份中的屬性值
7.3.4.2jsp頁面添加標籤
如果有商品修改權限頁面顯示“修改”鏈接。
<shiro:hasPermission name="item:update">
<a href="${pageContext.request.contextPath }/item/editItem.action?id=${item.id}">修改</a>
</shiro:hasPermission>
7.4 緩存
shiro每次授權都會通過realm獲取權限信息,爲了提高訪問速度需要添加緩存,第一次從realm中讀取權限數據,之後不再讀取,這裏Shiro和Ehcache整合。
7.4.1 添加Ehcache的jar包
7.4.2 配置cacheManager
在applicationContext-shiro.xml中配置緩存管理器。
<!-- 安全管理器 -->
<bean id="securityManager"class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="realm"ref="userRealm" />
<property name="cacheManager"ref="cacheManager"/>
</bean>
<!-- 緩存管理器 -->
<bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
<property name="cacheManagerConfigFile" value="classpath:shiro-ehcache.xml"/>
</bean>
7.4.3 配置shiro-ehcache.xml
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">
<!--diskStore:緩存數據持久化的目錄地址 -->
<diskStore path="F:\develop\ehcache"/>
<defaultCache
maxElementsInMemory="1000"
maxElementsOnDisk="10000000"
eternal="false"
overflowToDisk="false"
diskPersistent="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU">
</defaultCache>
</ehcache>
7.4.4 清空緩存
當用戶權限修改後,用戶再次登陸shiro會自動調用realm從數據庫獲取權限數據,如果在修改權限後想立即清除緩存則可以調用realm的clearCache方法清除緩存。
realm中定義clearCached方法:
//清除緩存
publicvoid clearCached(){
PrincipalCollection principals = SecurityUtils.getSubject().getPrincipals();
super.clearCache(principals);
}
在權限修改後調用realm中的方法,realm已經由spring管理,所以從spring中獲取realm實例,調用clearCached方法。
7.5 session管理
在applicationContext-shiro.xml中配置sessionManager:
<!-- 安全管理器 -->
<bean id="securityManager"class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="realm"ref="userRealm" />
<property name="sessionManager"ref="sessionManager" />
</bean>
<!-- 會話管理器 -->
<bean id="sessionManager"class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
<!-- session的失效時長,單位毫秒 -->
<property name="globalSessionTimeout" value="600000"/>
<!-- 刪除失效的session -->
<property name="deleteInvalidSessions" value="true"/>
</bean>
7.6 驗證碼
7.6.1 自定義FormAuthenticationFilter
需要在驗證賬號和名稱之前校驗驗證碼。
publicclass MyFormAuthenticationFilter extendsFormAuthenticationFilter {
protectedboolean onAccessDenied(ServletRequest request,
ServletResponse response, Object mappedValue) throws Exception {
// 校驗驗證碼
// 從session獲取正確的驗證碼
HttpSession session =((HttpServletRequest)request).getSession();
//頁面輸入的驗證碼
String randomcode = request.getParameter("randomcode");
//從session中取出驗證碼
String validateCode = (String)session.getAttribute("validateCode");
if (randomcode!=null && validateCode!=null) {
if(!randomcode.equals(validateCode)) {
// randomCodeError表示驗證碼錯誤
request.setAttribute("shiroLoginFailure", "randomCodeError");
//拒絕訪問,不再校驗賬號和密碼
returntrue;
}
returnsuper.onAccessDenied(request, response, mappedValue);
}
}
7.6.2 FormAuthenticationFilter配置
修改applicationContext-shiro.xml中對FormAuthenticationFilter的配置。
n 在shiroFilter中添加filters:
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="filters">
<map>
<!--FormAuthenticationFilter是基於表單認證的過慮器 -->
<entry key="authc"value-ref="formAuthenticationFilter" />
</map>
</property>
.....
.....
n formAuthenticationFilter定義
<!-- 基於Form表單的身份驗證過濾器,不配置將也會註冊此過慮器,表單中的用戶賬號、密碼及loginurl將採用默認值,建議配置 -->
<bean id="formAuthenticationFilter"
class="org.apache.shiro.web.filter.authc.MyFormAuthenticationFilter">
<!-- 表單中賬號的input名稱 -->
<property name="usernameParam"value="username" />
<!-- 表單中密碼的input名稱 -->
<property name="passwordParam"value="password" />
</bean>
7.6.3 登陸頁面
添加驗證碼:
<TR>
<TD>驗證碼:</TD>
<TD><input id="randomcode"name="randomcode" size="8" /><img
id="randomcode_img" src="${baseurl}validatecode.jsp" alt=""
width="56" height="20"align='absMiddle' /><a
href=javascript:randomcode_refresh()>刷新</a></TD>
</TR>
7.6.4 配置validatecode.jsp匿名訪問
修改applicationContext-shiro.xml:
7.7 記住我
用戶登陸選擇“自動登陸”本次登陸成功會向cookie寫身份信息,下次登陸從cookie中取出身份信息實現自動登陸。
7.7.1 用戶身份實現java.io.Serializable接口
向cookie記錄身份信息需要用戶身份信息對象實現序列化接口,如下:
7.7.2 配置rememberMeManager
<!-- 安全管理器 -->
<bean id="securityManager"class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="realm"ref="userRealm" />
<property name="sessionManager"ref="sessionManager" />
<property name="cacheManager"ref="cacheManager"/>
<!-- 記住我 -->
<property name="rememberMeManager"ref="rememberMeManager"/>
</bean>
<!--rememberMeManager管理器 -->
<bean id="rememberMeManager"class="org.apache.shiro.web.mgt.CookieRememberMeManager">
<property name="cookie"ref="rememberMeCookie" />
</bean>
<!-- 記住我cookie -->
<bean id="rememberMeCookie"class="org.apache.shiro.web.servlet.SimpleCookie">
<constructor-arg value="rememberMe"/>
<!-- 記住我cookie生效時間30天 -->
<property name="maxAge"value="2592000" />
</bean>
7.7.3 FormAuthenticationFilter配置
修改formAuthenticationFitler添加頁面中“記住我checkbox”的input名稱:
<bean id="formAuthenticationFilter"
class="cn.itcast.ssm.shiro.MyFormAuthenticationFilter">
<!-- 表單中賬號的input名稱 -->
<property name="usernameParam"value="usercode" />
<!-- 表單中密碼的input名稱 -->
<property name="passwordParam"value="password" />
<property name="rememberMeParam"value="rememberMe"/>
</bean>
7.7.4 登陸頁面
在login.jsp中添加“記住我”checkbox。
<TR>
<TD></TD>
<TD>
<input type="checkbox"name="rememberMe" />自動登陸
</TD>
</TR>