亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Apache Shiro 使用手冊(cè)(四) Realm 實(shí)現(xiàn)

 更新時(shí)間:2014年06月09日 23:39:13   作者:  
在認(rèn)證、授權(quán)內(nèi)部實(shí)現(xiàn)機(jī)制中都有提到,最終處理都將交給Real進(jìn)行處理。因?yàn)樵赟hiro中,最終是通過Realm來獲取應(yīng)用程序中的用戶、角色及權(quán)限信息的

在認(rèn)證、授權(quán)內(nèi)部實(shí)現(xiàn)機(jī)制中都有提到,最終處理都將交給Real進(jìn)行處理。因?yàn)樵赟hiro中,最終是通過Realm來獲取應(yīng)用程序中的用戶、角色及權(quán)限信息的。通常情況下,在Realm中會(huì)直接從我們的數(shù)據(jù)源中獲取Shiro需要的驗(yàn)證信息??梢哉f,Realm是專用于安全框架的DAO.

一、認(rèn)證實(shí)現(xiàn)

正如前文所提到的,Shiro的認(rèn)證過程最終會(huì)交由Realm執(zhí)行,這時(shí)會(huì)調(diào)用Realm的getAuthenticationInfo(token)方法。
該方法主要執(zhí)行以下操作:
1、檢查提交的進(jìn)行認(rèn)證的令牌信息
2、根據(jù)令牌信息從數(shù)據(jù)源(通常為數(shù)據(jù)庫(kù))中獲取用戶信息
3、對(duì)用戶信息進(jìn)行匹配驗(yàn)證。
4、驗(yàn)證通過將返回一個(gè)封裝了用戶信息的AuthenticationInfo實(shí)例。
5、驗(yàn)證失敗則拋出AuthenticationException異常信息。

而在我們的應(yīng)用程序中要做的就是自定義一個(gè)Realm類,繼承AuthorizingRealm抽象類,重載doGetAuthenticationInfo (),重寫獲取用戶信息的方法。

復(fù)制代碼 代碼如下:

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;
  }
}

二、授權(quán)實(shí)現(xiàn)

而授權(quán)實(shí)現(xiàn)則與認(rèn)證實(shí)現(xiàn)非常相似,在我們自定義的Realm中,重載doGetAuthorizationInfo()方法,重寫獲取用戶權(quán)限的方法即可。

復(fù)制代碼 代碼如下:

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;
  }
}

相關(guān)文章

最新評(píng)論