首頁 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屬性:

          至此Default
          至此Default
          完成、並回傳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)

熱門話題

Java教學
1664
14
CakePHP 教程
1421
52
Laravel 教程
1315
25
PHP教程
1266
29
C# 教程
1239
24
如何在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 程式碼即可。

Python在智慧交通系統中的應用案例分析 Python在智慧交通系統中的應用案例分析 Sep 08, 2023 am 08:13 AM

Python在智慧交通系統中的應用案例分析摘要:隨著智慧交通系統的快速發展,Python作為一種多功能、易於學習和使用的程式語言,被廣泛應用於智慧交通系統的開發和應用中。本文透過分析Python在智慧交通系統中的應用案例,並給出相關的程式碼範例,展示了Python在智慧交通領域的優勢和應用潛力。引言智慧交通系統是指利用現代通訊、資訊、感測等技術手段,透過對交

織夢CMS二級目錄打不開的原因分析 織夢CMS二級目錄打不開的原因分析 Mar 13, 2024 pm 06:24 PM

標題:解析織夢CMS二級目錄打不開的原因及解決方案織夢CMS(DedeCMS)是一款功能強大的開源內容管理系統,被廣泛應用於各類網站建設中。然而,有時在搭建網站過程中可能會遇到二級目錄無法開啟的情況,這給網站的正常運作帶來了困擾。在本文中,我們將分析二級目錄打不開的可能原因,並提供具體的程式碼範例來解決這個問題。一、可能的原因分析:偽靜態規則配置問題:在使用

分析騰訊主要的程式語言是否為Go 分析騰訊主要的程式語言是否為Go Mar 27, 2024 pm 04:21 PM

標題:騰訊主要的程式語言是否為Go:一項深入分析騰訊作為中國領先的科技公司,在程式語言的選擇上一直備受關注。近年來,有人認為騰訊主要採用Go作為主要的程式語言。本文將對騰訊主要的程式語言是否為Go進行深入分析,並給出具體的程式碼範例來支持這一觀點。一、Go語言在騰訊的應用Go是一種由Google開發的開源程式語言,它的高效性、並發性和簡潔性受到眾多開發者的喜

分析靜態定位技術的優缺點 分析靜態定位技術的優缺點 Jan 18, 2024 am 11:16 AM

靜態定位技術的優勢與限制分析隨著現代科技的發展,定位技術已成為我們生活中不可或缺的一部分。而靜態定位技術作為其中的一種,具有其特有的優點與限制。本文將對靜態定位技術進行深入分析,以便更了解其應用現狀和未來的發展趨勢。首先,我們來看看靜態定位技術的優勢所在。靜態定位技術是透過對待定位物件進行觀測、測量和計算來實現位置資訊的確定。相較於其他定位技術,

分析並解決Tomcat閃退的原因 分析並解決Tomcat閃退的原因 Jan 13, 2024 am 10:36 AM

Tomcat閃退原因分析及解決方法引言:隨著網際網路的快速發展,越來越多的應用程式被開發出來並部署在伺服器上以提供服務。而Tomcat作為常見的JavaWeb伺服器,在應用程式開發中得到了廣泛的應用。然而,有時候我們可能會遇到Tomcat閃退的問題,這會導致應用程式無法正常運作。本文將介紹Tomcat閃退的原因分析,並提供解決方法,同時給出具體的程式碼範例

See all articles