Shiro源码分析-初始化-Realm
在上一篇介绍SecurityManager的初始化过程中,也有realm的粗略介绍。
realm的概念在安全领域随处可见:
各种中间件的realm、spring security的realm、shiro的realm。。。如下:
tomcat的realm: http://tomcat.apache.org/tomcat-7.0-doc/realm-howto.html
weblogic的realm:http://edocs.weblogicfans.net/wls/docs92/secintro/realm_chap.html
spring security的realm http://www.oschina.net/translate/spring-security-basic-authentication?lang=eng
tomcat官网对realm的定义是这样的:
A Realm is a "database" of usernames and passwords that identify valid users of a web application (or set of web applications), plus an enumeration of the list of roles associated with each valid user. You can think of roles as similar to groups in Unix-like operating systems, because access to specific web application resources is granted to all users possessing a particular role (rather than enumerating the list of associated usernames). A particular user can have any number of roles associated with their username.
我个人理解realm相当于管理账号密码、角色、权限的仓库。有异议的,欢迎一起讨论。
原谅我的啰嗦,先进入正题。
Shiro的realm类图如下:
由图可见,Realm主要还是认证、授权服务,并提供cache支持。
shiro提供了几种realm可供项目选择,一般项目中比较常用的应该是JdbcRealm,运行测试用例一般使用IniRealm。
开涛在讲解身份验证的章节中演示了两种realm的方式(ini、jdbc)
http://jinnianshilongnian.iteye.com/blog/2019547
一、Ini方式的realm:
、
从图中可看出,shiro对于通过文本方式定义账号、权限提供了Ini、Properties两种方式。SimpleAccountRealm类的users、roles集合分别用来保存账号、权限信息。
还记得在前一篇介绍SecurityManager初始化中,关于realm的创建了么?
没错,就是这里。根据ini配置中的users、roles段落来解析成IniRealm的对象实例。
下面,简单跟踪一下代码吧:
//IniRealm构造函数会调用此方法完成解析操作
private void processDefinitions(Ini ini) {
if (CollectionUtils.isEmpty(ini)) {
log.warn("{} defined, but the ini instance is null or empty.", getClass().getSimpleName());
return;
} Ini.Section rolesSection = ini.getSection(ROLES_SECTION_NAME);
if (!CollectionUtils.isEmpty(rolesSection)) {
log.debug("Discovered the [{}] section. Processing...", ROLES_SECTION_NAME);
//解析roles段落交给父类完成
processRoleDefinitions(rolesSection);
} Ini.Section usersSection = ini.getSection(USERS_SECTION_NAME);
if (!CollectionUtils.isEmpty(usersSection)) {
log.debug("Discovered the [{}] section. Processing...", USERS_SECTION_NAME);
//解析users段落交给父类完成
processUserDefinitions(usersSection);
} else {
......
}
}
niRealm的父类TextConfigurationRealm根据子类的users、roles配置完成解析操作
//解析roles,并构造SimpleRole对象
protected void processRoleDefinitions(Map<String, String> roleDefs) {
if (roleDefs == null || roleDefs.isEmpty()) {
return;
}
for (String rolename : roleDefs.keySet()) {
String value = roleDefs.get(rolename); SimpleRole role = getRole(rolename);
if (role == null) {
role = new SimpleRole(rolename);
add(role);
} Set<Permission> permissions = PermissionUtils.resolveDelimitedPermissions(value, getPermissionResolver());
role.setPermissions(permissions);
}
}
//解析roles,并构造SimpleRole对象
protected void processRoleDefinitions(Map<String, String> roleDefs) {
if (roleDefs == null || roleDefs.isEmpty()) {
return;
}
for (String rolename : roleDefs.keySet()) {
String value = roleDefs.get(rolename); SimpleRole role = getRole(rolename);
if (role == null) {
role = new SimpleRole(rolename);
add(role);
} Set<Permission> permissions = PermissionUtils.resolveDelimitedPermissions(value, getPermissionResolver());
role.setPermissions(permissions);
}
}
//解析users,并构造SimpleAccount对象
protected void processUserDefinitions(Map<String, String> userDefs) {
if (userDefs == null || userDefs.isEmpty()) {
return;
}
for (String username : userDefs.keySet()) { String value = userDefs.get(username); String[] passwordAndRolesArray = StringUtils.split(value); String password = passwordAndRolesArray[]; SimpleAccount account = getUser(username);
if (account == null) {
account = new SimpleAccount(username, password, getName());
add(account);
}
account.setCredentials(password); if (passwordAndRolesArray.length > ) {
for (int i = ; i < passwordAndRolesArray.length; i++) {
String rolename = passwordAndRolesArray[i];
account.addRole(rolename); SimpleRole role = getRole(rolename);
if (role != null) {
account.addObjectPermissions(role.getPermissions());
}
}
} else {
account.setRoles(null);
}
}
}
二、Jdbc方式的realm:
jdbcRealm=org.apache.shiro.realm.jdbc.JdbcRealm
dataSource=com.alibaba.druid.pool.DruidDataSource
dataSource.driverClassName=com.mysql.jdbc.Driver
dataSource.url=jdbc:mysql://localhost:3306/shiro
dataSource.username=root
dataSource.password=root
jdbcRealm.dataSource=$dataSource
securityManager.realms=$jdbcRealm
由于shiro支持在ini中配置依赖注入,那么JdbcRealm中的sql信息也是可配置的。
jdbcRealm.authenticationQuery=用户查询
jdbcRealm.userRolesQuery=角色查询
jdbcRealm.permissionsQuery=权限查询
当然,如果不习惯这种方式,可以直接自定义Realm,并继承自AuthorizingRealm或者JdbcRealm都可以。
JdbcRealm通过查询数据库的认证实体、角色、权限,构造的对象分别是:SimpleAuthenticationInfo、SimpleAuthorizationInfo。
实际上,IniRealm方式构造的SimpleAccount、SimpleRole与JdbcRealm方式构造的SimpleAuthenticationInfo、SimpleAuthorizationInfo一一对应,且都实现认证接口AuthenticationInfo、授权接口AuthorizationInfo。关于更详细的讲解放在后面的认证、授权部分。
三、RealmFactory:
Shiro不仅支持Realm类型,还支持RealmFactory类型,在初始化的时候,如果配置中存在RealmFactory实现类,则直接调用其Collection<Realm> getRealms()方法。
该方式在多个realm的情况下很实用。
四、与Spring Security的比较:
Spring Security的Realm仅仅是用在basic认证方式。
Shiro的realm与Spring Security的UserDetailsService非常相似。但是命名确非常迷糊,下面进一步分析:
先看UserDetailsService接口定义:
//根据用户名称获取UserDetails
public interface UserDetailsService {
UserDetails loadUserByUsername(String username)
throws UsernameNotFoundException, DataAccessException;
}
UserDetails接口定义:
public interface UserDetails extends Serializable {
//获取授权的集合
Collection<GrantedAuthority> getAuthorities();
String getPassword();
String getUsername();
boolean isAccountNonExpired();
boolean isAccountNonLocked();
boolean isCredentialsNonExpired();
boolean isEnabled();
}
由此可看出,Spring Security的UserDetailsService获取的UserDetails已经拥有了授权信息。只是从接口命名中无法得知。
而Shiro的realm是把职责分解了,层次更清晰些。
后续在分析shiro的过程中,会增加与SpringSecurity的比较。
Shiro源码分析-初始化-Realm的更多相关文章
- Shiro源码分析之SecurityManager对象获取
目录 SecurityManager获取过程 1.SecurityManager接口介绍 2.SecurityManager实例化时序图 3.源码分析 4.总结 @ 上篇文章Shiro源码分析之获 ...
- Shiro 源码分析
http://my.oschina.net/huangyong/blog/215153 Shiro 是一个非常优秀的开源项目,源码非常值得学习与研究. 我想尝试做一次 不一样 的源码分析:源码分析不再 ...
- Shiro源码分析
1.入口类:AbstractAuthenticator 用户输入的登录信息经过其authenticate方法: public final AuthenticationInfo authenticate ...
- linux调度器源码分析 - 初始化(二)
本文为原创,转载请注明:http://www.cnblogs.com/tolimit/ 引言 上期文章linux调度器源码分析 - 概述(一)已经把调度器相关的数据结构介绍了一遍,本篇着重通过代码说明 ...
- Linux 内核调度器源码分析 - 初始化
导语 上篇系列文 混部之殇-论云原生资源隔离技术之CPU隔离(一) 介绍了云原生混部场景中CPU资源隔离核心技术:内核调度器,本系列文章<Linux内核调度器源码分析>将从源码的角度剖析内 ...
- linux中断源码分析 - 初始化(二)
本文为原创,转载请注明:http://www.cnblogs.com/tolimit/ 本篇文章主要讲述源码中是如何对中断进行一系列的初始化的. 回顾 在上一篇概述中,介绍了几个对于中断来说非常重要的 ...
- struts2源码分析-初始化流程
这一篇文章主要是记录struts.xml的初始化,还原struts2.xml的初始化流程.源码依据struts2-2.3.16.3版本. struts2初始化入口,位于web.xml中: <fi ...
- Shiro源码分析之Subject和SecurityManager
Subject 毫无疑问,Subject是Shiro最重要的一个概念. “Subject”只是一个安全术语,意味着应用程序用户的特定于安全性的“视图”.Shiro Subject实例代表单个应用程序用 ...
- wifidog源码分析 - 初始化阶段
Wifidog是一个linux下开源的认证网关软件,它主要用于配合认证服务器实现无线路由器的认证放行功能. wifidog是一个后台的服务程序,可以通过wdctrl命令对wifidog主程序进行控制. ...
随机推荐
- LightOJ1005 Rooks(DP/排列组合)
题目是在n*n的棋盘上放k个车使其不互相攻击的方案数. 首先可以明确的是n*n最多只能合法地放n个车,即每一行都指派一个列去放车. dp[i][j]表示棋盘前i行总共放了j个车的方案数 dp[0][0 ...
- WPF之DataContext
1. 继承属性: DataContext is a property on FrameworkElement (base class for all WPF Controls) and is impl ...
- ural 1272. Non-Yekaterinburg Subway
1272. Non-Yekaterinburg Subway Time limit: 1.0 secondMemory limit: 64 MB A little town started to co ...
- C# 文件读写FileInfo
using System; using System.Collections.Generic; using System.Text; using System.IO; namespace Consol ...
- HDU 3853(期望DP)
题意: 在一个r*c的网格中行走,在每个点分别有概率向右.向下或停止不动.每一步需要的时间为2,问从左上角走到右下角的期望时间. SOL: 非常水一个DP...(先贴个代码挖个坑 code: /*== ...
- windows8 开发教程 教你制作 多点触控Helper可将任意容器内任意对象进行多点缩放
http://blog.csdn.net/wangrenzhu2011/article/details/7732907 (转) 实现方法: 对Manipulation进行抽象化 使不同容器可共用多点缩 ...
- COJ1013 : WZJ的数据结构(十三)
这道题有这样一个解法: 首先把边依次加到图中,若当前这条边与图中的边形成了环,那么把这个环中最早加进来的边弹出去并将每条边把哪条边弹了出去记录下来:ntr[i] = j,特别地,要是没有弹出边,ntr ...
- BZOJ1196: [HNOI2006]公路修建问题
Description OI island是一个非常漂亮的岛屿,自开发以来,到这儿来旅游的人很多.然而,由于该岛屿刚刚开发不久,所以那里的交通情况还是很糟糕.所以,OIER Association组织 ...
- html5文章 -- 使用 jQuery Mobile 与 HTML5 开发 Web App —— jQuery Mobile 基础
这篇文章是使用 jQuery Mobile 与 HTML5 开发 Web App 系列的第二篇,在本文以及接下来的数篇文章 Kayo 将会介绍 jQuery Mobile 的组件.事件响应以及可以调用 ...
- 写sql语句连接的时候注意的一个小细节
我在写权限的查询的时候,用到了sql语句的链接写一下出错的时候的代码 $sqlpid="select auth_name from sw_auth where auth_level=0&qu ...