国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網(wǎng)站 > Apache > 正文

Apache Shiro 使用手冊(四) Realm 實現(xiàn)

2024-08-27 18:26:38
字體:
供稿:網(wǎng)友

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

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

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

而在我們的應(yīng)用程序中要做的就是自定義一個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)實現(xiàn)

而授權(quán)實現(xiàn)則與認(rèn)證實現(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;
  }
}

您可能感興趣的文章:

讓Apache Shiro保護(hù)你的應(yīng)用Apache Shiro 使用手冊(五) Shiro 配置說明Apache Shiro 使用手冊(三) Shiro授權(quán)Apache Shiro 使用手冊(二) Shiro 認(rèn)證Apache Shiro 使用手冊(一) Shiro架構(gòu)介紹Apache shiro的簡單介紹與使用教程(與spring整合使用)
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 崇礼县| 华宁县| 宜黄县| 黄山市| 思南县| 建阳市| 射阳县| 色达县| 郎溪县| 行唐县| 北安市| 鄢陵县| 察雅县| 阿鲁科尔沁旗| 云和县| 正阳县| 区。| 来安县| 方城县| 桐城市| 保德县| 祁连县| 民勤县| 沛县| 平乡县| 库尔勒市| 堆龙德庆县| 若尔盖县| 琼中| 天长市| 怀宁县| 武陟县| 商丘市| 安阳县| 泸西县| 政和县| 龙州县| 松原市| 三门县| 寿宁县| 江川县|