首页 Java java教程 shiro源码的详细介绍

shiro源码的详细介绍

Jul 26, 2017 pm 04:36 PM
shiro 分析 源码

(一)//1、获取SecurityManager工厂,此处使用Ini配置文件初始化SecurityManager
Factory factory = new IniSecurityManagerFactory("classpath:shiro.ini")
2.factory类的类结构为:
 

(3)abstractFactory类主要设置是否单例

(4)iniFactorySupport是支持ini设置创建的对象

(5)iniSecuritymanagerFactory是ini方式创建securityManager的实现类

(二)//2、得到SecurityManager实例 并绑定给SecurityUtils
org.apache.shiro.mgt.SecurityManagersecurityManager = factory.getInstance();
SecurityUtils.setSecurityManager(securityManager);
(1)通过创建的Ini对象创建Securitymanager对象
1  IniSecurityManagerFactory类:2      creatSecuritymanager(ini){3      SecurityManager securityManager = createSecurityManager(ini);4      return securityManager;5  }
登录后复制

 

(2)通过创建的Ini对象创建Securitymanager对象
 1 private SecurityManager createSecurityManager(Ini ini) { 2 //null 3 Ini.Section mainSection = ini.getSection(MAIN_SECTION_NAME); 4 if (CollectionUtils.isEmpty(mainSection)) { 5  6 //try the default: null 7 mainSection = ini.getSection(Ini.DEFAULT_SECTION_NAME); 8 } 9  return createSecurityManager(ini, mainSection);10  }
登录后复制

 (3)通过ini对象和主模块mainSession创建Securitymanager

 1 private SecurityManager createSecurityManager(Ini ini, Ini.Section mainSection) { 2 //{securityManager=DefaultSecurityManager,iniRealm=IniRealm}   3 Map defaults = createDefaults(ini, mainSection); 4 Map objects = buildInstances(mainSection, defaults); 5  6 SecurityManager securityManager = getSecurityManagerBean(); 7 boolean autoApplyRealms = isAutoApplyRealms(securityManager); 8 if (autoApplyRealms) { 9 //realms and realm factory might have been created - pull them out first  so we can initialize the securityManager:10  Collection realms = getRealms(objects);11  //set them on the SecurityManager12  if (!CollectionUtils.isEmpty(realms)) {13          applyRealmsToSecurityManager(realms, securityManager);14            }15         }16        return securityManager;17     }
登录后复制
(4)通过mainSession和默认object对象设置对象之间的关联
 1 private Map buildInstances(Ini.Section section, Map defaults) { 2      this.builder = new ReflectionBuilder(defaults); 3      return this.builder.buildObjects(section); 4      } 5 //类ReflectionBuilder 6 //通过mainSection创建对象并关联 7  public Map buildObjects(Map kvPairs) { 8   ..... 9  LifecycleUtils.init(objects.values());10 }
登录后复制
 (5)因为IniRealm实现了Initializable,则初始化IniRealm对象
 1 //类IniRealm 2 private void processDefinitions(Ini ini) { 3  Ini.Section usersSection = ini.getSection(USERS_SECTION_NAME); 4  processUserDefinitions(usersSection); 5 } 6 //通过userSection解析user模块 7  protected void processUserDefinitions(Map userDefs) { 8       for (String username : userDefs.keySet()) { 9          ........10       account = new SimpleAccount(username, password, getName());11       add(account);12         ........13        }14  }15 protected void add(SimpleAccount account) {16    String username = getUsername(account);17    USERS_LOCK.writeLock().lock();18    try {19     this.users.put(username, account);20    }finally {21     USERS_LOCK.writeLock().unlock();22 }
登录后复制
IniRealm的类结构为:

simpleAccount的结构为:
至此,创建对象关联,IniRealm的初始化完成!接下来看DefaultSecurityManager的结构图:

(7)设置DefaultSecurityManager的realm属性:
1  applyRealmsToSecurityManager(realms, securityManager){2     ((RealmSecurityManager) securityManager).setRealms(realms);3 }4 //在类RealmSecurityManager中5 public void setRealms(Collection realms) {6    this.realms = realms;7    afterRealmsSet();8 }
登录后复制

 注意:afterRealmsSet();主要用来设置authenticator、authorizer的realm属性:

           至此DefaultSecurityManager的属性设置完成、并返回DefaultSecurityManager对象
//3、得到Subject及创建用户名/密码身份验证Token(即用户身份/凭证)
Subject subject = SecurityUtils.getSubject();
 
1 //获取主题对象 2 public static Subject getSubject() {3     Subject subject = ThreadContext.getSubject();//第一次null4     if (subject == null) {5        subject = (new Subject.Builder()).buildSubject();6        ThreadContext.bind(subject);7         }8       return subject;9 }
登录后复制

 

(1)代码分析:使用建造者模式创建对象:

 1 public static class Builder{ 2        SubjectContext subjectContext; 3        SecurityManager securityManager; 4         public Builder(SecurityManager securityManager) { 5             if (securityManager == null) { 6                 throw new NullPointerException("SecurityManager method argument cannot be null."); 7             } 8             this.securityManager = securityManager; 9             this.subjectContext = newSubjectContextInstance();//DefaultSubjectContext(初始化一个backMap集合)10             if (this.subjectContext == null) {11                 throw new IllegalStateException("Subject instance returned from 'newSubjectContextInstance' " +12                         "cannot be null.");13             }14             this.subjectContext.setSecurityManager(securityManager);15         }16        public Subject buildSubject() {17             return this.securityManager.createSubject(this.subjectContext);18         }19 }
登录后复制

 (2)使用主题上下文创建主题

 1   public Subject createSubject(SubjectContext subjectContext) { 2         //create a copy so we don't modify the argument's backing map: 3         SubjectContext context = copy(subjectContext); 4  5         //ensure that the context has a SecurityManager instance, and if not, add one: 6         context = ensureSecurityManager(context);//DefaultSubjectContext.backMap.put(SecurityManage) 7  8         //Resolve an associated Session (usually based on a referenced session ID), and place it in the context before 9         //sending to the SubjectFactory.  The SubjectFactory should not need to know how to acquire sessions as the10         //process is often environment specific - better to shield the SF from these details:11         context = resolveSession(context);12 13         //Similarly, the SubjectFactory should not require any concept of RememberMe - translate that here first14         //if possible before handing off to the SubjectFactory:15         context = resolvePrincipals(context);16 17         Subject subject = doCreateSubject(context);18 19         //save this subject for future reference if necessary:20         //(this is needed here in case rememberMe principals were resolved and they need to be stored in the21         //session, so we don't constantly rehydrate the rememberMe PrincipalCollection on every operation).22         //Added in 1.2:23         save(subject);24 25         return subject;26     }
登录后复制

 (3)通过主题创建subject对象

 protected Subject doCreateSubject(SubjectContext context) {return getSubjectFactory().createSubject(context);
    }
登录后复制

 

(4)DefaultSubjectFactory创建主题对象:
 1    public Subject createSubject(SubjectContext context) { 2         SecurityManager securityManager = context.resolveSecurityManager(); 3         Session session = context.resolveSession(); 4         boolean sessionCreationEnabled = context.isSessionCreationEnabled(); 5         PrincipalCollection principals = context.resolvePrincipals(); 6         boolean authenticated = context.resolveAuthenticated(); 7         String host = context.resolveHost(); 8 9         return new DelegatingSubject(principals, authenticated, host, session, sessionCreationEnabled, securityManager);10     }
登录后复制

以上是shiro源码的详细介绍的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

如何在uniapp中实现数据统计和分析 如何在uniapp中实现数据统计和分析 Oct 24, 2023 pm 12:37 PM

如何在uniapp中实现数据统计和分析一、背景介绍数据统计和分析是移动应用开发过程中非常重要的一环,通过对用户行为的统计和分析,开发者可以深入了解用户的喜好和使用习惯,从而优化产品设计和用户体验。本文将介绍如何在uniapp中实现数据统计和分析的功能,并提供一些具体的代码示例。二、选择合适的数据统计和分析工具在uniapp中实现数据统计和分析的第一步是选择合

PHP代码在浏览器中如何显示源码而不被解释执行? PHP代码在浏览器中如何显示源码而不被解释执行? Mar 11, 2024 am 10:54 AM

PHP代码在浏览器中如何显示源码而不被解释执行?PHP是一种服务器端脚本语言,通常用于开发动态网页。当PHP文件在服务器上被请求时,服务器会解释执行其中的PHP代码,并将最终的HTML内容发送到浏览器以供显示。然而,有时我们希望在浏览器中直接展示PHP文件的源代码,而不是被执行。本文将介绍如何在浏览器中显示PHP代码的源码,而不被解释执行。在PHP中,可以使

网站在线看源码 网站在线看源码 Jan 10, 2024 pm 03:31 PM

可以使用浏览器的开发者工具来查看网站的源代码,在Google Chrome浏览器中:1、打开 Chrome 浏览器,访问要查看源代码的网站;2、右键单击网页上的任何位置,然后选择“检查”或按下快捷键 Ctrl + Shift + I打开开发者工具;3、在开发者工具的顶部菜单栏中,选择“Elements”选项卡;4、看到网站的 HTML 和 CSS 代码即可。

织梦CMS二级目录打不开的原因分析 织梦CMS二级目录打不开的原因分析 Mar 13, 2024 pm 06:24 PM

标题:解析织梦CMS二级目录打不开的原因及解决方案织梦CMS(DedeCMS)是一款功能强大的开源内容管理系统,被广泛应用于各类网站建设中。然而,有时候在搭建网站过程中可能会遇到二级目录无法打开的情况,这给网站的正常运行带来了困扰。在本文中,我们将分析二级目录打不开的可能原因,并提供具体的代码示例来解决这一问题。一、可能的原因分析:伪静态规则配置问题:在使用

Python在智能交通系统中的应用案例分析 Python在智能交通系统中的应用案例分析 Sep 08, 2023 am 08:13 AM

Python在智能交通系统中的应用案例分析摘要:随着智能交通系统的快速发展,Python作为一种多功能、易于学习和使用的编程语言,被广泛应用于智能交通系统的开发和应用中。本文通过分析Python在智能交通系统中的应用案例,并给出相关的代码示例,展示了Python在智能交通领域中的优势和应用潜力。引言智能交通系统是指利用现代通信、信息、传感等技术手段,通过对交

分析腾讯主要的编程语言是否为Go 分析腾讯主要的编程语言是否为Go Mar 27, 2024 pm 04:21 PM

标题:腾讯主要的编程语言是否为Go:一项深入分析腾讯作为中国领先的科技公司,在编程语言的选择上一直备受关注。近年来,有人认为腾讯主要采用Go作为主要的编程语言。本文将对腾讯主要的编程语言是否为Go进行深入分析,并给出具体的代码示例来支持这一观点。一、Go语言在腾讯的应用Go是一种由Google开发的开源编程语言,它的高效性、并发性和简洁性受到众多开发者的喜

分析静态定位技术的优缺点 分析静态定位技术的优缺点 Jan 18, 2024 am 11:16 AM

静态定位技术的优势与局限性分析随着现代科技的发展,定位技术已经成为我们生活中不可或缺的一部分。而静态定位技术作为其中的一种,具有其特有的优势和局限性。本文将对静态定位技术进行深入分析,以便更好地了解其应用现状和未来的发展趋势。首先,我们来看一下静态定位技术的优势所在。静态定位技术是通过对待定位对象进行观测、测量和计算来实现位置信息的确定。相较于其他定位技术,

golang框架源码学习与应用全面指南 golang框架源码学习与应用全面指南 Jun 01, 2024 pm 10:31 PM

通过理解Golang框架源码,开发者可以掌握语言精髓和扩展框架功能。首先,获取源码并熟悉其目录结构。其次,阅读代码、跟踪执行流和理解依赖关系。实战案例展示了如何应用这些知识:创建自定义中间件并扩展路由系统。最佳实践包括分步学习、避免盲目复制粘贴、利用工具和参考在线资源。

See all articles